Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: Fix PointLayer polygon filtering for GeoJSON column mode (#3410)
Igor Dykhta735bfc0dda4c
diff --git a/src/layers/src/point-layer/point-layer.ts b/src/layers/src/point-layer/point-layer.tsindex 8fb25529c..bcbff2fcc 100644--- a/src/layers/src/point-layer/point-layer.ts+++ b/src/layers/src/point-layer/point-layer.ts@@ -441,7 +441,7 @@ export default class PointLayer extends Layer {pushPointPosition(data, pos, index, neighbors);} else {// COLUMN_MODE_GEOJSON mode - point from geojson coordinates- const coordinates = this.dataToFeature[i];+ const coordinates = this.dataToFeature[index];// if multi pointsif (coordinates && Array.isArray(coordinates[0])) {coordinates.forEach(coord => {@@ -647,6 +647,14 @@ export default class PointLayer extends Layer {objectInfo.index >= 0 &&this.dataContainer) {+ if (this.config.columnMode === COLUMN_MODE_GEOJSON) {+ const coordinates = this.dataToFeature[objectInfo.index];+ if (!coordinates) return null;+ const position = Array.isArray(coordinates[0])+ ? (coordinates as number[][])[0]+ : coordinates;+ return {index: objectInfo.index, position};+ }return {index: objectInfo.index,position: this.getPositionAccessor(this.dataContainer)(objectInfo)diff --git a/src/utils/src/filter-utils.ts b/src/utils/src/filter-utils.tsindex 023265367..515dd53c6 100644--- a/src/utils/src/filter-utils.ts+++ b/src/utils/src/filter-utils.ts@@ -382,6 +382,23 @@ export const getPolygonFilterFunctor = (layer, filter, dataContainer) => {switch (layer.type) {case LAYER_TYPES.point:case LAYER_TYPES.icon:+ if (layer.config?.columnMode === 'geojson' && layer.dataToFeature?.length) {+ return data => {+ const coordinates = layer.dataToFeature[data.index];+ if (!coordinates) return false;+ if (Array.isArray(coordinates[0])) {+ return (coordinates as number[][]).some(+ coord =>+ coord.length >= 2 && coord.every(Number.isFinite) && isInPolygon(coord, filter.value)+ );+ }+ return (+ coordinates.length >= 2 &&+ coordinates.every(Number.isFinite) &&+ isInPolygon(coordinates, filter.value)+ );+ };+ }return data => {const pos = getPosition(data);return pos.every(Number.isFinite) && isInPolygon(pos, filter.value);diff --git a/test/node/utils/filter-utils-test.js b/test/node/utils/filter-utils-test.jsindex 3b2cc5069..459756845 100644--- a/test/node/utils/filter-utils-test.js+++ b/test/node/utils/filter-utils-test.js@@ -14,6 +14,7 @@ import {validatePolygonFilter,generatePolygonFilter,isInPolygon,+ getPolygonFilterFunctor,diffFilters,getTimestampFieldDomain,scaleSourceDomainToDestination,@@ -662,3 +663,56 @@ test('filterUtils -> mergeFilterWithTimeline', t => {t.end();});++test('filterUtils -> getPolygonFilterFunctor -> point layer with dataToFeature (GeoJSON column mode)', t => {+ const squarePolygon = {+ type: 'Feature',+ properties: {},+ geometry: {+ type: 'Polygon',+ coordinates: [[[-1, -1], [1, -1], [1, 1], [-1, 1], [-1, -1]]]+ }+ };++ const filter = {value: squarePolygon};++ // Point layer with dataToFeature (GeoJSON column mode)+ const layerWithDataToFeature = {+ type: 'point',+ config: {columnMode: 'geojson'},+ getPositionAccessor: () => () => null,+ dataToFeature: {+ length: 6,+ 0: [0.5, 0.5], // inside+ 1: [10, 10], // outside+ 2: [[10, 10], [0.2, 0.2]], // MultiPoint: one inside+ 3: [[10, 10], [20, 20]], // MultiPoint: all outside+ 4: [], // empty coordinates (e.g. empty GeometryCollection)+ 5: [[], [0.5, 0.5]] // MultiPoint with one empty coord pair+ }+ };++ const fn = getPolygonFilterFunctor(layerWithDataToFeature, filter, null);++ t.equal(fn({index: 0}), true, 'Single point inside polygon should return true');+ t.equal(fn({index: 1}), false, 'Single point outside polygon should return false');+ t.equal(fn({index: 2}), true, 'MultiPoint with at least one point inside should return true');+ t.equal(fn({index: 3}), false, 'MultiPoint with all points outside should return false');+ t.equal(fn({index: 4}), false, 'Empty coordinates should return false');+ t.equal(fn({index: 5}), true, 'MultiPoint with one empty and one valid inside should return true');++ // Point layer without dataToFeature (standard column mode)+ const layerWithoutDataToFeature = {+ type: 'point',+ config: {columnMode: 'points'},+ getPositionAccessor: () => d => d.position,+ dataToFeature: []+ };++ const fn2 = getPolygonFilterFunctor(layerWithoutDataToFeature, filter, null);++ t.equal(fn2({position: [0.5, 0.5]}), true, 'Standard mode: point inside should return true');+ t.equal(fn2({position: [10, 10]}), false, 'Standard mode: point outside should return false');++ t.end();+});