6

I have a Shapefile and a list in .csv which contains two columns with names:

  • "Old_name" which contains the old names that already exist in the attribute table of the previously mentioned Shapefile; and
  • "New_name" which contains the updated name.

How can I do this either on ArcGIS, QGIS or with python?

Based on this question I need to use the UpdateCursor function, but how can I use the list in this function?

  • 3
    Surely you could simply add a new field to the attribute table of the original Shapefile, then use a table join (using the common field "Old_name") and then copy the value across from "New_name" using Field Calculator into the newly created field? Or perhaps I'm missing something... – veedub Jul 15 '15 at 9:59
  • No you do not missing something else. I know that I can to do this with join, but I lookng a diffirent - programmatically way to do this. – Dimitris Kasabalis Jul 17 '15 at 6:14
2

Assuming the csv is ready to be consumed by arcpy as a table with two columns named OLD_NAME and NEW_NAME, and following the example provided in the question to get to the source shp in the UpdateCursor.

#create a dictionary of the csv to join to the shapefile
#r[0] is the first item passed to the SearchCursor and used to join to shp
#r[1] is the second item and used for the update
csvDict= dict([(r[0], (r[1])) for r in arcpy.da.SearchCursor(my_csv, ["OLD_NAME","NEW_NAME"])]) 

with arcpy.da.UpdateCursor(my_shp, ["OLD_NAME","NEW_NAME"]) as cursor:
    for row in cursor:
        joinFieldValue = row[0] #this is "OLD_NAME" on the shapefile            
        if row[0]  in csvDict: #check to see if old_name exists in new 
            row[1] = csvDict[joinFieldValue]
        else:
            row[1] = row[0] #new name is same as the old name
        cursor.updateRow(row)
    del cursor, row
  • When I run this code in ArcGis I get the following error message, Runtime error Traceback (most recent call last): File "<string>", line 4, in <module> KeyError: u'1011' – Dimitris Kasabalis Jul 16 '15 at 11:29
  • It would seem that the 1011 doesn't exist in the dictionary. See this example to check the contents stackoverflow.com/questions/10116518/… – dbusses Jul 16 '15 at 18:37
  • Thank you very much. As you notice I'm newbie in python. Is there a way to bypass tha values that isn't in the dictionary and change only the values that included? – Dimitris Kasabalis Jul 17 '15 at 6:07
  • I modified the code to include an if statement to check the value is in the dictionary. – dbusses Jul 17 '15 at 15:00
5

In QGIS you can perform Add Vector Join.

  1. Right click on your shapefile layer on Layer Panel and choose Properties.
  2. Then go to Joins tab.
  3. Press the + button, it will create new join. enter image description here
  4. Join layer is your shapefile layer. Join field is field with the common values in both. Target field is your field you want to join to.
  5. After that you will get a new attribute in your shapefiles table.

In ArcGIS it is very similar. You need to use Add Join tool.

More about joining in ArcGIS.

2

Import the csv as a table so you can see the values then right-click your original layer to choose joins and relates -> join. Be sure "Join Attributes from Table" is the option selected from the pulldown at the top. Choose the old_name from the original layer (under #1), the csv table under #2, and the old_name in the csv table for #3, then click Ok. If you then save the original layer to new output by right-clicking and choosing "Data"->"Export Data", it will have both fields from the csv table in the output attribute table so that you can use it for your symbology or however you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.