Calculation rules can be used to provide real-time validation.
By coupling ArcGIS Pro with some creative Arcade expressions, the editing and QA/QC experience can be greatly improved by adding attribute rules to a geodatabase layer. In this article we will focus on how to harness calculation-type attribute rules to perform validation checks on the existing data and write results to a text field.
Note: This article assumes the reader has access to ArcGIS Pro and general-use knowledge of the application, basic knowledge about Arcade expressions and how to assign Attribute Rules to a feature class. See our other blogs for more knowledge on these topics: ArcGIS Pro, Arcade and Attribute Rules.
Let’s Dive In!
Attribute Rule Run-down
Of the three types of Attribute Rules (Calculation, Constraint and Validation), calculation rules are by far the most versatile because they can return any type of value requested using Arcade. The other two types of rules, Constraint and Validation, can only return boolean values. Constraint rules use boolean values to restrict active editing and validation rules use the boolean values to return designated errors using the Error Inspector. The versatility of calculation rules makes them a perfect candidate to accomplish some basic data checks on features in our file geodatabase. In today’s blog post we will use a calculator rule to perform QA/QC checks on a feature, and return one or more error codes to a text field.
Getting Started
To get started you’ll need to open a Project in ArcGIS Pro, and have a file geodatabase with at least one feature layer. If the layer does not have a free text field to hold the returned results from our calculation, add a text field with the default length of 255 characters. Once we have a text field to assign the rule to, we can start to build the simple Arcade expression to check our data for errors.
Evaluate the Data
In the example today I will reference some Address data and show some simple errors that could arise during the editing phase and need to be identified and fixed during the QA/QC process. Some of the easiest errors to catch would be a feature with Country and State values that were not US and IL. Another set of errors to look for would be a feature that has no value for required fields like Address Number or Street Name. The image below shows some general errors that need to be rectified involving the fields of: Country, State, Address Number and Street Name.
Arcade Expression
Now that we have “addressed” some of the potential errors in our data, we can start to write an Arcade expression to check for those errors and return a specific code for each error to the Error Codes field. In general, we need to write an Arcade expression that will check for missing or unwanted values in certain fields, then add an error code to a list which can be read into the Error Codes field. The first two fields we want to check are the Country and State field. This is an easy check to make because the value for Country must be “US”, and the value for State must be “IL.”
// Name: Using Arcade for Error Checks // Create variables for fields needing checked var country = $feature["country"]; var state = $feature["state"]; var addNum = $feature["add_number"]; var stName = $feature["st_name"]; // Initialize array and counter var errors = [];var i = 0; // If Country is not "US" and add error code to array if(country != "US"){errors[i++]="104: Country not US"} // If State is not "IL" and add error code to array if(state != "IL"){errors[i++]="105: State not IL"} // If State is not "IL" and add error code to array if((addNum == 0) || (IsEmpty(addNum))){errors[i++]="110: AddNumber blank/0"} // If StName is blank or Null add error code to array if(IsEmpty(stName)){errors[i++]="107: StName blank"} // Concatenate error codes into readable string var vals = (Concatenate(errors, ", ")); // If vals is somehow empty, return No Errors, else return the error codes if (IsEmpty(vals)){return "No Errors"}else{return vals}
Add the code above to a text editor to follow along by line number.
The code block above shows an example of an Arcade expression that could be used to accomplish this check of the two fields, and add error codes to a list if necessary. On lines 4-7, variables for the field values of Country, State, Address Number and Street Name are created. On line 10, the empty list (i.e. array) is initialized, along with a counter that is needed to add items to the list. Both lines of 12 and 14 are doing the same thing, but for different fields: if the value in the field for the current feature is not equal to the value we want, an error code will be added to the list we initialized on line 10 above. On line 16, if the Address Number value is 0 or blank an error will be added to the list. On line 20, the values pushed into the array are concatenated into one string separated by commas and stored in a variable named vals. On line 22, the expression evaluates the value for vals, then does one of two things. If the list is empty (The IsEmpty function returns true/false) then a string value of “No Errors” will be written into the Error Codes field in our attribute table. If vals is not empty, then the string of concatenated error codes is written into the Error Codes field.
Testing the Arcade Expression
In order to test our expression, we can harness the Field Calculator in ArcGIS Pro. After opening the Attribute Table for the Address Points layer we can right click on the Error Codes field and choose to start the Field Calculator. Within the Field Calculator window, we can choose Arcade as the language, then copy and paste the expression from above into the window.
Creating the Attribute Rule
Now that we have successfully created an Arcade expression, we can leverage that expression inside of a Immediate Calculation Attribute Rule that will populate error codes every time a feature is inserted (created) or updated (attribute/geometry edit).
In the ArcGIS Pro project, with the feature layer added to a Map we can access the Attribute Rules pane for that layer and add the Calculation rule. The rule type will be Immediate, the rule’s name will be “Calculate Error Codes” and the field it will run on will be our Error Codes field. The Expression will mirror the expression outlined above, and the triggers will be both Insert and Update.
Now that we’ve set up all the parameters needed for our attribute rule, every time we add a feature or edit an existing feature, the Error Codes field will be populated with a new list of error codes, or have a value of “No Errors” if there are none present.
Note: Any existing features will not be checked for errors until they are edited. To check existing features without performing any edits, use the same Arcade expression in the Field Calculator to populate the Error Codes field.
Test the Attribute Rule
After the successful creation of the calculation attribute rule, we can open the Map and do some testing. In the attribute table we can attempt to generate some error codes by making the type of edits we set the rule to catch. Changing the value in the Country field to “CA” and the value in the State field to “MO” should provide some error codes. The image on the left illustrates the edits and subsequent changes to the Error Codes field values for each feature.
Contrary to generating error codes, the attribute rule will also return the value of “No Errors” if errors are fixed or not present on a feature. The image on the right illustrates the correction of the bad values in the attribute table, and the subsequent changes to the Error Codes field becoming “No Errors.”
Conclusion
Attribute Rules in ArcGIS Pro allow for a plethora of use case scenarios, and today we learned one somewhat complex but useful scenario to data validations on the fly. Attribute rules ultimately make the editing process of data more manageable and increase the integrity of the data. Esri’s Arcade language has a very wide and diverse range of functionality, making the possibilities of attribute rules and field calculations seemingly endless. If you want to learn more about implementing this workflow in ArcGIS Pro, or any of the other workflows or solutions from Esri that Cloudpoint Geospatial can assist in implementing, Contact us