Can Uber h3 cover the entire globe in hexagonal grid?

1.7k Views Asked by At

Uber has released h3, a framework for efficiently handling big data in the geospatial file. Using h3, I attempted to get the location of a hexagonal grid location as shown in the figure. (https://eng.uber.com/h3/)

I got the location of the hexagonal grid from the following code. Then I plotted it on a two-dimensional map to see if it covered the entire earth. However, I'm not getting valid hexagons on the boundaries(-90°,90°,-180°,180°). And it doesn't seem to cover the entire globe. (hexagonal grid)

    from h3 import h3
    import folium
    
    # Polyfill a Geo Json with hexagons
    geoJson1 = {'type': 'Polygon', 'coordinates': [[[90,-180],[90,0],[-90,0],[-90,-180]]]}
    geoJson2 = {'type': 'Polygon', 'coordinates': [[[90,0],[90,180],[-90,180],[-90,0]]]}
    hexagons = list(h3.polyfill(geoJson1, 1)) + list(h3.polyfill(geoJson2, 1))
    # Plot hexagons
    polylines = []
    for hex in hexagons:
        polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)
        outlines = [loop for polygon in polygons for loop in polygon]
        polyline = [outline + [outline[0]] for outline in outlines][0]
        polylines.append(polyline)
    base = folium.Map([0,0], zoom_start=2, tiles='cartodbpositron')
    for polyline in polylines:
        m = folium.PolyLine(locations=polyline, weight=1, color='black')
        base.add_child(m)
    m.save('test.html')

I want the position of hexagon that covers the entire globe. In this case, I approached using h3, but I don't care how as long as I can get the position.

2

There are 2 best solutions below

0
On BEST ANSWER

Yes, H3 covers the entire globe. What you're seeing in that image are rendering artifacts - depending on how you render a global grid in a flat projection, you may get similar artifacts at the poles or across the antimeridian. See e.g. this map for a projection of H3 that renders correctly across the antimeridian, though it still has some issues around the poles.

0
On

When you fill whole globe, you get H3 polygons that cross anti-meridian. These will have vertices both in East hemisphere (longitude > 0) and West hemisphere (longitude < 0). You can find such polygons using e.g.

for line in polylines:
  if any(p[1] > 0 for p in line) and any(p[1] < 0 for p in line):
    print(line)

Consider e.g. one edge from this list:

(-65.7243888731199, -176.62192487031285), (-66.42506103952591, 175.12130159942038)

It spans from longitude -176 to 173. H3 assumes the vertices are connected with geodesic (shortest) path, i.e. in this case a short path that cross antimeridian. The polygon thus should appear both at the left-most and right-most sides of the map. But when you give it to a planar-map drawing tool like Folium, it does not know anything about geodesic edges or antimeridian, it instead draws a long straight line across whole globe (shortest line on the map, rather than shortest line on the globe), crossing primary meridian. This is the source of all the long near-horizontal lines you have here.

There are several solutions for this, depending on what you want. Common choice is to fix each polygon that crosses antimeridian, to draw it either at the left or right side. Say you move all such polygons right - then add 360 to all negative longitudes, replacing -176 with 184. You'll get a small polygon that Folium should be able to draw "correctly", although it will stick out to the right of 180 meridian (I've no experience with Folium though, but this is the typical behavior).