nmandery / h3ronpy

A data science toolkit for the H3 geospatial grid
74 stars 8 forks source link

cells_to_wkb_points returning longitude for both coordinates #58

Closed zacharyDez closed 2 weeks ago

zacharyDez commented 2 weeks ago

Hi! I've been migrating a project to h3ronpy but I've been running into an issue returning the centroids of hexagons. It seems that the coordinates returned have exactly the same value:

import h3ronpy
from shapely.geometry import Point
from h3ronpy.arrow.vector import geometry_to_cells, cells_to_wkb_points
import pyarrow as pa
from shapely import wkb

# Step 1: Create a point (latitude and longitude)
lat, lon = 37.7749, -122.4194  # Example coordinates (San Francisco)
point = Point(lon, lat)  # shapely expects (longitude, latitude)

# Step 2: Convert the point to an H3 cell (resolution 9 for example)
resolution = 9
h3_cells = geometry_to_cells(point, resolution)

# Step 3: Convert the H3 cell back to WKB points
wkb_points = cells_to_wkb_points(h3_cells)

# Step 4: Decode the WKB point to a Shapely geometry
for wkb_point in wkb_points:
    if isinstance(wkb_point, pa.Scalar):  # Ensure it's a pyarrow Scalar
        shapely_point = wkb.loads(wkb_point.as_buffer().to_pybytes())
        print(f"Shapely Geometry: {shapely_point}")
    else:
        print(f"Unexpected Type: {type(wkb_point)}")

Returns:

Shapely Geometry: POINT (-122.41827103692466 -122.41827103692466)

I'd be happy to help with the fix!

nmandery commented 2 weeks ago

Hi. Thanks for bringing this up (and the offer to help) - this is definitely a bug. I just checked and it happens with geopandas as well. I will look into this.

Seems having more tests would have helped to catch this earlier ;)

nmandery commented 2 weeks ago

The fix will be in v0.21.1 and is currently build on GH actions