Hi,
I used this Python code to generate a series of codes from a single layer in an existing map document. However, the code returned an error message when I run it. Could you please help me figure out what could have gone wrong?
import arcpy
arcpy.env.overwriteOutput=True
mxd = arcpy.mapping.MapDocument(r"\\bsedom5\users\kachieng2\Desktop\Achieng\Python\Lesson1\Lesson1\ModelPractice.mxd")
DataFrame = arcpy.mapping.ListDataFrames(mxd)[0]
polygonLayer = arcpy.mapping.ListLayers(mxd, "us_cities.shp", DataFrame)[0]
Columns = arcpy.ListFields (polygonLayer)
for col in Columns:
arcpy.CalculateField_management(polygonLayer , "DummyColumn", col, "PYTHON_9.3")
arcpy.mapping.ExportToPNG(mxd, r"\\bsedom5\users\kachieng2\Desktop\Achieng\Python\Lesson1\Lesson1\maps"+ c + ".png")
Thanks in advance
Kevin
Not sure what you are trying to achieve with the code, but I see several errors. Let's insert the code with syntax highlighting to make things more clear:
import arcpy arcpy.env.overwriteOutput = True mxd = arcpy.mapping.MapDocument(r"\\bsedom5\users\kachieng2\Desktop\Achieng\Python\Lesson1\Lesson1\ModelPractice.mxd") DataFrame = arcpy.mapping.ListDataFrames(mxd)[0] polygonLayer = arcpy.mapping.ListLayers(mxd, "us_cities.shp", DataFrame)[0] Columns = arcpy.ListFields(polygonLayer) for col in Columns: # CalculateField_management (in_table, field, expression, {expression_type}, {code_block}) arcpy.CalculateField_management(polygonLayer , "DummyColumn", col, "PYTHON_9.3") arcpy.mapping.ExportToPNG(mxd, r"\\bsedom5\users\kachieng2\Desktop\Achieng\Python\Lesson1\Lesson1\maps"+ c + ".png")
Can you explain what you are trying to obtain with the code?
Kind regards, Xander
Hi Xander,
I'm trying to generate maps from multiple columns of data of different years. I would like to loop through these columns and generate PNG/PDF maps for different years.
Thanks in advance.
Kind regards,
Kevin
Hi Kevin,
Just to make things clear for me, you want to create maps based on your layer us_cities.shp, in which you visualize each column. You probably have a column “DummyColumn” which is used to symbolize the featureclass. You want to overwrite the content in that column so that the symbology changes and adapts to the current column in the loop.
Is this right?
You could try this:
import arcpy arcpy.env.overwriteOutput = True mxd = arcpy.mapping.MapDocument(r"\\bsedom5\users\kachieng2\Desktop\Achieng\Python\Lesson1\Lesson1\ModelPractice.mxd") DataFrame = arcpy.mapping.ListDataFrames(mxd)[0] polygonLayer = arcpy.mapping.ListLayers(mxd, "us_cities.shp", DataFrame)[0] fldnames = [fld.name for fld in arcpy.ListFields(polygonLayer)] # Columns = arcpy.ListFields(polygonLayer) for fldname in fldnames: # CalculateField_management (in_table, field, expression, {expression_type}, {code_block}) fldpy = "!{0}!".format(fldname) arcpy.CalculateField_management(polygonLayer, "DummyColumn", fldpy, "PYTHON_9.3") arcpy.mapping.ExportToPNG(mxd, r"\\bsedom5\users\kachieng2\Desktop\Achieng\Python\Lesson1\Lesson1\maps_"+ fldname + ".png")
In this case on line 9 a list of fieldnames is created (you may notice it takes the name property of the fld object). The Field Calculator requires a certain format for specifying the field name. Using Python is will be !fieldname!. That is what is created on line 13 and used on line 14. The fieldname is used for naming the output png.
Please be aware that this code still loops through all fields (including, text, shape, etc) which will create problems. A simple way would be to test for the field type to see if it can be used.
Kind regards, Xander