Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix(geojson-layer): add null-safety checks to prevent crashes on malformed features (#2383) (#3337)
pierreeuropeea81bbf29a34
diff --git a/src/layers/src/geojson-layer/geojson-utils.ts b/src/layers/src/geojson-layer/geojson-utils.tsindex 4fe421469..a4792d02b 100644--- a/src/layers/src/geojson-layer/geojson-utils.ts+++ b/src/layers/src/geojson-layer/geojson-utils.ts@@ -81,7 +81,7 @@ export function parseGeoJsonRawFeature(rawFeature: unknown): Feature | null {// Support GeoJson feature as object// probably need to normalize it as wellconst normalized = normalize(rawFeature);- if (!normalized || !Array.isArray(normalized.features)) {+ if (!normalized || !Array.isArray(normalized.features) || !normalized.features.length) {// fail to normalize GeoJsonreturn null;}@@ -140,7 +140,7 @@ export function getGeojsonLayerMeta({try {// TODO: use line interpolate to get center of line for LineStringconst cent = center(feature as AllGeoJSON);- meanCenters.push(cent.geometry.coordinates);+ meanCenters.push(cent?.geometry?.coordinates ?? null);} catch (e) {meanCenters.push(null);}@@ -476,11 +476,15 @@ export function applyFiltersToTableColumns(const filteredIndexSet = new Set(filteredIndex);const filteredFeatures: GeojsonDataMaps = [];for (const feature of dataToFeature) {+ // @ts-expect-error geometry.coordinates not available for BinaryFeatureCollection+ if (!feature || !feature.geometry || !feature.geometry.coordinates) {+ continue;+ }// @ts-expect-error geometry.coordinates not available for GeometryCollectionconst filteredCoords = feature.geometry.coordinates.filter(c =>filteredIndexSet.has(c.datumIndex));- if (filteredCoords.length > 0 && feature) {+ if (filteredCoords.length > 0) {filteredFeatures.push({...feature,geometry: {