Remove points of polylines that are outside of pol

2019-03-02 10:55发布

I've draw polyline programmatically (not using leaflet draw) inside polygone using leaflet draw plugin on map, I want to keep only the points of polyline that are inside of polygone and remove those are outside. Do you have any idea how to do that using a leaflet plugin?. Any help is much appreciated. Thanks

Here is a screenshot:

enter image description here

The expected result:

enter image description here

I did research on difference method of **turf" library as @Sam suggested, so finaly I can apply this method on my drawing polygon and line, here is a code snippet:

var line = path.toGeoJSON();
var polygon = selectedPoly.toGeoJSON();
var difference, result = [];
difference = turf.difference(line, polygon);
if (difference) 
{
    result.push(difference);
    var inter = L.geoJson(result).addTo(map);
}

This is a screenshot of the result:

enter image description here

Now I want to remove this part of line and keep only the section inside polygon, I tried to do that but not working. Can you help me please? Thank you

1条回答
劳资没心,怎么记你
2楼-- · 2019-03-02 11:19

I'm working with turfjs to check for overlapping polygones in leaflet.

map.on('draw:created', function (e) {
      var intersection = [];
      otherPolysLayer.eachLayer(function (layer) {
      if (!_.isUndefined(turf.intersect(e.layer.toGeoJSON(), ))) {
             intersection.push(layer);
      }
   }) 
});

You could change the above so that instead it checks for the entire polygone, you'd check with the difference method.

difference: Finds the difference between two polygons by clipping the second polygon from the first.

I've been looking for a good while for a decent library and looked into leaflet-pip, kevlindev amongst others but I find that turf really just works out of the box.

Update

http://jsfiddle.net/ddce1wh5/ how about this? I used intersect, because that is apparently the part you'd like to keep, I misread, apologies. The following http://jsfiddle.net/mcqL1y90/ we use an array of lines that uses either the intersecting line, or if no intersection is taking place it takes the line itself to draw on the map.

查看更多
登录 后发表回答