'How to polyfill a Multi-polygon in H3?
I have a set of multipolygons in GeoJSON format, for example:
{
"type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
]
}
I'm trying to use the polyfill_geojson method in the H3 library to get the hexagons that fall within it. But it seems like this method only supports Polygons and not multipolygons:
>>> h3.polyfill_geojson(geojson, 8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "python3.7/site-packages/h3/api/_api_template.py", line 486, in polyfill_geojson
mv = _cy.polyfill_geojson(geojson, res)
File "geo.pyx", line 186, in h3._cy.geo.polyfill_geojson
ValueError: Only Polygon GeoJSON supported
How can I get the H3 hexagons from a Multipolygon GeoJSON?
Solution 1:[1]
Easy....this gives you a list of sets (which you can easily join).
[h3.polyfill(shapely.geometry.mapping(x)) for x in list(shapely.geometry.loads(geojson))]
Solution 2:[2]
Apparently, there is no support yet for multipolygons on an h3.polyfill like function.
I suggest you to programmatically separate the polygons from the multipolygon, and to evaluate the polyfill function over each one of these single polygons.
Or, if you are working on a small number of polygons, use the https://www.keene.edu/campus/maps/tool/ tool to create a polygon that matches the multipolygon.
Sorry if this does not help too much.
Solution 3:[3]
The function that might help here is mosaicfill from Mosaic library which is an extension to the Apache Spark framework that allows easy and fast processing of very large geospatial datasets https://databrickslabs.github.io/mosaic/
https://databrickslabs.github.io/mosaic/api/spatial-functions.html?highlight=mosaic_fill#mosaicfill
Unlike the native H3 implementation, Mosaic wrapper function does handle multi geometries
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user3496060 |
| Solution 2 | SebaGM |
| Solution 3 |
