使用 Leaflet 删除多边形外部的折线点 [英] Remove points of polylines that are outside of polygon using Leaflet

查看:73
本文介绍了使用 Leaflet 删除多边形外部的折线点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用地图上的传单绘制插件以编程方式(不使用传单绘制)在多边形内部绘制折线,我只想保留多边形内部的折线点并删除那些在外面的点.你知道如何使用传单插件来做到这一点吗?任何帮助深表感谢.谢谢

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

这是截图:

预期结果:

我按照@Sam 的建议研究了 **turf" 库的 difference 方法,所以最后我可以将此方法应用于我的绘图多边形和线条,这是一个代码片段:

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);
}

这是结果的截图:

现在我想删除这部分线并只保留多边形内的部分,我尝试这样做但没有工作.你能帮我吗?谢谢

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

推荐答案

我正在与 turfjs 一起检查用于传单中的重叠多边形.

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:通过裁剪多边形来找出两个多边形之间的差异第一个多边形的第二个多边形.

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

我一直在寻找一个像样的图书馆,并研究了leaflet-pip、kevlindev 等,但我发现草皮真的可以开箱即用.

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.

更新

http://jsfiddle.net/ddce1wh5/ 这个怎么样?我使用了 intersect,因为这显然是您想要保留的部分,我误读了,抱歉.以下 http://jsfiddle.net/mcqL1y90/ 我们使用了一个使用相交线的数组线,或者如果没有发生交叉点,则将线本身绘制在地图上.

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.

这篇关于使用 Leaflet 删除多边形外部的折线点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