uber / h3

Hexagonal hierarchical geospatial indexing system
https://h3geo.org
Apache License 2.0
4.8k stars 457 forks source link

API | distance between h3s challenging to work around #840

Closed fhk closed 3 months ago

fhk commented 3 months ago

It should be easy given two h3 indexes to calculate their distance from one another, either from centroid or perhaps nearest vertices.

When making models you often want to encode start and end edges and the distance.

This is now hard as there is no centroid method on the h3?

You have to go through the whole __geo_interface__ or hope that the outer ordering is consistent?

    edges = {}
    for h in data_center_locations['h3_3']:
        for d in demand_locations['h3_3']:
            edges[i_j_hex[d], i_j_hex[h]] = h3.great_circle_distance(d, h)
ajfriend commented 3 months ago

h3.cell_to_latlng gives the centroid of an H3 cell.

If you have H3 cell ids h and d, you can compute the Haversine distance between their centroids with:

h1 = h3.cell_to_latlng(h)
d1 = h3.cell_to_latlng(d)
distance = h3.great_circle_distance(d1, h1)

Does that cover your use case?

nrabinowitz commented 3 months ago

There's also gridDistance, which gives you the distance in grid steps between two cells. This might be appropriate for some modeling tasks.

fhk commented 3 months ago

ah @ajfriend apologies I couldn't locate this in the new docs!

fhk commented 3 months ago

https://h3geo.org/docs/library/migration-3.x/functions/ any idea what it is in 4.x?

dfellis commented 3 months ago

@fhk I think part of the confusion on your end is that you're using the documentation for the core C library but comparing it to the Python bindings. The Java and Javascript bindings stick really close to the core C library's naming system (with all three being camelCase), but the Python library deviates from that.

There's some effort to include Python in most of the pages, but that particular page does not include it. Because of this and some other ways the Python library deviates or adds extra features (such as the H3Shape type), there's a second set of documentation just for it that may be useful to you.