javascript - Leaflet and Turf.js Points within poly - Stack Overflow

I have a simple map with 17 points (GeoJSON) in leaflet, and using the draw tool, I create a polygon to

I have a simple map with 17 points (GeoJSON) in leaflet, and using the draw tool, I create a polygon to use to select the points within th polygon.

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.

    };
};

Any ideas or suggestions?

I have a simple map with 17 points (GeoJSON) in leaflet, and using the draw tool, I create a polygon to use to select the points within th polygon.

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.

    };
};

Any ideas or suggestions?

Share Improve this question edited Mar 15, 2016 at 19:56 Bill Chappell asked Mar 15, 2016 at 18:25 Bill ChappellBill Chappell 3116 silver badges22 bronze badges 2
  • Typo in fcpolyn, should have been fcpoly? – ghybs Commented Mar 15, 2016 at 18:31
  • @ghybs, thanks, I fixed the typo and still have the same issue, a feature collection without features. – Bill Chappell Commented Mar 15, 2016 at 19:57
Add a ment  | 

2 Answers 2

Reset to default 3

turf.featurecollection expects an array of GeoJSON Features, not a Leaflet Layer Group like your allPoints and editableLayers variables.

Similarly, turf.within expects 2 GeoJSON Feature Collections as arguments, not Leaflet Layer Groups.

So you could probably try directly:

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

@ghybs was right, it was a difference between Leaflet and turf, while the points were OK, the polygon did not e over. Passing turf the GeoJson the polygon info allowed it to work.

Working copy:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745322979a4622526.html

相关推荐

  • javascript - Leaflet and Turf.js Points within poly - Stack Overflow

    I have a simple map with 17 points (GeoJSON) in leaflet, and using the draw tool, I create a polygon to

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信