Hello,
I'll try to give a brief background here. I recently received a large amount of data that was all digitized from paper maps. Each map was saved as an individual file that contains a number of records (polygons mostly). My goal is to merge all of these files into one shapefile or geodatabase, which is an easy enough task. However, other than spatial information, the records in the file do not have any distinguishing information so I would like to add a field and populate it with the original file name to track its provenance. For example, in the file "505_dmg.shp" I would like each record to have a "505_dmg" id in a column in the attribute table labeled "map_name". I am trying to automate this using Python and feel like I am very close. Here is the code I'm using:
# Import system module
import arcpy
from arcpy import env
from arcpy.sa import *
# Set overwrite on/off
arcpy.env.overwriteOutput = "TRUE"
# Define workspace
mywspace = "K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test"
print mywspace
# Set the workspace for the ListFeatureClass function
arcpy.env.workspace = mywspace
try:
for shp in arcpy.ListFeatureClasses("","POLYGON",""):
print shp
map_name = shp[0:-4]
print map_name
arcpy.AddField_management(shp, "map_name", "TEXT","","","20")
arcpy.CalculateField_management(shp, "map_name","map_name", "PYTHON")
except:
print "Fubar, It's not working"
print arcpy.GetMessages()
else:
print "You're a genius Aaron"
The output I receive from running this script:
>>>
K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test
505_dmg.shp
505_dmg
506_dmg.shp
506_dmg
You're a genius Aaron
Appears successful, right? Well, it has been: a field was added and populated, and it is perfect for 505_dmg.shp. Problem is. 506_dmg.shp is also been labeled "505_dmg" in the "map_name" column. Though the loop appears to be working, the map_name variable does not seem to be updating. Any thoughts or suggestions much appreciated. Files attached in the zip file.
Thanks,
Aaron
Solved! Go to Solution.
Hi Aaron,
Try using this instead, worked for me.
arcpy.CalculateField_management(shp, "map_name","\"" + map_name + "\"", "PYTHON")
on a lark...rename the field you are creating so that it isn't the same as your variable name...also, fix your indentation use try not to mix and match...it makes it hard to read...use "PYTHON_9.3" as well ... no time to test, but just some observations and probably not the solution
Hi Dan, thanks for the observations. I implemented them but to no avail, still having the same problem. Script is "working" but not populating the second file's field correctly.
Hi Aaron,
Try using this instead, worked for me.
arcpy.CalculateField_management(shp, "map_name","\"" + map_name + "\"", "PYTHON")
Hi Riyas Deen,
Thank you so much, that appears to have done the trick. Can I ask what exactly that does to the code? What do the quotes and slash add to what I originally had?
Again, thank you.
Aaron
Hi Aaron,
This "\"" would actually translate to a double quote in the script. The code is actually prefixing and suffixing the shape file name with double quotes. This will pass the shape file name as a string to Calculate Field tool.
Great, thanks for explanation
happily it is far easier...your calculatefield expression was correct, you just forgot to make a variable name out of it...see the example below
>>> import arcpy
>>> shp = r"C:\Temp\SoObviousNow.shp"
>>> arcpy.AddField_management(shp, "HereYaGo", "TEXT","","","40")
<Result 'C:\\Temp\\SoObviousNow.shp'>
>>> map_name = shp[0:-4]
>>> print map_name
C:\Temp\SoObviousNow
>>> arcpy.CalculateField_management(shp, "HereYaGo","map_name", "PYTHON_9.3")
<Result 'C:\\Temp\\SoObviousNow.shp'>
>>>