Remove points of polylines that are outside of polygon using Leaflet

强颜欢笑 提交于 2019-12-20 05:10:21

问题


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.



来源:https://stackoverflow.com/questions/42559669/remove-points-of-polylines-that-are-outside-of-polygon-using-leaflet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!