Digital Geography

20. January 2015

Attribute Updating using ArcPy

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:

  1. Choose the first shapefile (the source).
  2. Identify the unique ID field in that shapefile.
  3. Write down all the values from the desired field (to replace the old values).
  4. Choose the second shapefile (the destination).
  5. Search for the unique ID records which meet that unique ID field in (step 2).
  6. 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.

import arcpy

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

fc= "Updated_subset.shp"

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[0])

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[0]

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[0] = 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“.

Before

After

Your comments are more than welcome.

  • Jason Pardy

    You could do this using the Join Field tool. However, this tool can be slow:

    arcpy.JoinField_management(fc2, “FID”, “fc”, “FID”, [“STATE”])

    Or you can do it this way much faster:

    arcpy.AddField_management(“citiesCopy”, “STATE_NAME”, “TEXT”)
    arcpy.AddJoin_management(“citiesCopy”, “FID”, “cities”, “FID”) arcpy.CalculateField_management(“citiesCopy”, “citiesCopy.STATE_NAME”, “!cities.STATE_NAME!”, “PYTHON”)
    arcpy.RemoveJoin_management(“citiesCopy”)

    • Habboub

      Thanks Jason for your comment. I’ll try it 🙂