Skip to content

Commit

Permalink
Merge pull request #11 from juanpujol/develop
Browse files Browse the repository at this point in the history
Release v0.1.3
  • Loading branch information
juanpujol authored Mar 25, 2024
2 parents 0074b6a + b1c51eb commit 5d72db2
Show file tree
Hide file tree
Showing 11 changed files with 4,121 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node_modules/
.eslintcache
.env
dist
tests/output
NOTES.md
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"fbbox",
"geospatial",
"juanpujol",
"radiusInKm",
"mutipolygons",
"polyunion",
"rbush",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# PolyUnion Changelog

## v0.1.3 (2024-03-25)

### Fixes 🐞

- Fixes bug were turf intersect will throw an error that should't break the function response. #10 - Thanks ♥️ https://github.com/AbraaoAlves and https://github.com/lamartinecabral

## v0.1.2 (2024-03-12)

### Fixes 🐞
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# PolyUnion

A fast and efficient js utility function designed for merging multiple polygons within a GeoJSON FeatureCollection. It utilizes spatial indexing provided by **[rbush](https://github.com/mourner/rbush)** and the geospatial processing of **[Turfjs](https://github.com/Turfjs/turf)** to achieve high performance.
A fast and efficient Javascript utility function designed to mergE multiple polygons into a GeoJSON FeatureCollection. It utilizes spatial indexing provided by **[rbush](https://github.com/mourner/rbush)** and the geospatial processing of **[Turfjs](https://github.com/Turfjs/turf)** to achieve high performance.

## Performance
## How fast?

When attempting to merge approximately 1000 circle-shaped polygons using only @turf/union, it took approximately 25 seconds. In contrast, PolyUnion completed the same task in about 350 milliseconds.

Expand Down Expand Up @@ -45,14 +45,14 @@ const features = {
};

// Merge polygons
const mergedFeatureCollection = polyunion(features);
const mergedFeatureCollection = polyunion(features, 2);

console.log(mergedFeatureCollection);
```

The function has 2 parameters:

- `featureCollection` (required): A GeoJSON FeatureCollection containing polygons to merge.
- `fc` (required): A GeoJSON FeatureCollection containing polygons to merge (make sure the polygons are valid).
- `totalPasses` (optional, defaults to 3): The total number of passes for recursive calls. Increasing the number of iterations can improve the results, but it will also prolong the execution time. It depends on the number of polygons being merged. For the case in the picture above, 4 passes worked great (350ms).

## Credits
Expand Down
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polyunion",
"version": "0.1.2",
"version": "0.1.3",
"description": "A fast and efficient library for merging multiple polygons in a GeoJSON FeatureCollection",
"type": "module",
"main": "./dist/index.js",
Expand Down Expand Up @@ -37,6 +37,8 @@
"rbush": "^3.0.1"
},
"devDependencies": {
"@turf/circle": "^6.5.0",
"@types/node": "^20.11.30",
"@types/rbush": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"eslint": "^8.57.0",
Expand All @@ -46,4 +48,4 @@
"typescript": "^5.3.3",
"vitest": "^1.3.1"
}
}
}
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ function polyunionPass(

const targetFeature = fc.features[candidate.id];

if (
intersect(mergeCandidate, targetFeature) ||
booleanOverlap(mergeCandidate, targetFeature)
) {
mergeCandidate = union(mergeCandidate, targetFeature) as Feature<Polygon>;
processed.add(candidate.id);
// Attempt to merge the two features if they intersect or overlap
try {
if (
intersect(mergeCandidate, targetFeature) ||
booleanOverlap(mergeCandidate, targetFeature)
) {
mergeCandidate = union(mergeCandidate, targetFeature) as Feature<Polygon>;
processed.add(candidate.id);
}
} catch (err) {
console.error('Polyunion error =>', err);
}
});

mergedFeatures.push(mergeCandidate);

processed.add(index);
});

Expand Down
Loading

0 comments on commit 5d72db2

Please sign in to comment.