Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: the geo_json_conformant argument of polygon_to_cells is missing in v4 #307

Open
Zeroto521 opened this issue Jan 14, 2023 · 5 comments

Comments

@Zeroto521
Copy link

uber/h3-py v3 polyfill has an argument called geo_json_conformant

def polyfill(self, geojson, res, geo_json_conformant=False):

But this argument is missing in uber/h3-py v4 polygon_to_cells

def polygon_to_cells(self, polygon, res):

image

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

image

@Zeroto521 Zeroto521 changed the title API: the geo_json_conformant argument of polygon_to_cells in v4 API: the geo_json_conformant argument of polygon_to_cells is missing in v4 Jan 14, 2023
@mohit-at-delhivery
Copy link

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)

@ajfriend
Copy link
Contributor

I'm working on the v4 interface here: #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

@mohit-at-delhivery
Copy link

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.

@kamilmarwat
Copy link

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.

@mohit-at-delhivery
Copy link

mohit-at-delhivery commented Dec 30, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants