Geometries
How geometries are handled in the Tilebox python client.
Many datasets consist of granules that represent a certain geographical area on the earths surface. Often times a polygon defining the outline of this area - a footprint - is stored alongside other granule metadata in time series datasets. Tilebox provides special support for working with Geometries out of the box.
Here is an example that loads some granules
of the ERS SAR
Opendata dataset, that contains geometries.
Shapely
In the ers_data
dataset each granule contains a geometry
field which represents the footprint each granule as a polygon.
Tilebox automatically converts such geometry fields to Polygon
or MultiPolygon
objects from the
shapely library.
By integrating with shapely, you can make use of the rich set of libraries and tooling around it, which includes support for computing
polygon characteristics such as the total area, intersection checks or the conversion to other common formats.
Here are some geometries which are part of the ERS granules loaded.
Accessing Coordinates
You can select one polygon out of the geometries, and access the underlying coordinates, as well as an automatically computed centroid point.
Interactive environments such as Jupyter Notebooks can also directly visualize
Polygon shapes graphically, when a shapely.Polygon
is the output of a cell. Just type polygon
in an empty cell and
execute it to get a visual representation of the shape of the polygon.
Visualization on a Map
To visualize polygons on a map, you can use the folium library. Here is a short helper function which produces an OpenStreetMap with the Polygon overlaid on top.
Here is how you can use it.
The visualize
helper function also supports a whole list of polygons, which you can use to showcase the data layout of the ERS granules.
Format conversion
Shapely provides support out of the box for converting the Polygons to some common formats, such as GeoJSON or Well-Known Text (WKT)
Checking intersections
One common task when working with geometries is to check whether a given geometry falls into a certain area of interest.
Fortunately, shapely
provides an intersects method for this use case.
Combining polygons
As you saw in the preceding visualization of the granule footprints, the granules all put together form a whole orbit from pole to pole. Often times measurements are then combined together in certain processing steps. you can also do the same thing for the geometries, and combine them into a single polygon, which represents the hull around all individual footprints. To do so, you can make use of shapely.unary_union
Multi Polygons
As you can see, the computed hull actually consists of two polygons, because there is a gap (probably a missing granule) in the geometries.
Shapely represents such geometries as a shapely.MultiPolygon, which in essence just represent a series of individual polygons put together.
Antimeridian Crossings
One common problem when working with longitude / latitude
geometries like this are crossings of the 180-meridian, or the antimeridian.
Consider the coordinates of a LineString
from Japan to the United States. It’s longitude coordinates would look something like the following.
140, 141, 142, ..., 179, 180, -179, -178, ..., -125, -124
Libraries like shapely
are not designed for handling spherical coordinate systems, extra care needs to be taken when handling such geometries.
The GeoJSON
spec actually provides a solution for this problem.
In the section Antimeridian Cutting they propose to
always cut lines and polygons into two parts, one representing the eastern hemisphere, and the other one the western hemisphere.
As an example, here is an ERS
granule where this exact problem occurs.
This 2D visualization doesn’t seem right. Not only the visualization is wrong, the same also applies for calculations which you may want to do. For example, testing whether the granule intersects the 0-meridian gives the wrong result.
One solution to solve this issue is to cut the polygon into two parts. The antimeridian package does exactly that.
Since shapely
is not aware of the spherical nature of the data, the centroid
of this fixed polygon is still wrong.
The antimeridian
package also has a function to correct this.
Spherical Geometry
Another approach to handling the antimeridian issue is to perform all coordinate related calculations, such as polygon intersections, in a 3D spherical coordinate system.
One great library to do this is spherical_geometry. Here is an example.
Now you can easily compute intersections or check whether a certain point is inside the polygon.
You can compare the incorrect computation from shapely
with the fixed version when using spherical_geometry
.
Was this page helpful?