Select to view content in your preferred language

Arcade Expressions (ArcGIS Online) to display nearest ploygon Field Attribute

693
2
Jump to solution
01-18-2024 02:35 AM
MohdZukhairiAbdLatef
New Contributor II

Hello, I am trying to find nearest feature distance and display its attribute name through pop-ups using Arcade expression in the WebMap (Portal for ArcGIS).

I found below expression from the esri link;

How To: Display the Distance of the Nearest Point Feature Using an Arcade Expression in Po (esri.com...

I managed to configure pop-ups and display nearest distance, but I could not figure out how to display its attribute from the result of the nearest distance. 

Below is my scenario:

Layer 1: BigArea (polygon) [with one field name called "HOUSING PROJECT NAME"]

Layer 2: Land Parcel (Polygon)

I want to display distance and HOUSING PROJECT NAME when I click to any Land Parcel layer. It could me adding another attribute field or using the same distance field.

 

My script I found and modified from ESRI as below:

var BigArea = FeatureSetByName($map,"BigArea");

var buf = Buffer($feature, 2000000, "meter");

var fs_filtered = Intersects(BigArea, buf);

var cnt = Count(fs_filtered);

var min_dist = 2000000;

 

if (cnt > 0) {

    for (var f in fs_filtered) {

        var dist = Distance($feature, f);

        if (dist < min_dist) {

            min_dist = dist;

        }

    }

} 

return round(min_dist, 3);

Any suggestions are most welcome.

 

Regards

Zukh

 

0 Kudos
1 Solution

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

Dont call Count(), this causes an unnecessary query.  Just loop over the results.

 

try this

 

var BigArea = FeatureSetByName($map,"BigArea");
var buf = Buffer($feature, 2000000, "meter");

var fs_filtered = Intersects(BigArea, buf);

var min_dist = Infinity;
var closest_feat = null;
for (var f in fs_filtered) {

    var dist = Distance($feature, f);

    if (dist < min_dist) {
        closest_feat = f;
        min_dist = dist;

    }
}
if (IsEmpty(closest_feat)){
    return "There are no housing projects withing 2000000 meters"
}
return `${closest_feat.HOUSING PROJECT NAME} is ${round(min_dist, 3)} meters away.`;

 

View solution in original post

0 Kudos
2 Replies
MikeMillerGIS
Esri Frequent Contributor

Dont call Count(), this causes an unnecessary query.  Just loop over the results.

 

try this

 

var BigArea = FeatureSetByName($map,"BigArea");
var buf = Buffer($feature, 2000000, "meter");

var fs_filtered = Intersects(BigArea, buf);

var min_dist = Infinity;
var closest_feat = null;
for (var f in fs_filtered) {

    var dist = Distance($feature, f);

    if (dist < min_dist) {
        closest_feat = f;
        min_dist = dist;

    }
}
if (IsEmpty(closest_feat)){
    return "There are no housing projects withing 2000000 meters"
}
return `${closest_feat.HOUSING PROJECT NAME} is ${round(min_dist, 3)} meters away.`;

 

0 Kudos
MohdZukhairiAbdLatef
New Contributor II

It works! Thanks for sharing @MikeMillerGIS 

0 Kudos