Skip to content

Commit 3402f45

Browse files
Spatial tut 3
1 parent 662a018 commit 3402f45

9 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: Intro to SAP HANA Spatial: Polygons
3+
description: A polygon defines a region of space
4+
tags: [ tutorial>beginner, topic>big-data, topic>sql, products>sap-hana, products>sap-hana,-express-edition ]
5+
---
6+
## Prerequisites
7+
- **Proficiency:** Beginner
8+
- **Tutorials:** [Intro to SAP HANA Spatial: Strings](http://go.sap.com/developer/tutorials/hana-spatial-intro2-string.html)
9+
10+
## Next Steps
11+
- Tables with spatial columns (coming soon), or
12+
- Select a tutorial from the [Tutorial Navigator](http://go.sap.com/developer/tutorial-navigator.html) or the [Tutorial Catalog](http://go.sap.com/developer/tutorials.html)
13+
14+
## Details
15+
### You will learn
16+
You will continue learning basics of spatial processing now with ___polygons___ (also known as ___surfaces___) data types.
17+
18+
### Time to Complete
19+
**5 Min**.
20+
21+
---
22+
23+
1. Rings from the previous tutorials, are still strings, i.e. they have length, but no surface. To construct the surface you use ___polygons___. A polygon defines a region of space. A polygon is constructed from one exterior bounding ring that defines the outside of the region and zero or more interior rings which define holes in the region.
24+
25+
2. Open SQL Editor of your choice (web or desktop based) connected to your SAP HANA database instance.
26+
27+
Type and execute the following SQL statement.
28+
```sql
29+
SELECT NEW ST_Polygon('Polygon ((0 0, 4 0, 0 3, 0 0), (0.5 0.5, 0.5 1.5, 1.5 1.5, 1 0.5, 0.5 0.5))').ST_asSVG() FROM dummy;
30+
```
31+
This query instantiate a surface in the 2-dimensional Euclidean space and returns its dimension. In the example above it is a polygon defined by an external ring with a shape of a triangle connecting points (0, 0), i.e. `X=0` and `Y=0`, with points (4, 0) and (4, 3) and internal ring with a shape of a square. The constructor is using ___WKT___. As explained in the previous tutorial, the Well-known text is a text markup language for representing vector geometry objects defined by the Open Geospatial Consortium (OGC).
32+
33+
SVG modified to fill a geometry with yellow color `fill="yellow"` to better illustrate the meaning of the external and internal rings of polygons will display this.
34+
35+
![Polygon with 2 rings](spatial0301.jpg)
36+
37+
3. Now execute the following query.
38+
39+
```sql
40+
SELECT NEW ST_Polygon('Polygon ((0 0, 4 0, 0 3, 0 0), (0.5 0.5, 0.5 1.5, 1.5 1.5, 1 0.5, 0.5 0.5))').ST_Dimension() FROM dummy;
41+
```
42+
43+
The `ST_Dimension()` method will return `2`. In the exercise with the point the same method returned `0`, and in the exercise with strings it returned `1`.
44+
45+
4. Differently from the string, the polygon has a surface, and therefore has the area. Use the `ST_Area()` method to calculate it.
46+
47+
```sql
48+
SELECT NEW ST_Polygon('Polygon ((0 0, 4 0, 0 3, 0 0))').ST_Area() FROM dummy;
49+
```
50+
51+
Please note the double round brackets, as this polygon consists of only external ring.
52+
53+
This statement calculates the area of right triangle (also known as 'right-angled triangle'), with catheti (also known as legs) having lengths of 3 and 4. Obviously area is a half of 3*4 and equal 6.
54+
55+
![Area of the polygon](spatial0302.jpg)
56+
57+
5. Having a polygon you can calculate as well its ___boundary___ using `ST_Boundary()` method.
58+
59+
Check the boundary of the first polygon from this tutorial, i.e. a triangle with a square inside.
60+
61+
```sql
62+
SELECT NEW ST_Polygon('Polygon ((0 0, 4 0, 0 3, 0 0), (0.5 0.5, 0.5 1.5, 1.5 1.5, 1 0.5, 0.5 0.5))').ST_Boundary().ST_asWKT() FROM dummy;
63+
```
64+
65+
![Boundary of the polygon](spatial0302.jpg)
66+
67+
The result is a `MultiLineString` containing two `LineStrings`. One representing a triangle and another representing a square.
68+
69+
`MultiLineString` is another spatial type, which is a collection of line strings. There are two more spatial collection types supported by SAP HANA: `MultiPoint` and `MultiPolygon`. Names are intuitive enough to understand what they represent.
70+
71+
Strings too have their boundaries, represented by their endpoints, except when they are rings. Rings - curves where the start point is the same as the end point and there are no self-intersections — have no boundaries.
72+
73+
Check boundaries of a triangle string from the previous query.
74+
75+
```sql
76+
SELECT NEW ST_LineString('LineString (0 0,4 0,0 3,0 0)').ST_Boundary().ST_asWKT() FROM dummy;
77+
```
78+
79+
The result is an empty geometry.
80+
81+
![Empty boundary](spatial0304.jpg)
82+
83+
Check boundaries of a multi-segment line, where end points are not the same.
84+
85+
```sql
86+
SELECT NEW ST_LineString('LineString (0 0,4 0,0 3,1 0)').ST_Boundary().ST_asWKT() FROM dummy;
87+
```
88+
89+
The result is a `MultiPoint` containing two end points.
90+
91+
![Multipoint boundary](spatial0305.jpg)
92+
93+
Points do not have boundaries.
94+
95+
![Empty boundary for points](spatial0306.jpg)
96+
97+
6. Usual requirement in spatial calculations is finding relationships between different geometries, like if one geometry is covered by another geometry, like if some particular point of interest is within city's boundaries.
98+
99+
`ST_Within()` is one such a method. As with other spatial methods in SAP HANA the result `1` means 'true', and `0` means 'false'.
100+
101+
```sql
102+
SELECT NEW ST_Point (1,1).ST_Within(NEW ST_Polygon('Polygon ((0 0, 4 0, 0 3, 0 0), (0.5 0.5, 0.5 1.5, 1.5 1.5, 1 0.5, 0.5 0.5))')) FROM dummy;
103+
```
104+
105+
Indeed the point (1, 1) is not within interior of your polygon form the earlier exercise, defined by external ring in the shape of triangle and internal ring in the shape of a square. An are within a square is the ___exterior___ of that geometry.
106+
107+
![Within linestring polygon](spatial0307.jpg)
108+
109+
7. To check if a point is within a circle you use `ST_Boundary()` method defining the area of a particular distance from a circle's center point.
110+
111+
```sql
112+
SELECT NEW ST_Point (1,1).ST_Within(NEW ST_Point(0, 0).ST_Buffer(2)) FROM dummy;
113+
```
114+
115+
The point (1, 1) is in the circle with center point (0, 0) and the radius of 2.
116+
117+
![Circle distance](spatial0308.jpg)
118+
119+
### Optional
120+
- Check SAP HANA Spatial Reference at http://help.sap.com/hana_platform for the complete list of objects and methods
121+
122+
## Next Steps
123+
- Tables with spatial columns (coming soon), or
124+
- Select a tutorial from the [Tutorial Navigator](http://go.sap.com/developer/tutorial-navigator.html) or the [Tutorial Catalog](http://go.sap.com/developer/tutorials.html)
13.5 KB
Loading
40.3 KB
Loading
74.5 KB
Loading
52.4 KB
Loading
52 KB
Loading
44 KB
Loading
67.8 KB
Loading
40.4 KB
Loading

0 commit comments

Comments
 (0)