Skip to content

Instantly share code, notes, and snippets.

@maptastik
Last active December 1, 2018 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/666edc97efe75eb3c7fedfcd9c651d71 to your computer and use it in GitHub Desktop.
Save maptastik/666edc97efe75eb3c7fedfcd9c651d71 to your computer and use it in GitHub Desktop.
Iterate through polygon geojson and find number of points in each
function countPointsInPolygons(points, polygonJson) {
let countsArray = [];
let polygonFeaturesArray = polygonJson.features;
for (let i = 0; i < polygonFeaturesArray.length; i++) {
let iGeometry = turf.polygon(polygonFeaturesArray[i].geometry.coordinates);
let pointsWithinPolygon = turf.pointsWithinPolygon(points, iGeometry);
iGeometry.properties.count = pointsWithinPolygon.features.length;
countsArray.push(iGeometry);
}
return turf.featureCollection(countsArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment