in src/geojson2h3.js [112:142]
function featureToH3Set(feature, resolution, options = {}) {
const {type, geometry} = feature;
const geometryType = geometry && geometry.type;
if (type === FEATURE_COLLECTION) {
return featureCollectionToH3Set(feature, resolution);
}
if (type !== FEATURE) {
throw new Error(`Unhandled type: ${type}`);
}
if (geometryType !== POLYGON && geometryType !== MULTI_POLYGON) {
throw new Error(`Unhandled geometry type: ${geometryType}`);
}
// Normalize to MultiPolygon
const polygons = geometryType === POLYGON ? [geometry.coordinates] : geometry.coordinates;
// Polyfill each polygon and flatten the results
return flatten(
polygons.map(polygon => {
const result = h3.polyfill(polygon, resolution, true);
if (result.length || !options.ensureOutput) {
return result;
}
// If we got no results, index the centroid
const [lng, lat] = centroid(polygon);
return [h3.geoToH3(lat, lng, resolution)];
})
);
}