Turf.within is not working

回眸只為那壹抹淺笑 提交于 2019-12-25 09:11:51

问题


I have a function to get the nearest points (farmers) to a specific point(customer),i made a buffer around the customer with distance from the user then i am using turf.within to get all the points(farmers) inside this buffer. It works fine if i give the array of coordinates in the feature collection in the variable searchWithin,but when i gave it as a variable (array) which is in this case the points of the buffer bufresult it doesn't work.

function Find_Nearest()
{
//getting the orderid to get the customer id
var select1 = document.getElementById('OrderId');
var Order_ID = select1.options[select1.selectedIndex].text;
var Cust_Name = document.getElementById('CustName').textContent;

//to check the distance entered by user
var select = document.querySelector('input[name="optradio"]:checked').value;
var dist=document.getElementById('dist').value; 

if (select == "EnterRange") 
{
    if (dist == null || dist == "" || isNaN(dist) ) 
    {
        alert("Please enter a valid range");
        document.getElementById('dist').focus() ;   
    }
    else { distance=dist;}
}
else { distance=select;}

$.ajax({
    type:"POST",
    url:"CustomerID_geojson.php",
    data:{'Cust_Name': Cust_Name} ,
    dataType: 'json',
    success: function (response) 
    {
        var unit = 'kilometers';
        var buffered = turf.buffer(response, distance, unit);
        var bufresult = new Array();
        bufresult.push(buffered.geometry.coordinates);

        $.ajax({
        type: "POST",
        url: 'allfarmers_geojson.php',
        dataType: 'json',
        success: function (data) 
        {
            var searchWithin = {
              "type": "FeatureCollection",
              "features": [
                {
                  "type": "Feature",
                  "properties": {},
                  "geometry": {
                    "type": "Polygon",
                    "coordinates": bufresult //the problem is here
                //    [[
                //      [126.5443329669793,8.71772],
                //      [126.17866648348965,9.34375583885896],
                //      [125.44733351651035,9.34375583885896],
                //      [125.08166703302071,8.71772],
                //      [125.44733351651035,8.091684161141039],
                //      [126.17866648348965,8.091684161141039],
                //      [126.5443329669793,8.71772]
                //    ]]
                  }
                }
              ]
            };

            console.log(bufresult);
            console.log(searchWithin);

            var ptsWithin = turf.within(data, searchWithin);
            console.log(ptsWithin);         
            geojsonLayer = L.geoJson(ptsWithin).addTo(mymap);
            mymap.fitBounds(geojsonLayer.getBounds());
        }
        });
     }
});
}

回答1:


Have you tried replacing

var bufresult = new Array(); and bufresult.push(buffered.geometry.coordinates);

with

bufresult = buffered.geometry.coordinates

I think the issue may be that you are initializing an array (bufresult) and then pushing the buffered geometry coordinates into that array. You could actually just skip the bufresult variable all together and just plug the buffered.geometry.coordinates variable into where you're using bufresult. Let me know if this doesn't work for you. Could you also include the error that you're getting?



来源:https://stackoverflow.com/questions/40845108/turf-within-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!