UNPKG

wkt-parser-helper

Version:

Module to help parse GeoJSONs to WKT and back

38 lines (37 loc) 1.46 kB
import * as __WEBPACK_EXTERNAL_MODULE_betterknown__ from "betterknown"; function convertGeometryToWK(geojson) { return (0, __WEBPACK_EXTERNAL_MODULE_betterknown__.geoJSONToWkt)(geojson); } function convertFeatureToWK(geojson) { return convertGeometryToWK(geojson.geometry); } function convertFeatureCollection(featureCollection) { if ('FeatureCollection' !== featureCollection.type) throw new Error('GeoJSON is not a FeatureCollection'); return `GEOMETRYCOLLECTION(${featureCollection.features.map((d)=>convertFeatureToWK(d)).join(',')})`; } function convertToWK(geojson) { switch(geojson.type){ case 'Feature': return convertFeatureToWK(geojson); case 'FeatureCollection': return convertFeatureCollection(geojson); default: return convertGeometryToWK(geojson); } } function convertFeatureCollectionToWktCollection(geojson) { return geojson.features.map((d)=>({ wkt: convertGeometryToWK(d.geometry), ...d.properties })); } function parseFromWK(item, asFeature = false, properties = {}) { const parsed = (0, __WEBPACK_EXTERNAL_MODULE_betterknown__.wktToGeoJSON)(item); if (asFeature) return { type: 'Feature', geometry: parsed, properties }; return parsed; } export { convertFeatureCollection, convertFeatureCollectionToWktCollection, convertFeatureToWK, convertGeometryToWK, convertToWK, parseFromWK };