I have some big Polygon. Within it collection of leaflet layers (points). Every point have some numeric property. What I want is split up big Polygon into smaller ones. Every smaller polygon should contain points with ~equal (+-200 ok) sum of points properties. On the left side of my example page I added image of desirable result. Here is my simplified example with sufficient code and comments.
So my first step is find some start point within big Polygon. It's should be point near Polygon edge - the northernmost point for example.
var nothernmostPoint= 0; var nothernmostLayer= 0; L.geoJSON(features, { pointToLayer: function (feature) { return L.circleMarker(feature.geometry.coordinates.reverse(), defaultPointStyle); }, onEachFeature: function (feature, layer) { if (feature.geometry.coordinates[0] > nothernmostPoint) { nothernmostPoint = feature.geometry.coordinates[0]; nothernmostLayer = feature; } } }).addTo(map);
Second step is to find next nearest point(s) to my start point.
var geoJ = L.GeometryUtil.nClosestLayers(map, features, nothernmostLayer.geometry.coordinates, 5);
- Then sum up their properties. If sum is smaller than needed I go to step 2 and repeat, if sum is satisfy condition then I draw polygon with chosen points inside it and find nearest point to my last point and repeat search for points for the next smaller polygon.
My current difficulty is to find nearest point(s) to my start point. For that purpose I use GeometryUtil Leaflet plugin. Red dots are points that GeometryUtil found as nearest to my start point (the green one). It's certainly not what I was expected.
What am I doing wrong?
Maybe I should use different algorithm and/or tool for that task.
If it may help - all that data I store in PostgreSQL with PostGIS extension. Maybe this can be done on database level.