h3-py
h3-py copied to clipboard
API: the `geo_json_conformant` argument of `polygon_to_cells` is missing in v4
uber/h3-py v3 polyfill
has an argument called geo_json_conformant
https://github.com/uber/h3-py/blob/f8958ac788ea04e2b383c8b859f75a05c3fcb815/src/h3/api/_api_template.py#L495
But this argument is missing in uber/h3-py v4 polygon_to_cells
https://github.com/uber/h3-py/blob/3eece401ba311feeba99073eb65db2764ef85557/src/h3/api/_api_template.py#L400
But the document of uber/h3 v4 shows that geo_json_conformant
is still in polygon_to_cells
.
Any updates on this? Also how can we get all h3 cells within a given polygon? Is it necessary to convert a normal polygon into an h3 polygon? if so an example would be really appreciated. I tried the following but it gives the error "Error: object of type 'Polygon' has no len()".
import h3 from shapely.geometry Polygon my_polygon = Polygon([(1, 0), (1, 1), (2, 1), (2, 0)]) h3.Polygon(my_polygon)
I'm working on the v4 interface here: https://github.com/uber/h3-py/pull/301
The major change is that we'll have functions like h3.geo_to_cells(geo, res)
that can take in any object that implements a __geo_interface__
, which should make it automatically compatible with many existing libraries like geopandas
I am using v4 and I want to know the h3 cells contained within a shapely polygon. Currently how can we achieve this in v4, is there any work around for this? Please let me know.
I am using v4 and I want to know the h3 cells contained within a shapely polygon. Currently how can we achieve this in v4, is there any work around for this? Please let me know.
@mohit-at-delhivery by first creating a DataFrame with the h3-indexes given that the poly is a shapely Polygon and CRS is 4326
h3_filled_polygon = gpd.GeoDataFrame(
data = list (
h3.polygon_to_cells(
h3.Polygon(poly.exterior.coords),
res= 5,
)),
columns = ['h3']
)
Then create the polygon geometry
h3_filled_polygon['geometry']=h3_filled_polygon['h3'].apply(lambda x: shapely.Polygon(h3.cell_to_boundary(x)))
h3_filled_polygon = h3_filled_polygon.set_crs("4326")
but the problem with v4 is I am getting wrong shapes of the hex polygons. They are skewed, even when the projection is correct. So can't guarantee that.
Hi @kamilmarwat .. the coordinates might be reversed if the grids are skewed?
Here is a solution that I found which is similar to yours
import h3
from shapely.wkt import loads
import folium
polygon_wkt = 'POLYGON((28.60651289028247 77.18724444355178, 28.614630596755424 77.21696054040207, 28.619245246847916 77.20519410779309, 28.622500481098306 77.1974097750062, 28.614460198648654 77.18849247015262, 28.60651289028247 77.18724444355178))'
# Convert Shapely polygon to h3.Polygon
shapely_polygon = loads(polygon_wkt)
# Get the exterior coordinates of the Shapely polygon
exterior_coords = list(shapely_polygon.exterior.coords)
# Convert the coordinates to H3 indexes
h3_indexes = [h3.latlng_to_cell(lat, lng, res=9) for lat, lng in exterior_coords]
# Convert H3 indexes back to coordinates
h3_coordinates = [h3.cell_to_latlng(h3_index) for h3_index in h3_indexes]
# Create an H3.Polygon from the H3 coordinates
h3_polygon = h3.Polygon(h3_coordinates)
cells = list(h3.polygon_to_cells(h3_polygon, res=9))
### Plotting the output
folium_map = folium.Map(location= [28.60651289028247, 77.18724444355178], zoom_start=14, control_scale=True)
folium.Polygon(loads(polygon_wkt).exterior.coords, color='red').add_to(folium_map)
for cell in cells:
h3_polygons = h3.cells_to_polygons([cell])
folium.Polygon(locations=h3_polygons[0].outer,
color='blue').add_to(folium_map)
folium_map
Also you can take one/two hop neighbour grids of all h3 grids in the "cells" variable as well just to make sure that no area of the polygon is left without an h3 grid.