0

I am trying to add attributes to a layer. I have a select function that finds all points within 10 miles of a feature. If the point is within the radius distance I add it to an array and make another "Selected Set" out of it for a map and report. However they now want to sort the selected point based on distance from. How do I push this vale to the selected point?

var selPts = [];
    function SelectPoints(lat,lon){
        var dist = 10; //Get Radius distance
        xy = [lat,lon];  //center point of circle
        var theRadius = dist * 1609.34  //1609.34 meters in a mile 

        selPts.length =0;  //Reset the array if selecting new points

        sites.eachLayer(function (layer) {
            // Lat, long of current point as it loops through.
            layer_lat_long = layer.getLatLng();

            // Distance from our marker To current point in meters
            distance_from_centerPoint = layer_lat_long.distanceTo(xy);

            // See if pt is within radius, add the to array
            if (distance_from_centerPoint <= theRadius) {
                 selPts.push(layer.feature);  
                 // how do I push distance_from_centerPoint into the feature properties?
            }
        });
2
  • I think you might be talking about appending attributes from one layer to another? If so, you might be looking for the turf.js library to help with the GIS work. Here is a short video on Spatial Analysis in Leaflet using Turf.js that you may find useful. Mar 22 '19 at 19:48
  • Thanks for the link, I’m not actually transferring an attribute I’m grabbing a variable value and trying to plug it into the selected point. I’m able to transfer attributes with Turf intersect, but that doesn’t calculate distance from the point. Mar 22 '19 at 22:06
1

Since I wanted to just add a distance field (dist), and I had the feature in my loop, I just added "layer.feature.properties.dist = my Distance Value" then when I pushed the layer.feature it went along.

    sites.eachLayer(function (layer) {
        // Lat, long of current point as it loops through.
        layer_lat_long = layer.getLatLng();

        // Distance from our marker To current point in meters
        distance_from_centerPoint = layer_lat_long.distanceTo(xy);

        // See if pt is within radius, add the to array
        if (distance_from_centerPoint <= theRadius) {

           layer.feature.properties.dist = (distance_from_centerPoint / 1609.34); //meters to miles

             selPts.push(layer.feature);  

        }
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.