2

I'm trying to draw a hex grid over a world map. The latter can be done easily enough with Geopandas:

import geopandas
import matplotlib.pyplot as plt

world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
world.plot()
plt.savefig("world.png")
plt.show()

For the former, I get the impression H3 should be able to do it. I've got H3 installed, along with the Python bindings for it, and can now import h3, but... well, I'm currently at what might be the 'step 2: draw the rest of the effing owl' stage, or might be just one or two lines of code that I'm missing. Searching for H3 documentation and tutorials, finds plenty of discussion on the merits of hexagons, on how to do all sorts of subtle calculations with H3, but I have not yet found anything that points out how to draw the actual hex grid.

So: how to use H3 to draw a hex grid over a Geopandas map? (For that matter, is H3 indeed the best tool for this job?)

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 3
    The following [pages](https://geographicdata.science/book/data/h3_grid/build_sd_h3_grid.html) may be useful. For your reference. – r-beginners Jun 28 '21 at 14:42
  • 1
    You might also consider [H3-Pandas](https://github.com/DahnJ/H3-Pandas) to conveniently work with H3 hexagons and plot them. See e.g. [this answer](https://stackoverflow.com/a/68137170/1785661) that actually uses the same dataset. Though if you try to plot hexagons crossing the antimeridian, you might have to consider [this problem](https://stackoverflow.com/a/67814476/1785661). – Dahn Jun 29 '21 at 00:52

1 Answers1

3

is H3 indeed the best tool for this job?

That depends what you want a hex grid for. The advantage of H3 over "standard" hex grids is that most hex grids are regular hexagons drawn over a projected map, with the result that the geographic area of each hex cell varies widely depending on the projection and location of the cell on the map. This can still be sufficient for visualization, and it may be faster and easier to reason about than H3.

H3 uses a global grid where all of the cells are equal area (roughly - there's variation of about 2x across the globe; see this map and this table). This may give you better results depending on your use case.

To draw the entire grid at a particular resolution, you can get all of the base cells with h3.get_res0_indexes and then expand them to the desired resolution using h3.h3_to_children, e.g.

[cell for base_cell in h3.get_res0_indexes() 
  for cell in h3.h3_to_children(base_cell, res)]

Then you can use h3.h3_to_geo_boundary to get the bounds of each cell.

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165