calculating geospatial indexes for h3 at any given resolution

I am just starting out with Uber's h3geo (JavaScript), and am getting half the number of indexes at every resolution than what the docs suggest.

const nw = [-180,  90];
const ne = [ 180,  90];
const se = [ 180, -90];
const sw = [-180, -90];
let rect = [nw, ne, se, sw];
const res = 0;
const grid = h3.polyfill(rect, res);
console.log(grid.length);
// res 0 output → 61, should output 122
// res 1 output → 421, should 842
// res 2 output → 2951, should 5882
// res 3 output → 20580; should output 41162
// and so on

What am I doing wrong?

Additionally, the h3geo docs for regions show lat first and lng second. As shown above, I am using [lng, lat]. If I flip them, I consistently get grid.length as 0. And if I give a geoJson, I get an error saying coordinates don't have a length property. What am I missing?


Answered in part here, but responding here as well for visibility:

  • h3.polyfill expects [lat, lng] pairs, unless you pass true for the third isGeoJson argument. So you are currently polyfilling the reverse coordinates, which is why you get the wrong number of cells.
  • The H3 polyfill function only works at present with shapes with width < 180 degrees of longitude. We ignore winding order and use the smaller shape where possible. If you divide the bounding box into two shapes, you should get expected results. This is why you get no results for the [lat, lng] input.
  • h3-js doesn't support full GeoJSON input, you need geojson2h3 to handle GeoJSON structures.

This is admittedly a documentation issue for H3, which we'll try to update.