You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: datasets/geometries.mdx
+37-44Lines changed: 37 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,35 +5,32 @@ description: Best practices for working with geometries in Tilebox.
5
5
icon: draw-polygon
6
6
---
7
7
8
-
Geometries are a common cause of friction in geospatial data processing. This is especially true when working with geometries that cross the antimeridian or cover a pole.
9
-
Tilebox is designed to take away most of the friction involved in this, but it's still recommended to follow the best practices for handling geometries outlined below.
10
-
11
-
In doing so, you can ensure that no geometry related issues will arise even when interfacing with other libraries and tools that may not properly
12
-
support non-linearities in geometries.
8
+
Geometries are a common cause of friction in geospatial data processing, especially when working with shapes that cross the antimeridian or cover a pole.
9
+
Tilebox minimizes this friction, but adhering to the following best practices ensures optimal results. In doing so, you can ensure that no geometry related issues will
10
+
arise even when interfacing with other libraries and tools that may not properly support non-linearities in geometries.
13
11
14
12
## Best Practices
15
13
16
14
To ensure expected behavior when working with geometries, it's recommended to follow these best practices:
17
15
18
16
1. Use [language specific geometry libraries](#geometry-libraries-and-common-formats) for creating, parsing and serializing geometries.
19
-
2.Polygon exteriors should always be defined in counter-clockwise [winding order](#winding-order).
20
-
3.Polygon holes (interior rings) should always be defined in clockwise [winding order](#winding-order).
21
-
4.Longitude values should always be within the `[-180, 180]`range, and latitude values should always be within the `[-90, 90]` range.
22
-
5.Geometries that cross the antimeridian should be [cut along the antimeridian](#antimeridian-cutting) into two parts—one for the eastern hemisphere and one for the western hemisphere.
23
-
7. Geometries that cover a pole need to be [defined in a specific way](#pole-coverings)to ensure correct handling.
24
-
8. When downloading geometries from external sources, such as from STAC Catalogues always make sure to verify that those assumptions are met to avoid unexpected behavior down the road.
17
+
2.Define polygon exterior rings with a counter-clockwise [winding order](#winding-order).
18
+
3.Define polygon holes (interior rings) with a clockwise [winding order](#winding-order).
19
+
4.Ensure longitude values are within `[-180, 180]` and latitude values are within `[-90, 90]`.
20
+
5.For geometries crossing the antimeridian, [cut them along the antimeridian](#antimeridian-cutting) into two parts: one for the eastern hemisphere and one for the western.
21
+
6. Define geometries covering a pole in [a specific way](#pole-coverings)for correct handling.
22
+
7. When downloading geometries from external sources (for example from STAC Catalogs), always verify these assumptions to prevent unexpected behavior before ingesting them into Tilebox.
25
23
26
24
## Geometry vs Geography
27
25
28
-
Some systems expose two similar, but different data types related to geometries: `Geometry` and `Geography`. The main difference lies in how edge interpolation along the antimeridian is handled.
29
-
`Geometry` represents a geometry in an arbitrary 2D cartesian coordinate system, and does not perform any edge interpolation, while `Geography` typically does wrap around the antimeridian by performing edge interpolation, such as converting cartesian coordinates to a 3D spherical coordinate system.
30
-
For `Geography` types, the `x` coordinate is typically limited to the `[-180, 180]` range, and the `y` coordinate is limited to the `[-90, 90]` range, while for `Geometry` types no such limitations are imposed.
26
+
Some systems distinguish between `Geometry` and `Geography` data types. The primary distinction lies in how edge interpolation is handled along the antimeridian. `Geometry` represents a shape in an arbitrary 2D Cartesian coordinate system and does not perform edge interpolation.
27
+
In contrast, `Geography` typically wraps around the antimeridian by performing edge interpolation, such as converting Cartesian coordinates to a 3D spherical coordinate system. Typically, Geography types limit `x` coordinates to `[-180, 180]` and `y` coordinates to `[-90, 90]`, whereas `Geometry` types have no such limitations.
31
28
32
-
Tilebox does not make such a distinction between `Geometry` and `Geography` types. Instead, it provides a [query option to specify a coordinate reference system](/datasets/query/filter-by-location#coordinate-reference-system) to control whether geometry intersections and containment checks are performed in a 3D spherical coordinate system (which correctly handles antimeridian crossings) or in a standard 2D cartesian `lat/lon` coordinate system.
29
+
Tilebox does not differentiate between `Geometry` and `Geography` types. Instead, it offers a [query option to specify a coordinate reference system](/datasets/query/filter-by-location#coordinate-reference-system). This controls whether geometry intersections and containment checks are performed in a 3D spherical coordinate system (which correctly handles antimeridian crossings) or a standard 2D Cartesian lat/lon coordinate system.
33
30
34
31
## Terminology
35
32
36
-
Get familiar with some key concepts related to geometries.
33
+
Familiarize yourself with key geometry concepts.
37
34
38
35
<Tip>
39
36
A great resource to learn more about these concepts is this [blog post by Tom MacWright](https://macwright.com/2015/03/23/geojson-second-bite).
@@ -50,15 +47,15 @@ Every ring should be explicitly closed, meaning that the first and last point sh
50
47
51
48
**Polygon**
52
49
53
-
A `Polygon` is a collection of rings. The first ring is called the `exterior ring`, and represents the boundary of the polygon. Any other rings are called `interior rings`, and represent holes in the polygon.
50
+
A `Polygon` is a collection of rings. The first ring, the `exterior ring` defines the polygon's boundary. Any other rings are called `interior rings`, representing holes within the polygon.
54
51
55
52
**MultiPolygon**
56
53
57
54
A `MultiPolygon` is a collection of polygons.
58
55
59
56
## Geometry libraries and common formats
60
57
61
-
Geometries can be expressed in many different ways, and many formats exist for representing geometries. To handle these, the [Tilebox Client SDKs](/sdks/introduction) delegate geometry handling to external, well-known geometry libraries.
58
+
Geometries can be expressed in different formats. The [Tilebox Client SDKs](/sdks/introduction) delegate geometry handling to external, well-known libraries.
62
59
63
60
| Client SDK | Geometry library used by Tilebox |
64
61
| ---------- | -------------------------------- |
@@ -84,7 +81,7 @@ area := orb.Polygon{
84
81
```
85
82
</CodeGroup>
86
83
87
-
Through these libraries, Tilebox also supports some of the most common formats for representing geometries.
84
+
These librariesalso enable Tilebox to support common geometry formats.
88
85
89
86
### GeoJSON
90
87
@@ -112,7 +109,7 @@ Reading such a GeoJSON geometry can be achieved as follows.
There is also an extended well known binary format (ewkb) that supports additional information such as specifying a spatial reference system (like EPSG:4326) in the encoded geometry.
192
-
Pythons `shapely` library supports that out of the box, and the `orb` library for Go supports it as well via the `github.com/paulmach/orb/encoding/ewkb` package.
189
+
Pythons `shapely` library supports this out of the box, and the `orb` library for Go supports it as well via the `github.com/paulmach/orb/encoding/ewkb` package.
193
190
</Tip>
194
191
195
192
196
193
## Winding Order
197
194
198
-
The winding order of a ring defines the orientation of the ring, and is either clockwise or counter-clockwise. The winding order of a ring is determined by the order of the points within the ring.
195
+
A ring's winding order defines its orientation (clockwise or counter-clockwise), determined by the sequence of its points.
199
196
200
197
Geometries in Tilebox follow the right hand rule defined by the [GeoJSON specification](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6):
201
198
202
199
{/* vale off */}
203
200
> A linear ring MUST follow the right-hand rule with respect to the area it bounds, i.e., exterior rings are counterclockwise, and holes are clockwise.
204
201
{/* vale on */}
205
202
206
-
This means that the exterior ring of a polygon has a counter-clockwise winding order, while interior rings have a clockwise winding order.
207
-
208
-
When this rule is not followed, querying may yield unexpected results, as can be seen in the example below.
203
+
Thus, polygon exterior rings must have a counter-clockwise winding order, and interior rings must have a clockwise winding order. Failing to follow this rule can lead to unexpected query results, as shown below.
209
204
210
205
Take the following `Polygon` consisting of the same exterior ring but in different winding orders.
211
206
@@ -234,17 +229,17 @@ This is how those two geometries would be interpreted on a sphere.
234
229
</Columns>
235
230
236
231
<Tip>
237
-
Tilebox detects incorrect winding orders and automatically fixes them when ingesting data.
232
+
Tilebox automatically detects and corrects incorrect winding orders during data ingestion.
238
233
</Tip>
239
234
240
-
Since such unexpected behavior can cause issues and be hard to debug, Tilebox does detect incorrect winding orders and automatically fixes them when ingesting data.
241
-
This means that Geometries that really cover almost the whole globe except a small area**cannot** be ingested into Tilebox by simply specifying them as a `Polygon` with an exterior ring in clockwise winding order.
235
+
Since such unexpected behavior can cause issues and be hard to debug, Tilebox detects incorrect winding orders and automatically fixes them during ingestion.
236
+
This means that geometries covering nearly the entire globe except for a small holes**cannot** be ingested into Tilebox by simply specifying them as a `Polygon` with an exterior ring in clockwise winding order.
242
237
243
238
Instead, such geometries should be defined as a `Polygon` consisting of two rings:
244
239
- an exterior ring covering the whole globe in counter-clockwise winding order
245
240
- an interior ring specifying the hole in clockwise winding order
246
241
247
-
Such a definition, which Tilebox will interpret as intended, and also is valid according to the [GeoJSON specification](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6), is shown below.
242
+
Such a definition, as shown below, ensures correct interpretation by Tilebox, and is also valid according to the [GeoJSON specification](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6).
248
243
249
244
```plaintext Polygon covering the whole globe with a hole over Europe
Geometries that cross the antimeridian are a common occurrence in satellite data, but can cause issues when not handled correctly.
283
+
Geometries that cross the antimeridian are a common in satellite data, but can cause issues when not handled correctly.
289
284
290
285
Take the following `Polygon` that crosses the antimeridian.
291
286
@@ -300,15 +295,15 @@ Below are a couple of different ways to express such a geometry.
300
295
POLYGON ((173 5, -175 3, -172 20, 177 23, 173 5))
301
296
```
302
297
303
-
While this is a validrepresentation, it causes issues for a lot of libraries that do calculations or visualization in a cartesian coordinate system, since the line from `lon=173` to `lon=-175`will be interpreted as a line that is `348` degrees long, crossing the null meridian and almost all the way around the globe.
304
-
Often times, visualizations of such geometries will look like in the image below.
298
+
While valid, this representation can cause issues for many libraries performing calculations or visualizations in a Cartesian coordinate system. The line segment from `lon=173` to `lon=-175`is interpreted as a `348-degree` line crossing the null meridian, spanning nearly the entire globe.
299
+
Visualizations of such geometries often appear as shown below.
305
300
306
301
<Framecaption="Incorrect handling of a geometry crossing the antimeridian">
307
302
<imgsrc="/assets/datasets/geometries/buggy-antimeridian-light.png"alt="Polygon crossing the antimeridian"className="dark:hidden" />
308
303
<imgsrc="/assets/datasets/geometries/buggy-antimeridian-dark.png"alt="Polygon crossing the antimeridian"className="hidden dark:block" />
309
304
</Frame>
310
305
311
-
To avoid such issues, some tools working in cartesian coordinate space sometimes extend the possible longitude range beyond to usual bounds of `[-180, 180]`.
306
+
To mitigate these issues, some Cartesian coordinate tools extend the longitude range beyond the usual `[-180, 180]` bounds.
312
307
Expressing the geometry in such a way, may look like this.
313
308
314
309
```plaintext Extending the longitude range beyond 180
@@ -321,10 +316,8 @@ Or, the exact same area on the globe can also be expressed as a geometry by exte
While most visualization tools will probably handle such geometries correctly, special care needs to be taken when
325
-
working with such geometries when it comes to intersection and containment checks.
326
-
327
-
The below code snippet illustrates this, by constructing a `Polygon` that covers the exact same area in the two different methods shown, and checking whether they intersect each other (which they should, since they represent the same area on the globe).
319
+
While most visualization tools may handle such geometries correctly, special care is required for intersection and containment checks.
320
+
The following code snippet demonstrates this by constructing a Polygon that covers the same area using both methods and checking for intersection (which should logically occur, as they represent the same geographic area).
328
321
329
322
```python Incorrect intersection check
330
323
from shapely import from_wkt
@@ -363,18 +356,18 @@ By cutting geometries along the antimeridian:
363
356
364
357
## Pole Coverings
365
358
366
-
Geometries that cover a pole can be especially tricky to handle correctly. No one algorithm that can handle all possible cases correctly, and different libraries and tools may handle them differently.
367
-
A lot of times, injecting prior knowledge, such as the fact that a geometry is supposed to cover a pole, can help to resolve such issues. For an example of this, check out the relevant section in
368
-
the [antimeridian documentation](https://www.gadom.ski/antimeridian/latest/failure-modes/#force-the-geometry-over-the-north-pole).
359
+
Geometries covering a pole can be particularly challenging to handle correctly. No single algorithm addresses all cases, and different libraries and tools may interpret them differently.
360
+
Often, providing prior knowledge, such as confirming that a geometry is intended to cover a pole, can resolve these issues.
361
+
For an example of this, check out the relevant section in the [antimeridian documentation](https://www.gadom.ski/antimeridian/latest/failure-modes/#force-the-geometry-over-the-north-pole).
369
362
370
363
Generally speaking though, there are two approaches that work well:
371
364
372
365
### Approach 1: Cutting out a hole
373
366
374
-
Define a geometry with an exterior ring that covers the whole globe. Then cut out a hole by defining an interior ring of the area that is not covered by the geometry.
367
+
Define a geometry with an exterior ring that covers the whole globe. Then, cut out a hole by defining an interior ring of the area that not covered by the geometry.
375
368
376
369
<Tip>
377
-
This approach works especially well for geometries that cover both poles, since then the interior ring is guaranteed to not cover any of the poles.
370
+
This approach works especially well for geometries that cover both poles, since then the interior ring is guaranteed to not cover any poles.
378
371
</Tip>
379
372
380
373
<Columnscols={2}>
@@ -397,8 +390,8 @@ POLYGON (
397
390
398
391
### Approach 2: Splitting into multiple parts
399
392
400
-
Another approach, that works well for circular caps covering a pole, involves cutting the geometry into multiple parts, but instead of splitting the geometry only at the antimeridian, also splitting it at the null meridian, the `90` and `-90` meridians.
401
-
For a circular cap covering the north pole, this results in four triangular parts, which can be combined into a `MultiPolygon`. Visualization libraries as well as intersection and containment checks done in cartesian coordinate space will then typically handle such a geometry correctly.
393
+
Another approach, effective for circular caps covering a pole, involves cutting the geometry into multiple parts. Instead of splitting the geometry only at the antimeridian, also split it at the null meridian, and the `90` and `-90` meridians.
394
+
For a circular cap covering the north pole, this results in four triangular parts, which can be combined into a `MultiPolygon`. Visualization libraries and intersection/containment checks performed in cartesian coordinate space will typically handle such a geometry correctly.
402
395
403
396
<Framecaption="A geometry covering the north pole">
404
397
<imgsrc="/assets/datasets/geometries/pole-covering-light.png"alt="Polygon covering a pole"className="dark:hidden" />
0 commit comments