geometrygeopandash3

Converting an LineString to h3 hexagons using srai


I try to convert an shapely LineString to a sequence of h3 hexagons. For that task srai offers the funciton h3_to_geoseries unfortunately the function runs into the following error message:

AttributeError: 'MultiPolygon' object has no attribute 'exterior'

Could you give some advice how I can avoid this problem?


Solution

  • I'm one of the developers of the SRAI library. Sorry for the late reply, but here is the code that I can suggest you to transform linestrings into H3 cells.

    from shapely import from_geojson
    import h3
    from srai.h3 import h3_to_geoseries
    import geopandas as gpd
    
    # ls = linestring object from shapely
    
    H3_RESOLUTION = 12
    h3_cells = []
    points = ls.coords
    for idx in range(len(points) - 1):
        a, b = points[idx], points[idx + 1]
        start_hex = h3.latlng_to_cell(a[1], a[0], H3_RESOLUTION)
        end_hex = h3.latlng_to_cell(b[1], b[0], H3_RESOLUTION)
        for h3_cell in h3.grid_path_cells(start_hex, end_hex):
            if not h3_cells or h3_cells[-1] != h3_cell:
                h3_cells.append(h3_cell)
    

    enter image description here

    If you have any more issues with the SRAI library I'd suggest adding a new issue or discussion on the GitHub repository (I didn't expect anyone to add questions to the stack overflow :P)