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

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

问题描述

我已经在地图上使用小叶绘制插件以编程方式(不使用小叶绘制)在polygone内绘制了折线,我只想保留多边形内部的多义线点,并删除其外部.您有任何想法如何使用传单插件吗?任何帮助深表感谢.谢谢

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的建议研究了**草皮库的差异方法,因此最终我可以将此方法应用于我的图形多边形和线条,这是一个代码段:

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: Finds the difference between two polygons by clipping the second polygon from the first.

我一直在寻找一个不错的图书馆好一阵子,并研究了leaf-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/怎么样?我使用了相交,因为很显然这是您想要保留的部分,我误读了道歉.以下 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天全站免登陆