Polygon Aggregation POC

node v8.17.0
version: 1.0.0
endpointsharetweet
var pointGroup = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.09404754638672, 50.96615714459998 ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.10400390625, 50.96410303227502 ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.09696578979492, 50.96080545150975 ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.08615112304688, 50.96210288826351 ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.08906936645508, 50.973778188690716 ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.07559394836426, 50.96896788796588 ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "Point", "coordinates": [ -114.08005714416504, 50.975669743502245 ] } } ] } var polygons = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "id":'1'}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -114.0913438796997, 50.96538686312194 ], [ -114.09936904907225, 50.961346054560266 ], [ -114.08975601196289, 50.95458806431627 ], [ -114.08288955688475, 50.95977828838435 ], [ -114.08580780029297, 50.965832817113345 ], [ -114.0913438796997, 50.96538686312194 ] ] ] } }, { "type": "Feature", "properties": { "id":'2', "stroke": "#555555", "stroke-width": 2, "stroke-opacity": 1, "fill": "#555555", "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -114.08580780029297, 50.965832817113345 ], [ -114.08085107803345, 50.9657517348879 ], [ -114.08452033996582, 50.97110285820415 ], [ -114.09374713897704, 50.96863006722691 ], [ -114.09132242202759, 50.965373349297785 ], [ -114.08580780029297, 50.965832817113345 ] ] ] } } ] } const combinedGeoJson = { "type": "FeatureCollection", "features": [...pointGroup.features,...polygons.features] } console.log(combinedGeoJson)
var turf = require("@turf/turf"); // 1. Turf -> booleanPointInPolygon // https://turfjs.org/docs/#booleanPointInPolygon for (let i = 0, j = pointGroup.features.length - 1; i < j; i += 1) { const point = pointGroup.features[i]; for (let n = 0, m = polygons.features.length - 1; n < m; n += 1) { const polygon = polygons.features[n]; const result = turf.booleanPointInPolygon(point, polygon); if (result) { if (polygon.properties.data) { polygon.properties.data.push(point); polygon.properties.result += 1; } else { polygon.properties.data = [point]; polygon.properties.result = 0; } break; } } } polygons.features.forEach(polygon => { if(polygon.properties.data){ const combinedGeoJson = { type: "FeatureCollection", features: [polygon, ...polygon.properties.data] }; console.log(combinedGeoJson); } });
// 2. Turf -> pointsWithinPolygon // https://turfjs.org/docs/#pointsWithinPolygon for (let i=0, j=polygons.features.length-1; i<j; i+=1) { const polygon = polygons.features[i] const ptsWithin = turf.pointsWithinPolygon(pointGroup, polygon); polygon.properties.data = ptsWithin.features; } polygons.features.forEach(polygon => { if(polygon.properties.data){ const combinedGeoJson = { type: "FeatureCollection", features: [polygon, ...polygon.properties.data] }; console.log(combinedGeoJson); } });
Loading…

no comments

    sign in to comment