Select to view content in your preferred language

Find Nearest in Field Maps Form

386
7
Jump to solution
04-03-2025 05:04 PM
Scott_Gowan
Occasional Contributor

Hey all,

I’m writing a calculated expression to populate a field in a Field Maps form. The function is to find the nearest creek feature and extract a value to populate the field in my new point. All new points will be very close to a creek.  As a matter of fact, the collection tech may be standing directly in the water so finding a close feature isn’t an issue.  When I run the script within the form’s calculated expression the result is null, not an error, which leads me to believe it’s executing properly. Since I haven't actually digitized a point yet there’s no location from which to calculate.  However, when I test collecting a point in field maps the calculation fails.  Any pointers would be greatly appreciated.  Thanks in advance. 

 

 

// Get the geometry of the new digitized point

var pointGeometry = Geometry($feature);

var nearestCreek = null;

var nearestDistance = null;

 

// Access the Creek Reaches 2025 feature set

var creeks = FeatureSetByName($map, "Inspection 2025/Creek Reaches 2025");

 

// Loop through each creek to find the nearest one

for (var creek in creeks) {

    var creekGeometry = Geometry(creek);

    var distance = Distance(pointGeometry, creekGeometry);

   

    // Check if this is the closest creek found so far

    if (nearestDistance == null || distance < nearestDistance) {

        nearestDistance = distance;

        nearestCreek = creek;

    }

}

 

// Extract the desired field value (e.g., REACH) from the nearest creek feature

var reachName = null;

if (nearestCreek != null) {

    reachName = nearestCreek.REACH; // Ensure the field name matches the one in your dataset

}

 

// Return the reach name to populate the Reach_Name field

return reachName;

0 Kudos
1 Solution

Accepted Solutions
MarkBockenhauer
Esri Regular Contributor

I tried your script, it works.   I just changed the creek layer name and the field name to match my test data.

Yes, it will be null if your point features does not exist, but with features in the hosted feature service, it works.

MarkBockenhauer_0-1743775015136.png

 

 

View solution in original post

0 Kudos
7 Replies
MarkBockenhauer
Esri Regular Contributor

I tried your script, it works.   I just changed the creek layer name and the field name to match my test data.

Yes, it will be null if your point features does not exist, but with features in the hosted feature service, it works.

MarkBockenhauer_0-1743775015136.png

 

 

0 Kudos
Scott_Gowan
Occasional Contributor

Hey Mark,

Thanks so much for the quick reply and confirming the script is correct.  For me it "fails" when I insert a point in Field Maps so perhaps I have a simple improper setting in the form.  Great to know that the script is correct though so I can eliminate that issue. 

Thanks again,  Scott 

0 Kudos
MarkBockenhauer
Esri Regular Contributor

just occurred to me I did make one other change, The variable name 'distance', I modified to distance1 , apologies for omitting that bit.   you should see that as an error when authoring the form.

 

MarkBockenhauer_1-1743776628303.png

 

0 Kudos
Scott_Gowan
Occasional Contributor

Interesting,  I didn't get that flagged as an error.

 

0 Kudos
Scott_Gowan
Occasional Contributor

Hey Mark,  Just to confirm.  In your scenario above were you adding a new point or updating an existing point?

0 Kudos
MarkBockenhauer
Esri Regular Contributor

both work,  new and updating existing.  I have shared my test map public for the moment, if you would like to take a look  https://www.arcgis.com/home/item.html?id=94c10beba7fb4a87b3cebed1750355c0

0 Kudos
Scott_Gowan
Occasional Contributor

Got it.  Thanks so much for your effort.   This will be useful in so many other apps.   

Scott 

0 Kudos