Skip to content

Commit 1123ae8

Browse files
authored
Merge pull request #3 from brahn/fix-sjoin-with-duplicate-fieldnames
Fix sjoin with duplicate fieldnames
2 parents dff5f13 + d5704b1 commit 1123ae8

18 files changed

+20040
-28789
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spatialmerge",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Merging geospatial data in JS",
55
"type": "module",
66
"main": "./dist/spatialmerge.cjs.js",

src/helpers.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
1-
function mergeWith (object1, object2, lsuffix, rsuffix) {
2-
const keys1 = Object.keys(object1)
3-
const keys2 = Object.keys(object2)
1+
function findAllDuplicates(array) {
2+
const uniq = new Set(array);
3+
if (uniq.size === array.length) {
4+
return [];
5+
}
6+
const duplicates = new Set()
7+
for (const element of array) {
8+
if (uniq.has(element)) {
9+
uniq.delete(element);
10+
} else {
11+
duplicates.add(element);
12+
}
13+
}
14+
return [...duplicates];
15+
}
16+
17+
function mergeWith(leftFeatureProps, rightFeatureProps, lsuffix = 'left', rsuffix = 'right') {
18+
const keys1 = Object.keys(leftFeatureProps)
19+
const keys2 = Object.keys(rightFeatureProps)
420
const allKeys = [...keys1, ...keys2]
5-
const uniq = [...new Set(allKeys)]
21+
const duplicates = findAllDuplicates(allKeys)
622
const feature = {}
7-
if (allKeys.length === uniq.length) {
8-
keys1.forEach(key => (feature[key] = object1[key]))
9-
keys2.forEach(key => (feature[key] = object2[key]))
23+
if (duplicates.length === 0) {
24+
keys1.forEach(key => (feature[key] = leftFeatureProps[key]))
25+
keys2.forEach(key => (feature[key] = rightFeatureProps[key]))
1026
return feature
1127
} else {
12-
const duplicates = allKeys.filter(key => uniq.indexOf(key) === -1)
1328
keys1.forEach(key => {
1429
if (duplicates.indexOf(key) > -1) {
15-
feature[key + '_' + lsuffix] = object1[key]
30+
feature[key + '_' + lsuffix] = leftFeatureProps[key]
1631
} else {
17-
feature[key] = object1[key]
32+
feature[key] = leftFeatureProps[key]
1833
}
1934
})
2035
keys2.forEach(key => {
2136
if (duplicates.indexOf(key) > -1) {
22-
feature[key + '_' + rsuffix] = object2[key]
37+
feature[key + '_' + rsuffix] = rightFeatureProps[key]
2338
} else {
24-
feature[key] = object1[key]
39+
feature[key] = rightFeatureProps[key]
2540
}
2641
})
2742
return feature
2843
}
2944
}
3045

3146
export {
47+
findAllDuplicates, // exported just for testing
3248
mergeWith
3349
}

src/merge.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ function merge (geojson1, geojsonOrDataFrame, options = {}) {
2020
try {
2121
geojson1 = clone(geojson1)
2222
} catch (e) {
23-
throw new Error('Cloning input GeoJSON failed. Check for missing/empty geometries in the feature collection. Original @turf/clone error message:', e)
23+
// @turf/clone probably failed due to
24+
// https://github.com/Turfjs/turf/issues/2314
25+
// falling back to JSON.stringify / parse
26+
geojson1 = JSON.parse(JSON.stringify(geojson1))
2427
}
2528
}
2629

0 commit comments

Comments
 (0)