【问题标题】:Leaflet and Turf.js Points within poly多边形内的 Leaflet 和 Turf.js 点
【发布时间】:2016-03-15 18:25:20
【问题描述】:

我在传单中有一个包含 17 个点 (GeoJSON) 的简单地图,我使用绘图工具创建了一个多边形,用于选择多边形内的点。

map.on('draw:created', function (e) {  //from draw tool
    var type = e.layerType,
        layer = e.layer;
        editableLayers.addLayer(layer);
        GetSelection(editableLayers);
});

function GetSelection(layer){
    var count = allPoints.getLayers().length;
    console.log(count +" Sites");  //says 17
    var drawList = editableLayers.getLayers().length;
    console.log(drawList +" Polys");  //Says 1

    if (editableLayers.getLayers().length >0){
        var fcpt = turf.featurecollection(allPoints);
        console.log(fcpt);  // says 17
        var fcpoly = turf.featurecollection(editableLayers);
        console.log(fcpoly);  // fails as undefined
           //var ptsWithin = turf.within(fcpt,editableLayers);
        var ptsWithin = turf.within(fcpt,fcpoly);
        console.log(ptsWithin);  //never gets this far.

    };
};

有什么想法或建议吗?

【问题讨论】:

  • fcpolyn 中的错字,应该是fcpoly
  • @ghybs,谢谢,我修正了错字,但仍然有同样的问题,没有功能的功能集合。

标签: javascript leaflet turfjs


【解决方案1】:

turf.featurecollection 需要一组 GeoJSON 功能,而不是像 allPointseditableLayers 变量那样的传单层组。

同样,turf.within 期望 2 个 GeoJSON 特征集合作为参数,而不是传单层组。

所以你可以直接尝试:

var ptsWithin = turf.within(allPoints.toGeoJSON(), editableLayers.toGeoJSON());

【讨论】:

  • 我没有足够的分数来投票给你的答案。今天下午我走在了正确的轨道上,但找不到正确的代码,你有一个简单的解决方法。谢谢。
【解决方案2】:

@ghybs 是对的,这是 Leaflet 和 turf 的区别,虽然点没问题,但多边形没有过来。将草皮传递给 GeoJson 多边形信息允许它工作。

工作副本:

map.on('draw:created', function (e) {
    featureGroup.clearLayers();
    layer = e.layer;
    featureGroup.addLayer(layer);
    GetSelection(featureGroup);
});

function GetSelection(layer){

    var shape2 = allPoints.toGeoJSON()  //All facilities
    var ptsWithin = turf.within(shape2, layer.toGeoJSON());

        alert('Found ' + ptsWithin.features.length + ' features');  
        alert("results "+JSON.stringify(ptsWithin));
};

【讨论】:

  • 很高兴分享您的发现和解决方案! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 2020-12-26
  • 2015-12-13
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多