20. January 2015
After my first attempt in Python scripting using opensource Geospatial Data Abstraction Library (GDAL), I wanted to use ArcPy in my second trial.
The following code is so simple. You can use it to update the attribute table of a shapefile based on another shapefile/table.
Let me break it down using this pseudo-code:
- Choose the first shapefile (the source).
- Identify the unique ID field in that shapefile.
- Write down all the values from the desired field (to replace the old values).
- Choose the second shapefile (the destination).
- Search for the unique ID records which meet that unique ID field in (step 2).
- Update the values of a certain field (in all the records that exist in both shapefiles – from the source to the destination).
let’s see the code
1. Choosing the first shapefile (the source):
Since we are going to to write a stand-alone script, the “import ArcPy” statement is needed to import the ArcPy site package, including all its modules and functions.
When referencing data on disk, you can limit the need to write the full path by setting the workspace as part of the environment properties. For example, my workspace is the Data folder
from arcpy import env env.workspace = "C:/Data"
Now we are going to choose our source shapefile, which should be inside the workspace. Usually, it’s an updated subset from the original shapefile
2.Identifying the unique ID field in that shapefile:
To do so, we will iterate over the rows in the attribute table of the source. This is can be done using the SearchCursor function. in my case, the unique ID field is FID
cursor = arcpy.da.SearchCursor(fc, ["FID"]) for row in cursor: SQL_stat= "FID = "+ str(row)
The “SQL_stat” – as it implies – is an SQL statement which will be used later on (in steps 3&6). Since the SQL statement should be in a sting format, we will use str() to convert the FID from integer to string.
3.Choosing the second shapefile (the destination):
We won’t do anything new rather than referring to our destination shapefile
fc2 = "airports_old.shp"
4.Writing down the value of the desired field ( i.e. “STATE”)
cursor2 = arcpy.da.SearchCursor(fc, ["STATE"], SQL_stat) for row2 in cursor2: UpdatedValue = row2
I wish you’ve noticed the SQL_stat.
5&6. Searching for the unique ID records (in the destination) which meet that unique ID field in (step 2) and update the desired values.
cursor3 = arcpy.da.UpdateCursor(fc2, ["STATE"],SQL_stat) for row3 in cursor3: row3 = UpdatedValue cursor3.updateRow(row3)
We’ve used “Update cursors” to update attribute values. Since this will permanently modify the data, it is a good idea to copy the data first.
Finally, we end it with the following lines
del row del cursor del row2 del cursor2 del row3 del cursor3
Hope you enjoyed it.
Feel free to download my second tool “AttributeUpdater“.
Your comments are more than welcome.