I can see that multiple comments aren't being understood. This is what I meant:
import os,arcpy
from arcpy import env
mxd = arcpy.mapping.MapDocument("CURRENT")
#arcpy.env.overwriteOutput = True
fieldName = "RasterID"
RowsField = "unknown" # the field (numeric) for the number of rows
ColsField = "unknown" # the field (numeric) for the number of columns
for lyr in arcpy.mapping.ListLayers(mxd):
desc = arcpy.Describe(lyr)
expression = desc.baseName
desc = arcpy.Describe(desc.catalogPath) # describe the data that the layer points to
arcpy.AddMessage("Base Name: " + expression )
arcpy.AddMessage("Rows: " + str(desc.height) + ", Cols:" + str(desc.width))
arcpy.CalculateField_management(lyr, fieldName, "\"" + expression + "\"", "PYTHON_9.3")
arcpy.CalculateField_management(lyr, RowsField, desc.height,"PYTHON_9.3")
arcpy.CalculateField_management(lyr, ColsField, desc.width,"PYTHON_9.3")
arcpy.AddMessage("done")
When I say that you need to quote the value it is for the field calculator to know that you want to calculate a constant string value expression
and not a field called expression
, to do this you need to surround the expression by quote marks thus: "\"" + expression + "\""
; in python (and many other languages) if you want to include a quote mark in a string you need to escape it, this allows characters that you wouldn't normally be able to put into the string like \"
(quote), \n
(new line) and \t
(tab). Because the \ character is used to escape if you want one in a string you need to put two \\
which python interprets as \, for example a path name : "C:\\some\\folder"
is interpreted as C:\some\folder
.
This only applies for string fields, all other fields that you can calculate (short, long, double etc..) do not need to be quoted.
With some error checking:
import os,arcpy
from arcpy import env
mxd = arcpy.mapping.MapDocument("CURRENT")
fieldName = "RasterID"
RowsField = "unknown" # the field (numeric) for the number of rows
ColsField = "unknown" # the field (numeric) for the number of columns
for lyr in arcpy.mapping.ListLayers(mxd):
desc = arcpy.Describe(lyr) # describe the layer
expression = desc.baseName
arcpy.AddMessage( "Base Name: " + expression )
desc = arcpy.Describe(desc.catalogPath) # describe the data that the layer points to
gotRowsCols = True # assume true unless they can't be retrieved
try:
arcpy.AddMessage("Rows: " + str(desc.height) + ", Cols:" + str(desc.width))
except:
arcpy.AddWarning("Cannot retrieve rows-cols property, layer may not be a raster")
gotRowsCols = False # unable to get the rows-cols from the layer
# look for the RasterID field and if it exists try to calculate the name of the raster
# and then rows and columns if those fields are found
field = arcpy.ListFields(lyr,fieldName)
if field:
arcpy.CalculateField_management(lyr, fieldName, "\"" + expression + "\"", "PYTHON_9.3")
# if the rows and columns values can be retrieved then try find the fields and calc
if gotRowsCols:
# try to find the RowsField and if it exists then calc the height of the raster
field = arcpy.ListFields(lyr,RowsField)
if field:
arcpy.CalculateField_management(lyr, RowsField, desc.height,"PYTHON_9.3")
# try to find the ColsField and if it exists then calc the width of the raster
field = arcpy.ListFields(lyr,ColsField)
if field:
arcpy.CalculateField_management(lyr, ColsField, desc.width,"PYTHON_9.3")
else:
# field not found in this layer, issue a warning and move on.
arcpy.AddWarning("Field not found " + fieldName)
arcpy.AddMessage( "done" )