in h3_algos.c [775:805]
H3Error H3_EXPORT(maxPolygonToCellsSize)(const GeoPolygon *geoPolygon, int res,
uint32_t flags, int64_t *out) {
H3Error flagErr = validatePolygonFlags(flags);
if (flagErr) {
return flagErr;
}
// Get the bounding box for the GeoJSON-like struct
BBox bbox;
const GeoLoop geoloop = geoPolygon->geoloop;
bboxFromGeoLoop(&geoloop, &bbox);
int64_t numHexagons;
H3Error estimateErr = bboxHexEstimate(&bbox, res, &numHexagons);
if (estimateErr) {
return estimateErr;
}
// This algorithm assumes that the number of vertices is usually less than
// the number of hexagons, but when it's wrong, this will keep it from
// failing
int totalVerts = geoloop.numVerts;
for (int i = 0; i < geoPolygon->numHoles; i++) {
totalVerts += geoPolygon->holes[i].numVerts;
}
if (numHexagons < totalVerts) numHexagons = totalVerts;
// When the polygon is very small, near an icosahedron edge and is an odd
// resolution, the line tracing needs an extra buffer than the estimator
// function provides (but beefing that up to cover causes most situations to
// overallocate memory)
numHexagons += POLYGON_TO_CELLS_BUFFER;
*out = numHexagons;
return E_SUCCESS;
}