maptastik
12/1/2018 - 3:46 PM

Count points in polygons

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);
}