I have been using arcpy intermittently over the past year and a half mainly for automating and chaining batch processing to save myself countless hours of repetition. This week, however, I had to implement a facet of arcpy that I had not yet had the opportunity to utilise – the data access module.
A file geodatabase with 75 feature classes each containing hundreds to thousands of features. These feature classes were the product of a CAD (Bentley Microstation) to GIS conversions via FME with data coming from 50+ CAD files. As a result of the conversion each feature class could contain features with various attributes from one or multiple CAD files but each feature class consisted of the same schema which was helpful.
The main issue was that the version number for a chunk of the CAD files had not been corrected. Two things needed to be fixed: i) the ‘REV_NUM’ attribute for all feature classes needed to be ‘Ver2’, there would be a mix of ‘Ver1’ and ‘Ver2’, and ii) in the ‘MODEL_SUMMARY’ if ‘Ver1’ was found anywhere in the text it needed to be replaced with ‘Ver2’. There was one other issue and this stemmed from creating new features and not attributing them, this would have left a ‘NULL’ value in the ‘MODEL’ field (and the other fields). All features had to have standardised attributes. The script would not fix these but merely highlight the feature classes.
OK so a quick recap…
1. Set the ‘REV_NUM’ for every feature to ‘Ver2’
2. Find and replace ‘Ver1’ with ‘Ver2’ in the text string of ‘MODEL_SUMMARY’ for all features.
3. Find all feature classes that have ‘NULL’ in the ‘MODEL’ field.
Let’s take a look at the thirteen lines of code required to complete the mission.
import arcpy arcpy.env.workspace = r"C:\Users\*****\Documents\CleanedUp\Feature_Classes.gdb" fc_list = arcpy.ListFeatureClasses() fields = ["MODEL", "MODEL_SUMMARY", "REV_NUM"] for fc in fc_list: with arcpy.da.UpdateCursor(fc, fields) as cursor: for row in cursor: if row == None or row == "": print fc + ": Null value found for MODEL" break if row != None: row = row.replace("Ver1", "Ver2") row = "Ver2" cursor.updateRow(row)
Import the arcpy library (you need ArcGIS installed and a valid license to use)
Set the workspace path to the relevant file geodatabase
arcpy.env.workspace = r"C:\Users\*****\Documents\CleanedUp\Feature_Classes.gdb"
Create a list of all the feature classes within the file geodatabase.
fc_list = arcpy.ListFeatureClasses()
We know the names of the fields we wish to access so we add these to a list.
fields = ["MODEL", "MODEL_SUMMARY", "REV_NUM"]
For each feature class in the geodatabase we want to access the attributes of each feature for the relevant fields.
for fc in fc_list: with arcpy.da.UpdateCursor(fc, fields) as cursor: for row in cursor:
If the ‘MODEL’ attribute has a None (NULL) or empty string value then print the feature class name to the screen. Once one is found we can break out and move onto the next feature class.
if row == None or row == "": print fc + ": Null value found for MODEL" break
We know have a list of feature classes that we can fix the attributes manually.
Next we find any instance of ‘Ver1’ in ‘MODEL_SUMMARY’ text strings and replace it with ‘Ver2’….
if row != None: row = row.replace("Ver1", "Ver2")
…and update all ‘REV_NUM’ attributes to ‘Ver2’ regardless of what is already attributed. This is like using the Field Calculator to update.
row = "Ver2"
Perform and commit the above updates for each feature.
Very handy to update the data you need and this script can certainly be extended to handle more complex operations using the arcpy.da.UpdateCursor module.
Check out the documentation for arcpy.da.UpdateCursor