Remove points of polylines that are outside of pol

2019-03-02 10:34发布

问题:

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:

The expected result:

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:

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:

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.