pointsWithinPolygon performance improvement
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 10.5k
- Forks
- 1k
- Avg merge
- 1h 11m
- Merged PRs (30d)
- 4
Description
I've hit a really slow path of the code here. (a lot of points 65606 (only +/- 350 in each polygon) and 475 polygons)
I wanted to find points within each polygon and calculating an average of some properties.
This code stall the browser and making it unresponsive
for (let zone of zones.organisationUnits) {
const ptsWithin = turf.pointsWithinPolygon(
{ type: "FeatureCollection", features: points },
zone.geometry
);
const rwis = ptsWithin.features.map(r => parseFloat(r.properties.record.rwi));
zone.rwi = rwis && rwis.length ? average(rwis) : undefined;
}
I've modified the code to first filter on bbox then only calling pointsWithinPolygon on remaining points and now it completes and quite fast (and stole the inBBox function)
for (let zone of zones.organisationUnits) {
const bbox = turf.bbox(zone.geometry);
const pointsInBbox = points.filter(pt =>
inBBox(pt.geometry.coordinates, bbox)
);
const ptsWithin = turf.pointsWithinPolygon(
{ type: "FeatureCollection", features: pointsInBbox },
zone.geometry
);
const rwis = ptsWithin.features.map(r => parseFloat(r.properties.record.rwi));
zone.rwi = rwis && rwis.length ? average(rwis) : undefined;
}
May be that's an optimization the pointsWithinPolygon should have built-in no ?
you can see it in action here (click on Map)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the pointsWithinPolygon entry point and compare its current work with the reported bbox prefilter using turf.bbox and inBBox. Reproduce the large points-and-polygons case from the issue, then verify that the built-in path avoids the browser-stalling slowdown while preserving the points selected within each polygon.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100