Select to view content in your preferred language

Issue with Arcade Attribute Rule Script - Not Returning Expected Values

179
2
04-22-2024 06:08 AM
StefanAngerer
Occasional Contributor

Hi everybody,

I'm attempting to develop an Arcade Rule that, given a feature geometry, identifies the closest address feature within a specified buffer distance. The script should then update the created or updated features attributes with the closest attributes of the closest address feature. However, as so often, I'm encountering an issue with the script.

heres the script im using:

var featureGeometry = Geometry($feature)

if(isEmpty(featureGeometry)){
 return Null
}

var addressesLN = FeatureSetByName($datastore, 'VG_ADDRESSES_LN', ['*'], true)
var addressesPT = FeatureSetByName($datastore, 'VG_ADDRESSES_LN', ['*'], true)
var addressesAR = FeatureSetByName($datastore, 'VG_ADDRESSES_LN', ['*'], true)

var bufferSize = 20 
var bufferPolygon = Buffer(featureGeometry, bufferSize, 'meters')

var bufferIntersectingLN = Intersects(addressesLN, featureGeometry)
var bufferIntersectingPT = Intersects(addressesPT, featureGeometry)
var bufferIntersectingAR = Intersects(addressesAR, featureGeometry)

var mostInformativeAddresses = Null 

if(!isEmpty(bufferIntersectingPT)){
 mostInformativeAddresses = bufferIntersectingPT
} else if (!isEmpty(bufferIntersectingLN)){
 mostInformativeAddresses = bufferIntersectingLN
} else if (!isEmpty(bufferIntersectingAR)){
 mostInformativeAddresses = bufferIntersectingAR
} else {
 return
}

if(isEmpty(mostInformativeAddresses)){
 return
}

var closestFeature = Null
var closestDistance = 9999

for(var addressFeature in mostInformativeAddresses){
 var currentDistance = Distance(featureGeometry, Geometry(addressFeature), 'meters') 
 if (currentDistance < closestDistance){
 closestFeature = currentFeature
 closestDistance = currentDistance
}
}

return{
	"result":{
		"attributes":{
			"ADM_KEY_COMM": closestFeature.ADM_KEY_COMM, 
			"ADM_KEY_STREET": closestFeature.ADM_KEY_STREET, 
			"ADM_KEY_COMMPART": closestFeature.ADM_KEY_COMMPART, 
			"ADM_KEY_ADDRESS": closestFeature.ADM_KEY_ADDRESS, 
			"ADDRESS_TEXT": closestFeature.ADDRESS_TEXT
		}
	}
}

 

The script validates without errors, but it doesn't return any values, even though there is a valid address feature within a reasonable distance.

Can anybody help me find the issue here or suggest any improvements to the script?

Any assistance would be greatly appreciated!

Thanks,

Stefan

0 Kudos
2 Replies
TedHoward2
Esri Contributor

Hi Stefan,

It does not appear that you are using the buffer polygon created from $feature. Try updating the Intersects calls to use the buffer.

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

All three featuresets are pointing to the same class, you want to fix that.  I revised the logic, code below.  I think you want something like this.

var featureGeometry = Geometry($feature);

if (isEmpty(featureGeometry)) {
    return null;
}

var bufferSize = 20;
var bufferPolygon = Buffer(featureGeometry, bufferSize, 'meters');

function closest_item(fs) {

    var closestFeature = null;
    var closestDistance = INFINITY;

    for (var addressFeature in fs) {
        var currentDistance = Distance(featureGeometry, Geometry(addressFeature), 'meters')
        if (currentDistance < closestDistance) {
            closestFeature = currentFeature;
            closestDistance = currentDistance;
        }
    }
    return closestFeature
}

var addressesPT = FeatureSetByName($datastore, '<FILL OUT THE CORRECT CLASS>', ['*'], true);
var bufferIntersectingPT = Intersects(addressesPT, bufferPolygon);
var closest_feat = closest_item(bufferIntersectingPT);


if (IsEmpty(closest_feat)) {
    var addressesLN = FeatureSetByName($datastore, '<FILL OUT THE CORRECT CLASS>', ['*'], true);
    var bufferIntersectingLN = Intersects(addressesLN, featureGeometry);
    closest_feat = closest_item(bufferIntersectingLN);
}

if (IsEmpty(closest_feat)) {
    var addressesAR = FeatureSetByName($datastore, '<FILL OUT THE CORRECT CLASS>', ['*'], true);
    var bufferIntersectingAR = Intersects(addressesAR, featureGeometry);
    closest_feat = closest_item(bufferIntersectingAR);
}

if (isEmpty(closest_feat)) {
    return;
}


return {
    "result": {
        "attributes": {
            "ADM_KEY_COMM": closest_feat.ADM_KEY_COMM,
            "ADM_KEY_STREET": closest_feat.ADM_KEY_STREET,
            "ADM_KEY_COMMPART": closest_feat.ADM_KEY_COMMPART,
            "ADM_KEY_ADDRESS": closest_feat.ADM_KEY_ADDRESS,
            "ADDRESS_TEXT": closest_feat.ADDRESS_TEXT
        }
    }
};
0 Kudos