Changing of python code

4527
6
Jump to solution
03-22-2015 07:16 PM
PV
by
New Contributor III

Hi

I found this Code by   Yanli Zhang, Stephen F. Austin State University  online

I am not familiair with python but this script is exactly what I need, except that I do not have years but attribute names like WRBDeAA01 or WNHDeAA51 (list of attribute names)

How do I have to change the loop so the code runs through these attributes instead of years?

Thanks

import arcpy, os

#Specify the map document and the data frame

mxd = arcpy.mapping.MapDocument(r"C:\temp\pine.mxd")

df = arcpy.mapping.ListDataFrames(mxd, "layers")[0]

outPath = r"C:\temp\output\\"    #output file folder

#Year (2004, 2003, ...) is used as attribute field name

#Also it is used to control the loop

year = 2004 



while year >= 1860:       # 1860 is the last year in the attribute table

    for maplayer in arcpy.mapping.ListLayers(mxd):

        if maplayer.name == "Pine":

            #chang the symbology

if maplayer.symbologyType == "GRADUATED_SYMBOLS":

                maplayer.symbology.valueField = str(year)

                maplayer.symbology.classBreakValues = [0, 20.0, 30.0, 40.0, \

50.0, 60.0, 70.0, 80.0, \

90.0, 100.0, 110.0, 120.0, \

130.0, 140.0, 150.0]

                maplayer.symbology.classBreakLabels = ["0 to 19.99", "20 to 29.99", "30 to 39.99", \

   "40 to 49.99", "50 to 59.99", "60 to 69.99", \

   "70 to 79.99", "80 to 89.99", "90 to 99.99", \

   "100 to 109.99", "110 to 119.99", \

   "120 to 129.99", "130 to 139.99", \

   "140 to 150"]

    # Change title for each map

    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):

        if elm.text.find("Plot 4 Year")>= 0:

            elm.text = "Plot 4 Year " + str(year)

   

#export each map as a jpeg file. Also we can use ExportToPDF method.

nameJPEG = outPath + str(year) + ".jpg"   #spedify each output file name

arcpy.mapping.ExportToJPEG(mxd, nameJPEG)   

year = year - 1

del mxd, df
Tags (4)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Looking at the Help: ArcGIS Help (10.2, 10.2.1, and 10.2.2)  for GraduatedSymbolsSymbology (arcpy.mapping) it seems that you cannot define the renderer to use natural breaks. In that case you should do some data exploration and set the values for the breaks based on the data. Make sure that the layer in the TOC has the color ramp set to it to obtain the choropleth map you are looking for,

It might be possible to use the natural breaks if the layer is already defined to use it, but I'm not sure if setting the valuefield to a different field, triggers the jenks to be recalculated. You could give it a try though...

View solution in original post

6 Replies
BlakeTerhune
MVP Regular Contributor

One of the first things you should know about Python is that indentation matters. The code you posted has some strange indentation and I'm not confident I'm interpreting it correctly. Do you have a link to the original Python file or can you just upload it here?

0 Kudos
XanderBakker
Esri Esteemed Contributor

To make the code a little bit more readable I added so code highlighting to it (and made some minor changes):

import arcpy, os

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r"C:\temp\pine.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "layers")[0]
outPath = r"C:\temp\output"    #output file folder

#Year (2004, 2003, ...) is used as attribute field name
#Also it is used to control the loop
year = 2004

while year >= 1860:       # 1860 is the last year in the attribute table
    for maplayer in arcpy.mapping.ListLayers(mxd):
        if maplayer.name == "Pine":
            #chang the symbology
            if maplayer.symbologyType == "GRADUATED_SYMBOLS":
                maplayer.symbology.valueField = str(year)
                maplayer.symbology.classBreakValues = [0, 20.0, 30.0, 40.0, 
                                                       50.0, 60.0, 70.0, 80.0,
                                                       90.0, 100.0, 110.0, 120.0,
                                                       130.0, 140.0, 150.0]
                maplayer.symbology.classBreakLabels = ["0 to 19.99", "20 to 29.99",
                                                       "30 to 39.99", "40 to 49.99",
                                                       "50 to 59.99", "60 to 69.99",
                                                       "70 to 79.99", "80 to 89.99",
                                                       "90 to 99.99", "100 to 109.99",
                                                       "110 to 119.99", "120 to 129.99",
                                                       "130 to 139.99", "140 to 150"]

    # Change title for each map
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text.find("Plot 4 Year") >= 0:
            elm.text = "Plot 4 Year {0}".format(year)

    #export each map as a jpeg file. Also we can use ExportToPDF method.
    nameJPEG = os.path.join(outPath, "{0}.jpg".format(year))  #spedify each output file name
    arcpy.mapping.ExportToJPEG(mxd, nameJPEG)
    year = year - 1

