h3

measure of h3_distance function in python h3 geo version 3.x


I am new to the h3 geo module in python, I find myself really confused on some definition.

let's assume my resolution is set to 10 all the time

when I do h3.h3_distance(A, B), it returns me an number, but what measure is this number? I picked 2 random location from google map and using the following code

reso= 10
h3.geo_to_h3(41.282872, -84.093356, reso)
h3.geo_to_h3(41.284644, -83.794264, reso)
h3.h3_distance("8a2a9458c807fff", "8a2a94cdb6d7fff")

those 2 location from map are 15.5 miles away and almost perfect straight line. the return I get from the above code is 200, how does this 200 number translate to 15.5 miles?

only thing I can find from h3 website is the edge length table(https://h3geo.org/docs/core-library/restable/) for resolution 10, average edge length is 0.075863783km, so 200 * 0.075863783 = 15.17 which looks close, but it is in km not miles!

Does anyone understand what is wrong with my logic?

Thanks very much!


Solution

  • The h3_distance returns a distance in hexes between those two locations, that's right. But it's not in a straight line but rather created by connecting hexagons laying on the line by edges. It's true that it's equal to 200.

    To visualize it:

    import pydeck
    import pandas as pd
    import h3
    
    start = '8a2a9458c807fff'
    end = '8a2a94cdb6d7fff'
    
    path = h3.grid_path_cells(start, end)
    
    df = pd.DataFrame(data=dict(h3=path))
    
    layer = pydeck.Layer(
        "H3HexagonLayer",
        df,
        pickable=True,
        stroked=True,
        filled=True,
        extruded=False,
        get_hexagon="h3",
        get_line_color=[255, 255, 255],
        line_width_min_pixels=1,
    )
    view_state = pydeck.ViewState(latitude=41.282872, longitude=-84.093356, zoom=14)
    r = pydeck.Deck(layers=[layer], initial_view_state=view_state)
    r
    

    H3 Line between points

    If we were to do some simple math using hexagon calculations, we certainly cannot just add single edges together, but rather short diagonals (equal to sqrt(3) * edge length), which would equal 131.3m * 200 = 26.26 km = 16.317 miles. Much closer to the real 15.5 miles, but those h3 cells aren't in a straight line, but rather a little bit offset.

    Hexagon distances calclations

    Remember that H3 hexagonal grid system doesn't calculate hexagons on the fly for each location, but it's fixed for the whole globe, that's why we cannot translate between the H3 hexagonal distance and real straight line distance, that would be more of an approximation. H3 API contains great circle distance though if you need to calculate it on the fly.