From 03875713eacfaa74a80eedd6ccb022c24b56b56b Mon Sep 17 00:00:00 2001 From: Meghna Holla Date: Wed, 18 Dec 2024 10:43:39 -0800 Subject: [PATCH] add and accept other geometry types --- api/src/constants.ts | 9 +++++++++ api/src/paths/admin-defined-shapes.ts | 1 + api/src/utils/kml-import.ts | 23 +++++++++-------------- 3 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 api/src/constants.ts diff --git a/api/src/constants.ts b/api/src/constants.ts new file mode 100644 index 000000000..6859f749c --- /dev/null +++ b/api/src/constants.ts @@ -0,0 +1,9 @@ +export const VALID_GEOMETRY_TYPES = [ + 'GeometryCollection', + 'MultiPolygon', + 'MultiLineString', + 'MultiPoint', + 'Polygon', + 'LineString', + 'Point' +]; diff --git a/api/src/paths/admin-defined-shapes.ts b/api/src/paths/admin-defined-shapes.ts index 46fb2d1e3..8fd64936c 100644 --- a/api/src/paths/admin-defined-shapes.ts +++ b/api/src/paths/admin-defined-shapes.ts @@ -274,6 +274,7 @@ function uploadShape(): RequestHandler { const buffer = Buffer.from(data['data'], 'base64'); const KML = KMZToKML(buffer); const dirtyGeoJSON = GeoJSONFromKML(KML); + geoJSON = sanitizeGeoJSON(dirtyGeoJSON); break; diff --git a/api/src/utils/kml-import.ts b/api/src/utils/kml-import.ts index 04265a5b0..6e1f8d58d 100644 --- a/api/src/utils/kml-import.ts +++ b/api/src/utils/kml-import.ts @@ -3,6 +3,7 @@ import * as GeoJSON from '@mapbox/togeojson'; import AdmZip from 'adm-zip'; import { FeatureCollection } from 'geojson'; import { getLogger } from 'utils/logger'; +import { VALID_GEOMETRY_TYPES } from 'constants'; function KMZToKML(data: Buffer): Buffer { const log = getLogger('KML'); @@ -59,20 +60,14 @@ function sanitizeGeoJSON(data: FeatureCollection): FeatureCollection { throw new Error(`Invalid GeoJSON Type: ${data.type}`); } - // filter out non-polygon features (V1) - const newFeatures = data.features.map((feature) => { - if ( - feature.geometry.type === 'Polygon' || - feature.geometry.type === 'LineString' || - feature.geometry.type === 'Point' - ) { - return { - type: feature.type, - geometry: feature.geometry, - properties: {} - }; - } - }); + // Exclude features with geometry types that are not supported + const newFeatures = data.features + .filter((feature) => VALID_GEOMETRY_TYPES.includes(feature.geometry.type)) + .map((feature) => ({ + type: feature.type, + geometry: feature.geometry, + properties: {} + })); const filteredData: FeatureCollection = { type: 'FeatureCollection', features: newFeatures };