ArcGIS Pro

$originalFeature - New Attribute Rules Arcade Global

Attribute Rules are scripts that can be added to a dataset and executed on certain events such as when a feature is inserted, updated or deleted. The limitation prior to  ArcGIS Pro 2.5 and ArcGIS Enterprise 10.8 is any update you make to the feature will trigger the attribute rule, which might not be a desired behavior.

In this blog we will go through the new Arcade global $originalFeature in the attribute rules profile and see how it can help mitigate this problem.

 

Take a look at these two attribute rules, the first one calculates the distance of the nearest point and the second one calculates a uniqueName every time the Name field is updated by appending a guid.

//get the feature geometry
var g1 = Geometry($feature)
//get the object id
var oid = $feature.objectid
//get the entire class 
var fsPointClass  = FeatureSetByName($datastore, "pointClass", ['objectid'], true) 
//filter all features except this feature (we don't want the current feature to be returned)
var allPointsExceptThis  = Filter(fsPointClass  ,  "objectid <> @oid")
//return if no rows 
if (count(allPointsExceptThis) == 0) return 0;
//calculate the minimum distance
var distances = [];
var c = 0;
for (var f in allPointsExceptThis) {
   var d =  Distance(g1, Geometry(f), 'feet')
   distances[c++] = d;
}
//return 
return min(distances)
return $feature.Name + Guid() 

The scripts are executed regardless of any edit and that might not be desirable. Ideally, we want the distance to be calculated only when the feature geometry is updated (e.g. a feature is moved) and for the uniqueName to get calculated with a new guid only when the Name field is updated.

Meet $originalFeature.

What is the $originalFeature?

The $originalFeature is a new Arcade global that presents the state of the feature before the edit giving the script author flexibility to compare the current values $feature with the original values of the feature.

Let us rewrite the distance attribute rule so that we only execute the script if the geometry has changed.

//check if the geometry has changed or not, if it didn't simply return
if ( Equals(Geometry($feature), Geometry($originalFeature))) return $feature.distance

//get the feature geometry
var g1 = Geometry($feature)
//get the object id
var oid = $feature.objectid
//get the entire class 
var fsPointClass  = FeatureSetByName($datastore, "pointClass", ['objectid'], true) 
//filter all features except this feature (we don't want the current feature to be returned)
var allPointsExceptThis  = Filter(fsPointClass  ,  "objectid <> @oid")
//return if no rows 
if (count(allPointsExceptThis) == 0) return 0;
//calculate the minimum distance
var distances = [];
var c = 0;
for (var f in allPointsExceptThis) {
   var d =  Distance(g1, Geometry(f), 'feet')
   distances[c++] = d;
}
//return 
return min(distances)

We can do the same for the UniqueName attribute rule by checking if the Name field has been updated or not and return the current unqiueName.

//if the name didn't change don't generate a new uniquename guid
if ($originalFeature.Name == $feature.Name) return $feature.uniqueName
return $feature.Name + Guid() 

Free ArcGIS Pro Trial

About the author

Product Engineer at Esri, Author of several GIS books and Software Engineering Content Creator on YouTube and a podcast host.

Connect:

Leave a Reply

Please Login to comment

Next Article

HOW TO nominate your stuff to the ArcGIS Living Atlas of the World

Read this article