From 92966d64aac85e7e6abd4891dac62251529ce1ac Mon Sep 17 00:00:00 2001 From: Meghna Holla <35996935+meghna0593@users.noreply.github.com> Date: Wed, 18 Dec 2024 11:15:08 -0800 Subject: [PATCH] Fix KML Upload Visibility Issue (#3759) * add and accept other geometry types * move geo type to sharedAPI constants, remove unused activity type from constants --- api/src/constants/misc.ts | 12 ------------ api/src/paths/admin-defined-shapes.ts | 1 + api/src/utils/kml-import.ts | 23 +++++++++-------------- sharedAPI/src/validation/constants.ts | 10 ++++++++++ 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/api/src/constants/misc.ts b/api/src/constants/misc.ts index 699cd4b8f..17b6a0c9b 100644 --- a/api/src/constants/misc.ts +++ b/api/src/constants/misc.ts @@ -52,18 +52,6 @@ export const ALL_ROLES = [ Role.PRIMARY_BIOCONTROL_USER ]; -/** - * Supported activity types. - * - * @export - * @enum {number} - */ -export enum ActivityType { - OBSERVATION = 'Observation', - MONITOR = 'Monitor', - TREATMENT = 'Treatment' -} - /** * Some of the S3 ACL roles supported by default. * 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..c1663f5c8 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 'sharedAPI'; 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 }; diff --git a/sharedAPI/src/validation/constants.ts b/sharedAPI/src/validation/constants.ts index 6d2b7043b..e86dec943 100644 --- a/sharedAPI/src/validation/constants.ts +++ b/sharedAPI/src/validation/constants.ts @@ -22,3 +22,13 @@ export const FORM_SUBTYPES_WITH_AREA_LIMITS = [ 'Activity_Transect_BiocontrolEfficacy', 'Activity_Biocontrol_Collection' ]; + +export const VALID_GEOMETRY_TYPES = [ + 'GeometryCollection', + 'MultiPolygon', + 'MultiLineString', + 'MultiPoint', + 'Polygon', + 'LineString', + 'Point' +];