UNPKG

wkt-parser-helper

Version:

Module to help parse GeoJSONs to WKT and back

143 lines (142 loc) 5.34 kB
"use strict"; var __webpack_require__ = {}; (()=>{ __webpack_require__.d = (exports1, definition)=>{ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, get: definition[key] }); }; })(); (()=>{ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); })(); (()=>{ __webpack_require__.r = (exports1)=>{ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { geometryToWkt: ()=>geometryToWkt, wkt3dTo2d: ()=>wkt3dTo2d, wktToGeojson: ()=>wktToGeojson, geojson3dTo2d: ()=>geojson3dTo2d, geojsonToWkt: ()=>geojsonToWkt, featureCollectionToWktList: ()=>featureCollectionToWktList, EMPTY_GEOMETRY_COLLECTION_WKT: ()=>EMPTY_GEOMETRY_COLLECTION_WKT, featureCollectionToWkt: ()=>featureCollectionToWkt, featureToWkt: ()=>featureToWkt }); const external_betterknown_namespaceObject = require("betterknown"); const json_namespaceObject = require("klona/json"); const EMPTY_GEOMETRY_COLLECTION_WKT = 'GEOMETRYCOLLECTION EMPTY'; function geometryToWkt(geojson) { return (0, external_betterknown_namespaceObject.geoJSONToWkt)(geojson); } function featureToWkt(geojson) { return geometryToWkt(geojson.geometry); } function featureCollectionToWkt(featureCollection) { if ('FeatureCollection' !== featureCollection.type) throw new Error('GeoJSON is not a FeatureCollection'); return 0 === featureCollection.features.length ? EMPTY_GEOMETRY_COLLECTION_WKT : `GEOMETRYCOLLECTION(${featureCollection.features.map((d)=>featureToWkt(d)).join(',')})`; } function geojsonToWkt(geojson) { switch(geojson.type){ case 'Feature': return featureToWkt(geojson); case 'FeatureCollection': return featureCollectionToWkt(geojson); default: return geometryToWkt(geojson); } } function featureCollectionToWktList(geojson) { return geojson.features.map((d)=>({ wkt: geometryToWkt(d.geometry), ...d.properties })); } function wktToGeojson(item, asFeature = false, properties = {}) { let parsed; parsed = item === EMPTY_GEOMETRY_COLLECTION_WKT ? { type: 'GeometryCollection', geometries: [] } : (0, external_betterknown_namespaceObject.wktToGeoJSON)(item); if (asFeature) return { type: 'Feature', geometry: parsed, properties }; return parsed; } function stripZCoordinates(coords) { if ('number' == typeof coords[0]) return coords.slice(0, 2); return coords.map(stripZCoordinates); } function geojson3dTo2d(geojson) { const newGeoJSON = (0, json_namespaceObject.klona)(geojson); function processGeometry(geom) { if (null == geom) return null; const type = geom.type; switch(type){ case 'Point': case 'MultiPoint': case 'LineString': case 'MultiLineString': case 'Polygon': case 'MultiPolygon': geom.coordinates = stripZCoordinates(geom.coordinates); break; case 'GeometryCollection': geom.geometries = geom.geometries.map(processGeometry); break; default: throw new Error(`geometry type not supported: ${type}`); } return geom; } function processFeature(feature) { if (feature.geometry) feature.geometry = processGeometry(feature.geometry); return feature; } if ('Feature' === newGeoJSON.type) return processFeature(newGeoJSON); if ('FeatureCollection' === newGeoJSON.type) { newGeoJSON.features = newGeoJSON.features.map(processFeature); return newGeoJSON; } return processGeometry(newGeoJSON); } function wkt3dTo2d(wkt) { const geojson = wktToGeojson(wkt); return geojsonToWkt(geojson3dTo2d(geojson)); } exports.EMPTY_GEOMETRY_COLLECTION_WKT = __webpack_exports__.EMPTY_GEOMETRY_COLLECTION_WKT; exports.featureCollectionToWkt = __webpack_exports__.featureCollectionToWkt; exports.featureCollectionToWktList = __webpack_exports__.featureCollectionToWktList; exports.featureToWkt = __webpack_exports__.featureToWkt; exports.geojson3dTo2d = __webpack_exports__.geojson3dTo2d; exports.geojsonToWkt = __webpack_exports__.geojsonToWkt; exports.geometryToWkt = __webpack_exports__.geometryToWkt; exports.wkt3dTo2d = __webpack_exports__.wkt3dTo2d; exports.wktToGeojson = __webpack_exports__.wktToGeojson; for(var __webpack_i__ in __webpack_exports__)if (-1 === [ "EMPTY_GEOMETRY_COLLECTION_WKT", "featureCollectionToWkt", "featureCollectionToWktList", "featureToWkt", "geojson3dTo2d", "geojsonToWkt", "geometryToWkt", "wkt3dTo2d", "wktToGeojson" ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; Object.defineProperty(exports, '__esModule', { value: true });