javascripth3

H3 - How to get the H3 index of all cells that are at least partially inside a bounding box/polygon


I would like to get the h3 cell index of every cell that is at least partially within a bounding box. I have been using h3.polygonToCells() but it appears that h3.polygonToCells() only returns cells that are mostly within the bounding box (polygon).

This how the coverage looks over central London, with resolution 3:

const coords = [
    [51.55, -0.01],
    [51.45, -0.01],
    [51.45, -0.16],
    [51.55, -0.16],
];

let RESOLUTION_LEVEL = 3;
const cells = h3.polygonToCells(coords, RESOLUTION_LEVEL, false);

console.log("cells", cells); // returns an array of 0 items

picture showing map with H3 resolution 7 - no cells returned

And now the same area, with resolution 7:

RESOLUTION_LEVEL = 7;
const cells = h3.polygonToCells(coords, RESOLUTION_LEVEL, false);

console.log("cells", cells); // returns an array of 26 items

picture showing map with H3 resolution 3 - many cells returned, all with a majority of the cell inside the bounding box

How do I get all the H3 cell indexes for the whole bounding box?


Solution

  • You probably want to use the h3shape_to_cells_experimental function instead, with the overlap flag.

    https://h3geo.org/docs/api/regions#polygontocellsexperimental