Leaflet 和 Turf.js 内聚点

Leaflet and Turf.js Points within poly

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

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.

    };
};

有什么想法或建议吗?

turf.featurecollection 需要一组 GeoJSON 特征,而不是像您的 allPointseditableLayers 变量那样的 Leaflet 图层组。

同样,turf.within 需要 2 个 GeoJSON 特征集合作为参数,而不是 Leaflet 图层组。

所以你可以直接试试:

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

@ghybs说的对,就是Leaflet和turf的区别,点还可以,多边形没过来。将 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));
};