del mxd, df

Now if you have fields like "WRBDeAA01" or "WNHDeAA51" you may want to consider using a dictionary the contains the logic of translating the field names to a year:

dct = {"WRBDeAA01": some_year,
       "WNHDeAA51": another_year,
       etc}

Then you will have to loop through the dictionary and retrieve the field name and corresponding name.

Kind regards, Xander

PV
by
New Contributor III

thank you Xander Bakker
Do you by chance also know a way how to create a maplayer with natural break (jenks) classbreakvalues and instead of graduated symbols I d like to use a choropleth map..

Best, P.

import arcpy, os
#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r"D:\Atlas_mxd.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "layers")[0]
#output file folder
outPath = r"D:\Atlas_folder\\"   
#attrnr (2, 1, ...) is used as attribute field name
#Also it is used to control the loop
attrnr = 2 
dct={"Gender_related_txt_WNHDeAA51":2, "Gender_related_txt_WRBDeAA01":1}
while attrnr >= 1:      # 1 is the last attrnr in the attribute table
  for maplayer in arcpy.mapping.ListLayers(mxd):
      if maplayer.name == "aTEST2":
          #chang the symbology
          if maplayer.symbologyType == "GRADUATED_SYMBOLS":
              maplayer.symbology.valueField = str(attrnr)
              maplayer.symbology.classBreakValues = [0, 20.0, 30.0, 40.0, \
              50.0, 60.0, 70.0, 80.0, \
              90.0, 100.0, 110.0, 120.0, \
              130.0, 140.0, 150.0]
              maplayer.symbology.classBreakLabels = ["0 to 19.99", "20 to 29.99", "30 to 39.99", \
                "40 to 49.99", "50 to 59.99", "60 to 69.99", \
                "70 to 79.99", "80 to 89.99", "90 to 99.99", \
                "100 to 109.99", "110 to 119.99", \
                "120 to 129.99", "130 to 139.99", \
                "140 to 150"]
  #Change title for each map
  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
      if elm.text.find("Plot 4 attrnr")>= 0:
          elm.text = "Plot 4 attrnr " + str(attrnr)
    
#export each map as a jpeg file. Also we can use ExportToPDF method. 
  nameJPEG = outPath + str(attrnr) + ".jpg"  #spedify each output file name
  arcpy.mapping.ExportToJPEG(mxd, nameJPEG)    
  attrnr = attrnr - 1
del mxd, df
0 Kudos
XanderBakker
Esri Esteemed Contributor

Looking at the Help: ArcGIS Help (10.2, 10.2.1, and 10.2.2)  for GraduatedSymbolsSymbology (arcpy.mapping) it seems that you cannot define the renderer to use natural breaks. In that case you should do some data exploration and set the values for the breaks based on the data. Make sure that the layer in the TOC has the color ramp set to it to obtain the choropleth map you are looking for,

It might be possible to use the natural breaks if the layer is already defined to use it, but I'm not sure if setting the valuefield to a different field, triggers the jenks to be recalculated. You could give it a try though...

Zeke
by
Regular Contributor III

I believe you can set the classification method using ArcObjects, but that would be a question for a different forum. But if it's an issue, might be worth exploring.

DarrenWiens2
MVP Honored Contributor

If you're feeling brave, install PySAL and use the natural breaks function once you have a list of values (get using a SearchCursor​, or better yet FeatureClassToNumPyArray). I'd consider this an advanced manoeuvre which would probably spawn many more questions, but worth noting.