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;
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
Solved! Go to Solution.
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.`;
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.`;
It works! Thanks for sharing @MikeMillerGIS