2

我在传单中有一个包含 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.

    };
};

有什么想法或建议吗?

4

2 回答 2

2

turf.featurecollection需要一组GeoJSON Features,而不是像你的allPointseditableLayers变量这样的 Leaflet Layer Group 。

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

所以你可以直接尝试:

var ptsWithin = turf.within(allPoints.toGeoJSON(), editableLayers.toGeoJSON());
于 2016-03-15T20:23:16.263 回答
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));
};
于 2016-03-16T18:49:38.607 回答