UNPKG

geobuf-merge

Version:

Merge multiple geobuf-encoded geometries together

51 lines (47 loc) 1.13 kB
// src/index.ts import Pbf2 from "pbf"; import { encode } from "geobuf"; // src/helpers/normalize.ts import { decode } from "geobuf"; import Pbf from "pbf"; function normalizePbf(pbf) { const decoded = decode(new Pbf(pbf)); if (decoded.type === "FeatureCollection") { return decoded; } if (decoded.type === "GeometryCollection") { return { type: "FeatureCollection", features: decoded.geometries.map((geometry) => ({ type: "Feature", geometry, properties: {} })) }; } return { type: "FeatureCollection", features: [ decoded.type === "Feature" ? decoded : { type: "Feature", geometry: decoded, properties: {} } ] }; } // src/helpers/merge.ts function mergeFeatureCollections(featureCollections) { return { type: "FeatureCollection", features: featureCollections.flatMap((d) => d.features) }; } // src/index.ts function mergeGeobufs(geobufs) { const decoded = geobufs.map((geobuf) => normalizePbf(geobuf)); return encode(mergeFeatureCollections(decoded), new Pbf2()); } export { mergeGeobufs };