Return nearest feature within same feature layer using Arcade

1162
3
Jump to solution
02-02-2021 12:53 PM
AFackler_NAPSG
Occasional Contributor

I am trying to create an Arcade script that, within the pop-up, will return the next closest feature within the same layer. In my case, if I click on a wildfire point, it will tell me what the next closest wildfire point is. I have has some success changing up the script found in lines 30-44 here, but what I have found is that my current script returns the point that is furthest from the selected point within the search distance. Any guidance on what I can do?

 

var searchDistance =50;
var points = $layer
var closestPoint = Intersects(points, BufferGeodetic($feature, searchDistance, "Miles"));
var minDistance = Infinity
for(var listing in closestPoint){
    var facilityDistance = Distance(listing, $feature, "miles");
    if (facilityDistance < minDistance){
        closestPoint = listing;
            return closestPoint.iaName
    }
};

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi @AFackler_NAPSG ,

 

You have to update the minimum distance and as @KenBuja  pointed out you have to put the return outside the for loop.

 

var searchDistance = 50;
var points = $layer;
var CurrentiaName = $feature.iaName;
var closestPoints = Intersects(points, BufferGeodetic($feature, searchDistance, "Miles"));
var minDistance = Infinity;
for(var pnt in closestPoints){
    var facilityDistance = DistanceGeodetic(pnt, $feature, "miles");
    if (facilityDistance < minDistance){
        if (CurrentiaName != pnt.iaName) {
            closestPoint = pnt;
            minDistance = facilityDistance;
        }
    }
};
return closestPoint.iaName;

 Furthermore, you have to avoid returning the same input feature. You could do this by checking for an ID (I used the iaName in this case. 

View solution in original post

3 Replies
KenBuja
MVP Esteemed Contributor

Give this a try

var searchDistance =50;
var points = $layer
var closestPoints = Intersects(points, BufferGeodetic($feature, searchDistance, "Miles"));
var closestPoint;
var minDistance = Infinity;
for(var listing in closestPoints){
    var facilityDistance = Distance(listing, $feature, "miles");
    if (facilityDistance < minDistance){
        closestPoint = listing;
    }
};
return closestPoint;
0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @AFackler_NAPSG ,

 

You have to update the minimum distance and as @KenBuja  pointed out you have to put the return outside the for loop.

 

var searchDistance = 50;
var points = $layer;
var CurrentiaName = $feature.iaName;
var closestPoints = Intersects(points, BufferGeodetic($feature, searchDistance, "Miles"));
var minDistance = Infinity;
for(var pnt in closestPoints){
    var facilityDistance = DistanceGeodetic(pnt, $feature, "miles");
    if (facilityDistance < minDistance){
        if (CurrentiaName != pnt.iaName) {
            closestPoint = pnt;
            minDistance = facilityDistance;
        }
    }
};
return closestPoint.iaName;

 Furthermore, you have to avoid returning the same input feature. You could do this by checking for an ID (I used the iaName in this case. 

AFackler_NAPSG
Occasional Contributor

Thank you @KenBuja and @XanderBakker ! 

0 Kudos