uber / h3

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

Docs request - Which function(s) to use to convert Level 11 index to Level 8? #649

Closed gigatexal closed 2 years ago

gigatexal commented 2 years ago

Hi folks,

Unsure if this is even the right place to ask or if this even makes sense -- it seems like it might.

We've a data source that is sending level 11 indexes and we'd like to convert them to level 8 (larger areas from the "higher resolution" level 11 index). But I am unsure how to do this using the functions provided in the API.

-Alex

isaacbrodsky commented 2 years ago

This can be done using the cellToParent (h3ToParent in v3) function. This is a very efficient function as it only does bitwise operations on the index. Since hexagons don't cleanly subdivide, there will be areas where the original point would index into a different cell at the coarser resolution.

dfellis commented 2 years ago

This can be done using the cellToParent (h3ToParent in v3) function. This is a very efficient function as it only does bitwise operations on the index. Since hexagons don't cleanly subdivide, there will be areas where the original point would index into a different cell at the coarser resolution.

To expand on this, @nrabinowitz has a very nice interactive diagram of the hierarchical relationship that you can play with. While these are different resolutions, the delta between them is the same so the abstract geometry would look nearly identical to this:

Screenshot from 2022-08-23 10-41-58

The vast majority of the cells overlap, but a few, due to the partial non-containment visible on the first parent-to-child level, as shown below, causes containment "drift" as you go to smaller and smaller resolutions, in a fractal-like pattern.

Screenshot from 2022-08-23 10-44-28

A more computationally expensive operation can be done to establish containment by hexagon centerpoint. Simply calling cellToLatLng (aka h3ToGeo in v3) to get the center coordinates of the res 11 cells and then feeding that into latLngToCell (geoToH3 in v3) at resolution 8. This will have the data assigned according to the geographic containment rather than the hierarchical containment, but is multiple orders of magnitude slower.

gigatexal commented 2 years ago

Wonderful answers. Thank you both!