Select to view content in your preferred language

Automatic Map creation with Python

3473
3
11-25-2014 02:09 PM
KevinAchieng
New Contributor

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

Tags (1)
0 Kudos
3 Replies
XanderBakker
Esri Esteemed Contributor

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")

  • When you loop through the fields, you obtain Field objects (not field names)
  • For each field will loop over fields like he Shape field, OID, etc
  • With the field calculator for each field you assign the field object to the output field "DummyColumn" (I don't think this is what you want)
  • On line 12 you have a variable c that does not exist. This might be "col", but since that is an object, you probably mean col.name...

Can you explain what you are trying to obtain with the code?

Kind regards, Xander

0 Kudos
KevinAchieng
New Contributor

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

0 Kudos
XanderBakker
Esri Esteemed Contributor

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

0 Kudos