Giter Site home page Giter Site logo

Comments (2)

ajfriend avatar ajfriend commented on August 20, 2024 1

While the interface might not be as integrated as h3pandas, I did recently put up an example notebook that does exactly this with the new v4.0 beta of the plain h3-py library: https://uber.github.io/h3-py/polygon_tutorial.html

from h3-pandas.

CarlaFernandez avatar CarlaFernandez commented on August 20, 2024

So I managed to get it working for one polygon by changing my function like so:

def generate_grid(region_bounds: gpd.GeoDataFrame, resolution=9) -> pd.DataFrame:
    grid = region_bounds.h3.polyfill(resolution, explode=True)

    grid = grid.set_index('h3_polyfill')
    grid = grid.h3.h3_to_geo_boundary()

    return grid

If I input more than one polygon, now the error is different:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/h3pandas/util/decorator.py:27, in catch_invalid_h3_address.<locals>.safe_f(*args, **kwargs)
     26 try:
---> 27     return f(*args, **kwargs)
     28 except (TypeError, ValueError, H3CellError) as e:

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/h3/api/_api_template.py:294, in _API_FUNCTIONS.h3_to_geo_boundary(self, h, geo_json)
    277 """
    278 Return tuple of lat/lng pairs describing the cell boundary.
    279 
   (...)
    292 tuple of (float, float) tuples
    293 """
--> 294 return _cy.cell_boundary(self._in_scalar(h), geo_json)

TypeError: Argument 'h' has incorrect type (expected str, got float)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In[24], line 14
     10     grid = grid.h3.h3_to_geo_boundary()
     12     return grid
---> 14 grid_9 = generate_grid(pp_cs_gdf[:2], 9)

Cell In[24], line 10, in generate_grid(region_bounds, resolution)
      8 grid = grid.set_index('h3_polyfill')
      9 display(grid)
---> 10 grid = grid.h3.h3_to_geo_boundary()
     12 return grid

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/h3pandas/h3pandas.py:160, in H3Accessor.h3_to_geo_boundary(self)
    139 def h3_to_geo_boundary(self) -> GeoDataFrame:
    140     """Add `geometry` with H3 hexagons to the DataFrame. Assumes H3 index.
    141 
    142     Returns
   (...)
    158     881e2659c3fffff    1  POLYGON ((14.99201 51.00565, 14.98973 51.00133...
    159     """
--> 160     return self._apply_index_assign(
    161         wrapped_partial(h3.h3_to_geo_boundary, geo_json=True),
    162         "geometry",
    163         lambda x: shapely.geometry.Polygon(x),
    164         lambda x: gpd.GeoDataFrame(x, crs="epsg:4326"),
    165     )

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/h3pandas/h3pandas.py:836, in H3Accessor._apply_index_assign(self, func, column_name, processor, finalizer)
    817 """Helper method. Applies `func` to index and assigns the result to `column`.
    818 
    819 Parameters
   (...)
    833 If using `finalizer`, can return anything the `finalizer` returns.
    834 """
    835 func = catch_invalid_h3_address(func)
--> 836 result = [processor(func(h3address)) for h3address in self._df.index]
    837 assign_args = {column_name: result}
    838 return finalizer(self._df.assign(**assign_args))

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/h3pandas/h3pandas.py:836, in <listcomp>(.0)
    817 """Helper method. Applies `func` to index and assigns the result to `column`.
    818 
    819 Parameters
   (...)
    833 If using `finalizer`, can return anything the `finalizer` returns.
    834 """
    835 func = catch_invalid_h3_address(func)
--> 836 result = [processor(func(h3address)) for h3address in self._df.index]
    837 assign_args = {column_name: result}
    838 return finalizer(self._df.assign(**assign_args))

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/h3pandas/util/decorator.py:32, in catch_invalid_h3_address.<locals>.safe_f(*args, **kwargs)
     30 message += f"\nCaller: {f.__name__}({_print_signature(*args, **kwargs)})"
     31 message += f"\nOriginal error: {repr(e)}"
---> 32 raise ValueError(message)

ValueError: H3 method raised an error. Is the H3 address correct?
Caller: h3_to_geo_boundary(nan)
Original error: TypeError("Argument 'h' has incorrect type (expected str, got float)")

When I display the grid, the h3_polyfill result contains NaN, and that seems to be the cause:
image

Any idea why this NaN would appear?

from h3-pandas.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.