Geometries
How geometries are handled in the Tilebox Python client.
Many datasets consist of granules that represent specific geographical areas on the Earth’s surface. Often, a polygon defining the outline of these areas—a footprint—accompanies other granule metadata in time series datasets. Tilebox provides built-in support for working with geometries.
Here’s an example that loads some granules from the ERS SAR
Opendata dataset, which contains geometries.
Shapely
In the ers_data
dataset, each granule includes a geometry
field that represents the footprint of each granule as a polygon. Tilebox automatically converts geometry fields to Polygon
or MultiPolygon
objects from the Shapely library. By integrating with Shapely, you can use the rich set of libraries and tools it provides. That includes support for computing polygon characteristics such as total area, intersection checks, and conversion to other formats.
Each geometry is a shapely.Geometry.
Geometries are not always Polygon objects. More complex footprint geometries are represented as MultiPolygon objects.
Accessing Coordinates
You can select a polygon from the geometries and access the underlying coordinates and an automatically computed centroid point.
Interactive environments such as Jupyter Notebooks can visualize Polygon shapes graphically. Just type polygon
in an empty cell and execute it for a visual representation of the polygon shape.
Visualization on a Map
To visualize polygons on a map, you can use Folium. Below is a helper function that produces an OpenStreetMap with the Polygon overlaid.
Here’s how to use it.
The visualize
helper function supports a list of polygons, which can display the data layout of the ERS granules.
Format conversion
Shapely supports converting Polygons to some common formats, such as GeoJSON or Well-Known Text (WKT).
Checking intersections
One common task when working with geometries is checking if a given geometry falls into a specific area of interest. Shapely provides an intersects
method for this purpose.
Combining polygons
As shown in the visualization of the granule footprints, the granules collectively form an orbit from pole to pole. Measurements are often combined during processing. You can do the same with geometries by combining them into a single polygon, which represents the hull around all individual footprints using shapely.unary_union.
The computed hull consists of two polygons due to a gap (probably a missing granule) in the geometries. Such geometries are represented as Multi Polygons.
Multi Polygons
A collection of one or more non-overlapping polygons combined into a single geometry is called a MultiPolygon. Footprint geometries can be of type MultiPolygon
due to gaps or pole discontinuities. The computed hull in the previous example is a MultiPolygon
.
Antimeridian Crossings
A common issue with longitude / latitude
geometries is crossings of the 180-degree meridian, or the antimeridian. For example, the coordinates of a LineString
from Japan to the United States might look like this:
140, 141, 142, ..., 179, 180, -179, -178, ..., -125, -124
Libraries like Shapely are not designed to handle spherical coordinate systems, so caution is necessary with such geometries.
Here’s an ERS
granule demonstrating this issue.
This 2D visualization appears incorrect. Both the visualization and any calculations performed may yield inaccurate results. For instance, testing whether the granule intersects the 0-degree meridian provides a false positive.
The GeoJSON specification offers a solution for this problem. In the section Antimeridian Cutting, it suggests always cutting lines and polygons into two parts—one for the eastern hemisphere and one for the western hemisphere. In python, this can be achieved using the antimeridian package.
Since Shapely is unaware of the spherical nature of this data, the centroid of the fixed polygon is still incorrect. The antimeridian package also includes a function to correct this.
Spherical Geometry
Another approach to handle the antimeridian issue is performing all coordinate-related calculations, such as polygon intersections, in a spherical coordinate system.
One useful library for this is spherical_geometry. Here’s an example.
Now, you can compute intersections or check if a particular point is within the polygon. You can compare the incorrect calculation using shapely
with the correct version when using spherical_geometry
.
Was this page helpful?