h3-py icon indicating copy to clipboard operation
h3-py copied to clipboard

API: the `geo_json_conformant` argument of `polygon_to_cells` is missing in v4

Open Zeroto521 opened this issue 2 years ago • 5 comments

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 image

But the document of uber/h3 v4 shows that geo_json_conformant is still in polygon_to_cells.

image

Zeroto521 avatar Jan 14 '23 02:01 Zeroto521

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)

mohit-at-delhivery avatar Oct 30 '23 02:10 mohit-at-delhivery

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

ajfriend avatar Nov 13 '23 01:11 ajfriend

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 avatar Dec 02 '23 08:12 mohit-at-delhivery

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.

kamilmarwat avatar Dec 30 '23 19:12 kamilmarwat

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.

mohit-at-delhivery avatar Dec 30 '23 20:12 mohit-at-delhivery