Potential bug in calculating point attributes from nearest parcel/polygon

06-30-2022 06:26 AM
New Contributor III

Hello! I am trying to output the address of the nearest parcel to automate data entry in a FM form. After modeling an expression from this example (and a few others), I've come up with the below expression. When testing in the arcade expression builder window, it seems to work as it should. When tested as an attribute expression in Map Viewer and added to a popup, it again works flawlessly. The address of the nearest parcel is being displayed in every testing scenario I try.

But, when I use it in the form in the FM mobile app (android and iOS), I get some really odd behavior. It displays an address of a somewhat nearby parcel, but it is never the closest. In addition, I changed the expression to also output the distance, and I am getting unreasonably large distances recorded. 

Can someone help me to understand whether or not this is a bug, limitation, or if I am simply doing something wrong in my expression?

Thanks in advance! Here it is:


var searchDistance = 100;
var Parcels = Filter(FeatureSetByName($map,"Parcels",['propertylo']),'propertylo IS NOT NULL');
var closestParcels = Intersects(Parcels, Buffer($feature, searchDistance, "feet"));
var cnt = Count(closestParcels)
Console("Nearby parcels: " + cnt);
var minDistance = 150
var closestParcel;

For (var f in closestParcels){
    var parcelDistance = Distance(f, $feature, "feet");
    if (parcelDistance < minDistance && !IsEmpty(f.propertylo)) {
        closestParcel = f.propertylo;
        minDistance = parcelDistance;
    Console(f.propertylo + ": " + " " + parcelDistance + " ft");
    //Return f.propertylo + ": " + " " + parcelDistance
Console("Closest: " + closestParcel + " (" +parcelDistance+" ft)");
return closestParcel+" (" +parcelDistance+" ft" + ", closest of " + cnt + " nearby)";



Here it is working properly in Map Viewer popup:


...And improperly in FM mobile (Android):






0 Kudos
1 Reply
New Contributor III

@Anonymous User 

After posting I saw this very similar scenario described in this post. I tried this expression out as a model and came up with what you see below. For some reason, this works when the feature (tree site) is directly on top of a large parcel, but when it is next to, or on top of a smaller, residential parcel, I only get a null value returned. 

// If feature doesn't have geometry return null
//if (IsEmpty(Geometry($feature))) { return null }

// Get the parcels layer
var parcels = Filter(FeatureSetByName($map,"Parcels",['propertylo']),'propertylo IS NOT NULL')

// Buffer the current location and intersect with parcels
var bufferedLocation = Buffer($feature, 100, 'feet')
var candidateParcels = Intersects(parcels, bufferedLocation)

// Calculate the distance between the parcel and the current location
// Store the feature and distance as a dictionary and push it into an array
var featuresWithDistances = []
for (var f in candidateParcels) {
            'distance': Distance($feature, f, 'feet'),
            'feature': f

// Sort the candidate parcels by distance using a custom function
function sortByDistance(a, b) {
    return a['distance'] - b['distance']
var sorted = Sort(featuresWithDistances, sortByDistance)

// Get the closest feature
var closestFeatureWithDistance = First(sorted)

// If there was no feature, return null
//if (IsEmpty(closestFeatureWithDistance)) { return null }

// Return the address
return `${closestFeatureWithDistance['feature']['propertylo']}`

I also tried this as a popup expression, and it also returns null values (method 2; method 1 uses the expression in the original post above).



Any thoughts on why either of these is not working?

0 Kudos