function featureToH3Set()

in src/geojson2h3.js [115:156]


function featureToH3Set(feature, resolution, options = {}) {
    const {type, geometry} = feature;
    const geometryType = geometry && geometry.type;

    if (type === FEATURE_COLLECTION) {
        // TODO: options are not passed here
        return featureCollectionToH3Set(feature, resolution);
    }

    if (type !== FEATURE) {
        throw new Error(`Unhandled type: ${type}`);
    }

    if (geometryType === POINT) {
        return [h3.latLngToCell(geometry.coordinates[1], geometry.coordinates[0], resolution)];
    }
    if (geometryType === MULTI_POINT) {
        return flatten(
            geometry.coordinates.map(point => [h3.latLngToCell(point[1], point[0], resolution)])
        );
    }

    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.polygonToCells(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.latLngToCell(lat, lng, resolution)];
        })
    );
}