Select to view content in your preferred language

Fetch Attribute From Nearby Feature on Field Maps

1156
1
08-09-2022 10:46 AM
KatiePiper
Occasional Contributor

Hi,

I have been trying to do the fetch attributes from nearby features arcade code that I have seen on blog, github and various other community posts by @Anonymous User . I cannot get it to work for my field map. 

 

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

// Get the parcels layer
var parcels = FeatureSetById($map, /* Yurok Addresses */ "17d69e9a280-layer-9")

// Buffer the current location and intersect with parcels
var bufferedLocation = Buffer($feature, 3000, '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) {
    Push(featuresWithDistances, 
        {
            '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']['ADDNUM']} ${closestFeatureWithDistance['feature']['ADDRESSNAM']}`

 

 I get the error "failed to calculate" every time. We are surveying for water tank placements and trying to make it easier on our fieldworkers. If all else fails, I will have them write it in or create a domain but would prefer to have it auto populate. 

 

Should I change "ADDNUM" and "ADDRESSNAM" to something within my layer? Attached is a list of field names for my Address layer. I could just use "Full Address" or "Label" instead of "Address_number" and "StreetName" 

 

I read on one post that it could be because of the spatial reference but that was supposed to be fixed in the last release and I am not getting that same error message. I am still fairly new at Arcade but was able to implement a code for the child table auto population from this parent layer. If I can get this next one to work correctly, it would be incredibly useful in other projects. 

0 Kudos
1 Reply
anonymous55
Frequent Contributor

Hello

You just need to change this line of the code

return `${closestFeatureWithDistance['feature']['ADDNUM']} ${closestFeatureWithDistance['feature']['ADDRESSNAM']}`

To this

return `${closestFeatureWithDistance['feature']['Address_number']} ${closestFeatureWithDistance['feature']['StreetName']}`

also you need to paly with buffer number to get result. 

0 Kudos