I have been struggling with polygons in python 3 for a long time. There is a geojson with a list of polygons and multipolygons. It is necessary to cover these polygons with hexes of a CERTAIN level. At the moment, hexagons are level 5, but there may be others. If I now cross polygons with level 5 hexes, then some polygons do not find hexagon at all. When I began to debug the output, I concluded that a hex is issued only if the polygon area covers 50% of the hex area. And how to do the same, only to set the degree of overlap yourself? I want to be able to get all the hexes where at least 1 pixel is occupied by a polygon. Ideally, you want to manually set the overlap area.
Now I get hexagons by polygons with this function:
def hex_id(df_polygon, resolution):
global h3_id_df
global msc
list_set_h3_id = []
list_h3_id = []
gdf = geopandas.GeoDataFrame(df_polygon, geometry = 'geometry')
exploded = gdf.explode()
polygons_list = exploded['geometry'].astype('str').tolist()
for i in polygons_list:
msc = geojson.Feature(geometry = shapely.wkt.loads(i), properties={})
list_set_h3_id.append(h3.polyfill_geojson(msc.geometry, resolution))
df_polygon['geo_id'] = list_set_h3_id
for i in list_set_h3_id:
for i in (list(i)):
list_h3_id.append(i)
h3_id_df = pd.DataFrame({'geo_id':list_h3_id})
return h3_id_df