metno / MetAlert-Search

Polygon search and indexing package for Annotated Atlas
Apache License 2.0
1 stars 0 forks source link

Select polygon search tool #2

Closed vkbo closed 3 years ago

vkbo commented 3 years ago

We need to select an appropriate tool that can handle the polygon intersect functionality and which can be run from Python.

fzeiser commented 3 years ago

ne possibility:

It is based on the widely deployed GEOS (the engine of PostGIS) and JTS (from which GEOS is ported) libraries. Shapely is not concerned with data formats or coordinate systems, but can be readily integrated with packages that are.

Simple method to get the overlap ratio, e.g.:
poly1.intersection(poly2).area / poly1.area [criticism: might be slower then "only" calculating the overlap ratio]

Requirements:

fzeiser commented 3 years ago

Interesting issue here with many suggestions: https://github.com/astropy/regions/issues/46

I see Boost also has some polygon stuff: https://www.boost.org/doc/libs/1_70_0/libs/polygon/doc/index.htm

But again if you look at the spherical polygon code - it's very complex (like in the Google S2 code): https://github.com/boostorg/geometry/blob/develop/include/boost/geometry/strategies/spherical/point_in_poly_winding.hpp

vkbo commented 3 years ago

Shapely seems to be built on a port of JTS, which is the package I was suggesting. So in principle Shapely and JTS should be equivalent.

There's also a package for SolR called Geo3D that works on ellipsoid geometry in addition to Cartesian, but I don't think the polygon areas will be large enough that this is needed. Something to discuss anyway.

fzeiser commented 3 years ago

Used shapely on the kartverket data of #10 to get a feeling (not an exact test) on the timings etc.

Test-file: (ipynb) https://app.zenhub.com/files/376049307/46aa6315-ccc7-4619-ae9f-af36b50de9c4/download

fzeiser commented 3 years ago

There's also a package for SolR called Geo3D that works on ellipsoid geometry in addition to Cartesian, but I don't think the polygon areas will be large enough that this is needed. Something to discuss anyway.

I think we can drop ellipsoid/spherical geometry in the first implementation and potentially open for this later.

fzeiser commented 3 years ago

Update on the timings: As expected, the calculation of the intersection polygon is dependent on the number of vertices. Shapely/GEOS has a simplify method with a given tolerance.

A "slight" simplification (tolerance parameter = 800), will produce following polygons (solid line, vs dashed). The upper map is of Malvik, the lower one is of Trøndelag. The gives a speedup of a factor 10, whilst it is difficult to imagine that this should have a practical impact. (Due to the resolution / scaling, the simplification is not visible by eye on this scale for the Trøndelag picture.) index.png

vkbo commented 3 years ago

Closing this since we have definitely decided to go with shapely. :)