Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix(table): use full polygon geometry in spatial joins (#3862)
Igor Dykhtaff3239eb45cc
diff --git a/src/table/package.json b/src/table/package.jsonindex a2075cbc0..a4e162e49 100644--- a/src/table/package.json+++ b/src/table/package.json@@ -40,6 +40,7 @@"@turf/boolean-intersects": "^7.2.0","@turf/boolean-overlap": "^7.2.0","@turf/boolean-touches": "^7.2.0",+ "@turf/difference": "^7.2.0","@types/d3-array": "^2.8.0","d3-array": "^2.8.0","es-toolkit": "^1.39.10",diff --git a/src/table/src/dataset-ops.spec.ts b/src/table/src/dataset-ops.spec.tsindex c51fb2029..dfebe31d7 100644--- a/src/table/src/dataset-ops.spec.ts+++ b/src/table/src/dataset-ops.spec.ts@@ -348,7 +348,7 @@ describe('dataset-ops engine', () => {expect(result.data.rows[0][3]).toBe(3);});- test('spatialJoinDatasets joins polygons whose centroid is inside the left polygon', () => {+ test('spatialJoinDatasets joins polygons contained by the left polygon', () => {const left = makeTable({id: 'outer',fields: [{name: 'geom', type: ALL_FIELD_TYPES.geojson}],@@ -431,6 +431,252 @@ describe('dataset-ops engine', () => {expect(result.data.rows[0][2]).toBe(7);});+ test.each(['within', 'intersects'] as const)(+ 'spatialJoinDatasets %s excludes centroid-only polygon matches from aggregates',+ predicate => {+ const donut = square(-10, -10, 10, 10);+ donut.geometry.coordinates.push(square(-8, -8, 8, 8).geometry.coordinates[0]);+ const left = makeTable({+ id: 'target',+ fields: [{name: 'geom', type: ALL_FIELD_TYPES.geojson}],+ rows: [[predicate === 'within' ? square(-10, -10, 10, 10) : square(-3, -3, 3, 3)]]+ });+ const right = makeTable({+ id: 'join',+ fields: [+ {name: 'geom', type: ALL_FIELD_TYPES.geojson},+ {name: 'value', type: ALL_FIELD_TYPES.integer}+ ],+ rows: [[predicate === 'within' ? square(-5, -5, 5, 5) : donut, 99]]+ });++ const result = spatialJoinDatasets(left, right, {+ leftGeo: {kind: 'geojson', fieldName: 'geom'},+ rightGeo: {kind: 'geojson', fieldName: 'geom'},+ aggregations: {value: DATASET_OPS_AGGREGATIONS.sum},+ predicate+ });++ expect(result.data.rows.map(row => row.slice(1))).toEqual([[0, null]]);+ }+ );++ describe('spatial join polygon predicates', () => {+ const outer = square(-10, -10, 10, 10);+ const donut = square(-10, -10, 10, 10);+ donut.geometry.coordinates.push(square(-8, -8, 8, 8).geometry.coordinates[0]);+ const multi = (...polygons: ReturnType<typeof square>[]) => ({+ type: 'MultiPolygon',+ coordinates: polygons.map(polygon => polygon.geometry.coordinates)+ });+ const islands = multi(square(-10, -10, -5, -5), square(5, 5, 10, 10));+ const concave = {+ type: 'Polygon',+ coordinates: [+ [+ [0, 0],+ [6, 0],+ [6, 6],+ [4, 6],+ [4, 2],+ [2, 2],+ [2, 6],+ [0, 6],+ [0, 0]+ ]+ ]+ };++ test.each([+ {name: 'equal polygons', target: outer, join: outer, within: true, intersects: true},+ {+ name: 'shared contained edge',+ target: square(-10, -10, 0, 0),+ join: outer,+ within: true,+ intersects: true+ },+ {+ name: 'edge contact',+ target: square(10, 0, 12, 2),+ join: outer,+ within: false,+ intersects: true+ },+ {+ name: 'vertex contact',+ target: square(10, 10, 12, 12),+ join: outer,+ within: false,+ intersects: true+ },+ {+ name: 'partial overlap',+ target: square(5, 5, 15, 15),+ join: outer,+ within: false,+ intersects: true+ },+ {+ name: 'disjoint bounds',+ target: square(20, 20, 30, 30),+ join: outer,+ within: false,+ intersects: false+ },+ {+ name: 'enclosed hole',+ target: square(-9, -9, 9, 9),+ join: donut,+ within: false,+ intersects: true+ },+ {name: 'equal donuts', target: donut, join: donut, within: true, intersects: true},+ {name: 'donut in solid polygon', target: donut, join: outer, within: true, intersects: true},+ {+ name: 'hole boundary contact',+ target: square(-8, -2, -6, 2),+ join: donut,+ within: false,+ intersects: true+ },+ {+ name: 'edge crosses concavity',+ target: square(1, 1, 5, 5),+ join: concave,+ within: false,+ intersects: true+ },+ {+ name: 'contained components',+ target: multi(square(-9, -9, -6, -6), square(6, 6, 9, 9)),+ join: islands,+ within: true,+ intersects: true+ },+ {+ name: 'one outside component',+ target: multi(square(-9, -9, -6, -6), square(20, 20, 21, 21)),+ join: islands,+ within: false,+ intersects: true+ },+ {+ name: 'between components',+ target: square(-1, -1, 1, 1),+ join: islands,+ within: false,+ intersects: false+ },+ {name: 'point inside', target: [0, 0], join: outer, within: true, intersects: true},+ {name: 'point on boundary', target: [10, 0], join: outer, within: false, intersects: true},+ {name: 'point in hole', target: [0, 0], join: donut, within: false, intersects: false}+ ])('$name preserves count, sum and average', ({target, join, within, intersects}) => {+ const left = makeTable({+ id: 'target',+ fields: [{name: 'geom', type: ALL_FIELD_TYPES.geojson}],+ rows: [[target]]+ });+ const right = makeTable({+ id: 'join',+ fields: [+ {name: 'geom', type: ALL_FIELD_TYPES.geojson},+ {name: 'value', type: ALL_FIELD_TYPES.integer},+ {name: 'score', type: ALL_FIELD_TYPES.integer}+ ],+ rows: [+ [join, 10, 10],+ [join, 20, 20],+ [square(60, 60, 70, 70), 999, 999]+ ]+ });+ for (const predicate of ['within', 'intersects'] as const) {+ const result = spatialJoinDatasets(left, right, {+ leftGeo: {kind: 'geojson', fieldName: 'geom'},+ rightGeo: {kind: 'geojson', fieldName: 'geom'},+ aggregations: {+ value: DATASET_OPS_AGGREGATIONS.sum,+ score: DATASET_OPS_AGGREGATIONS.average+ },+ predicate+ });+ const matches = predicate === 'within' ? within : intersects;+ expect(result.data.rows[0].slice(1)).toEqual(matches ? [2, 30, 15] : [0, null, null]);+ }+ });++ test.each([+ null,+ 'invalid json',+ {type: 'Feature', geometry: null, properties: {}},+ {type: 'Polygon', coordinates: []},+ {type: 'Polygon', coordinates: [[]]},+ {type: 'MultiPolygon', coordinates: []},+ {+ type: 'Polygon',+ coordinates: [+ [+ [0, 0],+ [1, 0],+ [0, 1]+ ]+ ]+ },+ {+ type: 'Polygon',+ coordinates: [+ [+ [0, 0],+ [1, 0],+ [0, 1],+ [1, 1]+ ]+ ]+ },+ {type: 'Feature', geometry: {type: 'Polygon', coordinates: null}, properties: {}},+ {+ type: 'Polygon',+ coordinates: [+ [+ [NaN, 0],+ [1, 0],+ [0, 1],+ [NaN, 0]+ ]+ ]+ }+ ])('ignores missing, empty or malformed polygon geometry: %j', invalid => {+ const left = makeTable({+ id: 'target',+ fields: [{name: 'geom', type: ALL_FIELD_TYPES.geojson}],+ rows: [[invalid], [outer]]+ });+ const right = makeTable({+ id: 'join',+ fields: [+ {name: 'geom', type: ALL_FIELD_TYPES.geojson},+ {name: 'value', type: ALL_FIELD_TYPES.integer}+ ],+ rows: [+ [invalid, 99],+ [outer, 7]+ ]+ });+ for (const predicate of ['within', 'intersects'] as const) {+ const result = spatialJoinDatasets(left, right, {+ leftGeo: {kind: 'geojson', fieldName: 'geom'},+ rightGeo: {kind: 'geojson', fieldName: 'geom'},+ aggregations: {value: DATASET_OPS_AGGREGATIONS.sum},+ predicate+ });+ expect(result.data.rows.map(row => row.slice(1))).toEqual([+ [0, null],+ [1, 7]+ ]);+ }+ });+ });+test('isTabularDatasetForOps skips tiles and disabled datasets', () => {expect(isTabularDatasetForOps({type: 'local'})).toBe(true);expect(isTabularDatasetForOps({type: ''})).toBe(true);diff --git a/src/table/src/dataset-ops/spatial-join.ts b/src/table/src/dataset-ops/spatial-join.tsindex f680b333d..497aa307c 100644--- a/src/table/src/dataset-ops/spatial-join.ts+++ b/src/table/src/dataset-ops/spatial-join.ts@@ -9,7 +9,8 @@ import {booleanEqual} from '@turf/boolean-equal';import {booleanIntersects} from '@turf/boolean-intersects';import {booleanOverlap} from '@turf/boolean-overlap';import {booleanTouches} from '@turf/boolean-touches';-import type {Feature} from 'geojson';+import {difference} from '@turf/difference';+import type {Feature, MultiPolygon, Polygon} from 'geojson';import {aggregateValues, resultFieldType} from './aggregations';import {@@ -67,6 +68,38 @@ export function suggestSpatialGeo(fields: Field[]): SpatialGeoSource | null {return null;}+function hasPolygonCoordinates(feature: Feature): boolean {+ const geometry = feature.geometry;+ const polygons =+ geometry.type === 'MultiPolygon'+ ? geometry.coordinates+ : geometry.type === 'Polygon'+ ? [geometry.coordinates]+ : [];+ return (+ Array.isArray(polygons) &&+ polygons.length > 0 &&+ polygons.every(+ polygon =>+ Array.isArray(polygon) &&+ polygon.length > 0 &&+ polygon.every(+ ring =>+ Array.isArray(ring) &&+ ring.length >= 4 &&+ ring.every(+ position =>+ Array.isArray(position) &&+ Number.isFinite(position[0]) &&+ Number.isFinite(position[1])+ ) &&+ ring[0][0] === ring[ring.length - 1][0] &&+ ring[0][1] === ring[ring.length - 1][1]+ )+ )+ );+}+function readSpatialValue(dataset: DatasetOpsTable,rowIndex: number,@@ -100,7 +133,8 @@ function readSpatialValue(return null;}if (isPolygonFeature(feature)) {- return {kind: 'polygon', feature};+ // Turf predicates may return true for malformed coordinates instead of throwing.+ return hasPolygonCoordinates(feature) ? {kind: 'polygon', feature} : null;}const lngLat = getLngLat(feature);return lngLat ? {kind: 'point', lngLat} : null;@@ -114,8 +148,22 @@ export function leftContainsRight(left: ParsedSpatialValue, right: ParsedSpatialreturn polygonContainsPoint(left.feature, right.lngLat);}if (left.kind === 'polygon' && right.kind === 'polygon') {- const centroid = getCentroidLngLat(right.feature);- return Boolean(centroid && polygonContainsPoint(left.feature, centroid));+ return turfMatch(+ (container, target) => {+ // Test the entire area, including holes and every MultiPolygon component.+ // Equal polygons and contained polygons sharing an edge are within; a+ // boundary-only contact is not. Intersects also rejects empty geometries.+ return (+ booleanIntersects(container, target) &&+ difference({+ type: 'FeatureCollection',+ features: [target, container] as Feature<Polygon | MultiPolygon>[]+ }) === null+ );+ },+ left,+ right+ );}if (left.kind === 'polygon' && right.kind === 'h3') {const centroid = getCentroid({id: right.index});@@ -210,6 +258,9 @@ export function geometriesMatchPredicate(return turfMatch(booleanTouches, left, right);case SPATIAL_JOIN_PREDICATES.intersects:default:+ if (left?.kind === 'polygon' && right?.kind === 'polygon') {+ return turfMatch(booleanIntersects, left, right);+ }return (leftContainsRight(left, right) ||leftContainsRight(right, left) ||diff --git a/yarn.lock b/yarn.lockindex 5cb0f84d6..012cab775 100644--- a/yarn.lock+++ b/yarn.lock@@ -4491,6 +4491,7 @@ __metadata:"@turf/boolean-intersects": "npm:^7.2.0""@turf/boolean-overlap": "npm:^7.2.0""@turf/boolean-touches": "npm:^7.2.0"+ "@turf/difference": "npm:^7.2.0""@types/d3-array": "npm:^2.8.0"d3-array: "npm:^2.8.0"es-toolkit: "npm:^1.39.10"