uber / h3-py

Python bindings for H3, a hierarchical hexagonal geospatial indexing system
https://uber.github.io/h3-py
Apache License 2.0
810 stars 130 forks source link

local_ij_to_cell error #387

Closed ischlo closed 1 month ago

ischlo commented 1 month ago

Hello, Unless I didn't understand what this function does, it is returning wrong values. I try to look at the ij neighbors of a cell to approximate a square shape, and the resulting set of cells are mapped in a completely different location, please find a reprex : I am using 'h3==4.0.0b3'.

import h3
import geopandas as gpd

res_h3 = 11

x = 51.51176 
y = -0.1227

h3_cell = h3.latlng_to_cell(lng=y,lat=x,res=res_h3)
h3_geo = h3.cells_to_h3shape([h3_cell])
h3_geo

gpd.GeoSeries([h3_geo],crs="EPSG:4326").explore() # should show a hex over Covent garden in London

# Getting the ij neighbors h3 ids.

i_range = range(-2,2)
j_range = range(-2,2)

coords = [(i,j) for i in i_range for j in j_range]
# coords
neighbs = [h3.local_ij_to_cell(origin=h3_cell,i=id[0],j=id[1]) for id in coords]
# neighbs
neighbs_geo = h3.cells_to_h3shape(neighbs)

# plots in the Atlantic Ocean
gpd.GeoSeries([h3_geo,neighbs_geo],crs="EPSG:4326").explore()

Instead of plotting the ij neighbors of the cell in London, it plots in the middle of the ocean. Thanks for your help. Best, Ivann

nrabinowitz commented 1 month ago

There is some confusion here - we could definitely be more explicit in the docs. The IJ coordinates are given in the same coordinate space as the origin, but they are not relative to the origin. So you need to get h3.cell_to_local_ij(origin, origin) to get the origin's IJ coords in that space, then use that as the coords for your IJ offsets.

untested:

origin_i, origin_j = h3.cell_to_local_ij(origin, origin)
neighbs = [h3.local_ij_to_cell(origin=h3_cell, i=origin_i + id[0], j=origin_j + id[1]) for id in coords]
ischlo commented 1 month ago

Hi Nick, Thanks for clarifying, Just to make sure, the function uses as origin the centre of the corresponding icosahedron in which the origin parameter cell lies ? Best, Ivann


De : Nick Rabinowitz @.> Envoyé : Friday, July 26, 2024 5:12:47 PM À : uber/h3-py @.> Cc : Schlosser, Ivann @.>; Author @.> Objet : Re: [uber/h3-py] local_ij_to_cell error (Issue #387)

There is some confusion here - we could definitely be more explicit in the docs. The IJ coordinates are given in the same coordinate space as the origin, but they are not relative to the origin. So you need to get h3.cell_to_local_ij(origin, origin) to get the origin's IJ coords in that space, then use that as the coords for your IJ offsets.

untested:

origin_i, origin_j = h3.cell_to_local_ij(origin, origin) neighbs = [h3.local_ij_to_cell(origin=h3_cell, i=origin_i + id[0], j=origin_j + id[1]) for id in coords]

— Reply to this email directly, view it on GitHubhttps://github.com/uber/h3-py/issues/387#issuecomment-2253075659, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANSJR2U3SOUXFWQHNJQCBLDZOJYP7AVCNFSM6AAAAABLQTS7ASVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENJTGA3TKNRVHE. You are receiving this because you authored the thread.Message ID: @.***>

nrabinowitz commented 1 month ago

Yes, that's correct, the IJ coordinates are centered on the face of the origin cell. But I'd recommend treating the coordinate system as opaque, i.e. only care about relative coordinates. It's still possible (but unlikely) that we could change the coordinate system under the hood, so it's better not to make assumptions in your code about where 0,0 is.