Skip to content

Commit

Permalink
Merge pull request #3 from brahn/fix-sjoin-with-duplicate-fieldnames
Browse files Browse the repository at this point in the history
Fix sjoin with duplicate fieldnames
  • Loading branch information
chrispahm authored Jul 26, 2022
2 parents dff5f13 + d5704b1 commit 1123ae8
Show file tree
Hide file tree
Showing 18 changed files with 20,040 additions and 28,789 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spatialmerge",
"version": "1.0.0",
"version": "1.0.1",
"description": "Merging geospatial data in JS",
"type": "module",
"main": "./dist/spatialmerge.cjs.js",
Expand Down
40 changes: 28 additions & 12 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
function mergeWith (object1, object2, lsuffix, rsuffix) {
const keys1 = Object.keys(object1)
const keys2 = Object.keys(object2)
function findAllDuplicates(array) {
const uniq = new Set(array);
if (uniq.size === array.length) {
return [];
}
const duplicates = new Set()
for (const element of array) {
if (uniq.has(element)) {
uniq.delete(element);
} else {
duplicates.add(element);
}
}
return [...duplicates];
}

function mergeWith(leftFeatureProps, rightFeatureProps, lsuffix = 'left', rsuffix = 'right') {
const keys1 = Object.keys(leftFeatureProps)
const keys2 = Object.keys(rightFeatureProps)
const allKeys = [...keys1, ...keys2]
const uniq = [...new Set(allKeys)]
const duplicates = findAllDuplicates(allKeys)
const feature = {}
if (allKeys.length === uniq.length) {
keys1.forEach(key => (feature[key] = object1[key]))
keys2.forEach(key => (feature[key] = object2[key]))
if (duplicates.length === 0) {
keys1.forEach(key => (feature[key] = leftFeatureProps[key]))
keys2.forEach(key => (feature[key] = rightFeatureProps[key]))
return feature
} else {
const duplicates = allKeys.filter(key => uniq.indexOf(key) === -1)
keys1.forEach(key => {
if (duplicates.indexOf(key) > -1) {
feature[key + '_' + lsuffix] = object1[key]
feature[key + '_' + lsuffix] = leftFeatureProps[key]
} else {
feature[key] = object1[key]
feature[key] = leftFeatureProps[key]
}
})
keys2.forEach(key => {
if (duplicates.indexOf(key) > -1) {
feature[key + '_' + rsuffix] = object2[key]
feature[key + '_' + rsuffix] = rightFeatureProps[key]
} else {
feature[key] = object1[key]
feature[key] = rightFeatureProps[key]
}
})
return feature
}
}

export {
findAllDuplicates, // exported just for testing
mergeWith
}
5 changes: 4 additions & 1 deletion src/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ function merge (geojson1, geojsonOrDataFrame, options = {}) {
try {
geojson1 = clone(geojson1)
} catch (e) {
throw new Error('Cloning input GeoJSON failed. Check for missing/empty geometries in the feature collection. Original @turf/clone error message:', e)
// @turf/clone probably failed due to
// https://github.com/Turfjs/turf/issues/2314
// falling back to JSON.stringify / parse
geojson1 = JSON.parse(JSON.stringify(geojson1))
}
}

Expand Down
Loading

0 comments on commit 1123ae8

Please sign in to comment.