in h3_polyfill.c [732:781]
H3Error H3_EXPORT(maxPolygonToCellsSizeExperimental)(const GeoPolygon *polygon,
int res, uint32_t flags,
int64_t *out) {
// Special case: 0-vertex polygon
if (polygon->geoloop.numVerts == 0) {
*out = 0;
return E_SUCCESS;
}
// Initialize the iterator without stepping, so we can adjust the res and
// flags (after they are validated by the initialization) before we start
IterCellsPolygonCompact iter = _iterInitPolygonCompact(polygon, res, flags);
if (iter.error) {
return iter.error;
}
// Ignore the requested flags and use the faster overlapping-bbox mode
iter._flags = CONTAINMENT_OVERLAPPING_BBOX;
// Get a (very) rough area of the polygon bounding box
BBox *polygonBBox = &iter._bboxes[0];
double polygonBBoxAreaKm2 =
bboxHeightRads(polygonBBox) * bboxWidthRads(polygonBBox) /
cos(fmin(fabs(polygonBBox->north), fabs(polygonBBox->south))) *
EARTH_RADIUS_KM * EARTH_RADIUS_KM;
// Determine the res for the size estimate, based on a (very) rough estimate
// of the number of cells at various resolutions that would fit in the
// polygon. All we need here is a general order of magnitude.
while (iter._res > 0 &&
polygonBBoxAreaKm2 / getAverageCellArea(iter._res - 1) >
MAX_SIZE_CELL_THRESHOLD) {
iter._res--;
}
// Now run the polyfill, counting the output in the target res.
// We have to take the first step outside the loop, to get the first
// valid output cell
iterStepPolygonCompact(&iter);
*out = 0;
int64_t childrenSize;
for (; iter.cell; iterStepPolygonCompact(&iter)) {
H3_EXPORT(cellToChildrenSize)(iter.cell, res, &childrenSize);
*out += childrenSize;
}
return iter.error;
}