Skip to content

Commit

Permalink
add and accept other geometry types
Browse files Browse the repository at this point in the history
  • Loading branch information
meghna0593 committed Dec 18, 2024
1 parent d5ad255 commit 0387571
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
9 changes: 9 additions & 0 deletions api/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const VALID_GEOMETRY_TYPES = [
'GeometryCollection',
'MultiPolygon',
'MultiLineString',
'MultiPoint',
'Polygon',
'LineString',
'Point'
];
1 change: 1 addition & 0 deletions api/src/paths/admin-defined-shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 9 additions & 14 deletions api/src/utils/kml-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 };

Expand Down

0 comments on commit 0387571

Please sign in to comment.