Select to view content in your preferred language

Return Not Detected for Address field insted of Null values for arcade expression

685
3
09-15-2022 07:52 AM
anonymous55
Frequent Contributor

Hello

I have this code which is from Esri blog and I am trying to return exact address values from parcel data (MapPLUTO) but I don't want to retune Null values instead I want return Not Detected for Address field for cases which code can find address

 

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

// Get the parcels layer
var parcels = FeatureSetByName($map,"MapPLUTO")

// Buffer the current location and intersect with parcels
var bufferedLocation = Buffer($feature,50, '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 "Not Detected"}

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

 

 

anonymous_0-1663253400011.png

 

Tags (2)
0 Kudos
3 Replies
DavidSolari
Frequent Contributor

It looks like the only exit point that can return a null value is the final return for the address itself. You can coerce a null address to your string like so:

var address = closestFeatureWithDistance['feature']['Address']
return Iif(IsEmpty(address), "Not Detected", address)

I think the issue is you're checking if the entire "closestFeatureWithDistance" object is null on line 33 rather than just the Address attribute.

anonymous55
Frequent Contributor

Thanks @DavidSolari 
this is working great another question;

some of my features already have address but when I am using this expression is updating address field which I don't want to happen? do you know how can we limit these edits?

0 Kudos
JohannesLindner
MVP Frequent Contributor

You can check if candidateParcels is an empty Featureset (no parcels intersected).

Two ways to do that (both after line 9 )

 

// intuitive, but slow for large datasets (should be OK here)
if(Count(candidateParcels) == 0) {
    return "Not Detected"
}

// not as intuitive, but much faster
if(First(candidateParcels) == null) {
  return "Not Detected"
}

 


Have a great day!
Johannes