ENH: Implement a "finer `polyfill`"
A common use-case is to generate all hexagons that intersect with a given polygon. That's not straightforward to do using H3.
One approximate solution is to first generate a finer resolution grid using polyfill and then return to the desired resolution using h3_to_parent.
The H3-Pandas API could thus contain a convenience function that performs both of these operations in a single pass.
A quick demonstration of the idea:
import geopandas as gpd
import h3pandas
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Resample to H3 cells
gdf = gdf.h3.polyfill_resample(4)
gdf = gdf.h3.h3_to_parent_aggregate(2)
Sidenote
The above code yields a "strange" result:

This is because a number of the hexagons cross the anti-meridian, and get rendered wrongly. Since this case might be reasonably common, H3-Pandas could perhaps provide a fix, something like
import numpy as np
from shapely.geometry import Polygon
def fix(cell):
cell_coords = np.array(cell.boundary.coords)
lngs = cell_coords[:, 0]
if not (np.any(lngs < 0) and np.any(lngs > 0)) or not (np.any(np.abs(lngs) > 170)):
return cell
negative = lngs < 0
lngs[negative] = lngs[negative] + 360
return Polygon(cell_coords)
Applying the fix to the dataframe's geometries
gdf['geometry'] = gdf['geometry'].apply(fix)
fixes the visual output

Please can you help. For which object to use the fix function? when i use it for gdf, i get an error AttributeError: 'GeoSeries' object has no attribute 'coords'
Please can you help. For which object to use the fix function? when i use it for gdf, i get an error AttributeError: 'GeoSeries' object has no attribute 'coords'
The fix must be applied to the geometries themselves. I have added the relevant line to my post above.
Hi @DahnJ
Long time on this issue but..
I've modified polyfill with the idea suggested above (not sure if you were planning on making changes).
This solution below does two things:
- If a given polygon is too small (returns no h3 cells), check iteratively with higher resolutions and return the parent cells at the res requested.
- Add option for overfill returning also the outer edge cases using h3.k_ring.
I can submit a PR if you think this could be a useful change!
https://github.com/andresfchamorro/H3-Pandas/commit/c4a7852965331a7de7c1d7b8a29499ab6c3b0fec