1

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?

Dakshin
  • 437
  • 1
  • 6
  • 16

4 Answers4

3

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))]
user3496060
  • 800
  • 10
  • 20
0

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.

SebaGM
  • 1
0

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

0

You might consider using geopandas.GeoDataFrame.explode to convert the multipolygons to polygons.

After that, you can probably work out the rest (and do a more elegant job!), but I did this:

hexagons = []
for index, row in geofence.iterrows():
  hexagons.extend(h3.polyfill(row['geometry'].__geo_interface__, h3resolution, geo_json_conformant=True))
Tim Harding
  • 116
  • 1
  • 4