0

I'm creating a mapbook(PDF) of all California's counties with its population centroid. The code runs, but the graduated symbology for the centroid is not displaying on the pdf.

# Imports 
import os, arcpy

# Allows us to re-run the code multiple times, overwriting previous material
arcpy.env.overwriteOutput = True

# Defines the folder path
folder_path = r"E:\Geog173Programming\Final Project\California"

# Allows to access the arcmap document that I created and saved 
mxd = arcpy.mapping.MapDocument(os.path.join(folder_path, r"California_Mapbook.mxd"))

# Allows it to access the data frame (2 data frames will be used)  
mapDF = arcpy.mapping.ListDataFrames(mxd, "Map")[0] # Main map data frame
insetDF = arcpy.mapping.ListDataFrames(mxd, "Inset")[0] # Inset data frame

# Creates a final PDF document and titles it
finalPDF_frame = os.path.join(folder_path, r'CAMapbook.pdf')
final_PDF = arcpy.mapping.PDFDocumentCreate(finalPDF_frame)

# Defines the temporary pdf file 
tmpPDF = os.path.join(folder_path, r'tmp.pdf')

# Reads that we it will put information in the new empty text box that
# I created 
titlePageinfo = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")[0]

# Gives the cover page a title, and the authors name
titlePageinfo.text = r"Population Weighted Centroid of Each State" + "\r" + "By: XXX"

# Assigns it to my temporary pdf
arcpy.mapping.ExportToPDF(mxd, tmpPDF)
# And then appends it to the final pdf
final_PDF.appendPages(tmpPDF)

for lyr in arcpy.mapping.ListLayers(mxd,"County Centroid",mapDF):
    if lyr.symbologyType=="GRADUATED_SYMBOLS":
        lyr.symbology.valueField="totpop"
        lyr.symbology.numClasses=5

# Selecting the north arrow, lengend, and scale bar
# and issues a postion of x and y coordinates 
North_Arrow = arcpy.mapping.ListLayoutElements(mxd, "MAPSURROUND_ELEMENT", "North Arrow")[0]
North_Arrow = elementPositionX = 0.7138
North_Arrow = elementPositionY = 9.5479

Legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
Legend = elementPositionX = 0.7504
Legend = elementPostitionY = 1.1939

Scale = arcpy.mapping.ListLayoutElements(mxd, "MAPSURROUND_ELEMENT", "Alternating Scale Bar")[0]
Scale = elementPositionX = 0.7704
Scale = elementPositionY = 0.6872

# Labels the state
mapStateLyr = arcpy.mapping.ListLayers(mxd, "California", mapDF)[0]
mapStateLyr.showLabels = True

# Defines the title variable. Reads the same text box that
# created earlier 
titleinfo = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")[0]

# Places the Inset map frame
insetDF.elementPositionX = 5.6023
insetDF.elementPositionY = 0.4836
insetDF.elementHeight = 2.25
insetDF.elementWidth = 2.3027

stateFields = ['OID@', 'NAME', 'CNTY_FIPS', 'SHAPE@']

# Get details for all the states in the Inset map to
# allow selection in Inset
insetStateLyr = arcpy.mapping.ListLayers(mxd, "California2", insetDF)[0]
insetStateDict = dict()
with arcpy.da.SearchCursor(insetStateLyr, stateFields) as insetLyrCursor:
    for insetRow in insetLyrCursor:
        insetStateDict[insetRow[1]] = insetRow[0]

# Searches through each row in the state layer. 
# Prints the state name and state FIPS for each page
with arcpy.da.SearchCursor(mapStateLyr, stateFields) as mapLyrCursor:
    for mapRow in mapLyrCursor:
        print "County: {}".format(mapRow[1])
        titleinfo.text = "County Name:" + str(mapRow[1]) + "\n" + "County FIPS:" + str(mapRow[2])
        # Positions the the title on each page at a particular x and y coordinate
        titleinfo.elementPositionX = 6.1329
        titleinfo.elementPositionY = 10.1693 
        # Pulling out descripitve information and getting value from the state shp.
        feature = mapRow[3]
        # Extent of that particualr feature. Zooms into the county.
        mapDF.extent = feature.extent

        # Find state in Inset map and Select it
        if mapRow[1] in insetStateDict:
            stateSelection = [insetStateDict[mapRow[1]]] # setSelectionSet requires a List
        insetStateLyr.setSelectionSet("NEW", stateSelection)
        arcpy.RefreshActiveView()

        # Finally appends it to the temporary pdf, which then appends to the final pdf
        arcpy.mapping.ExportToPDF(mxd, tmpPDF)
        final_PDF.appendPages(tmpPDF)

# Saves and closed the final PDF document
final_PDF.saveAndClose()
arcpy.Delete_management(tmpPDF)

del mxd

print "Done"
7
  • 1
    What is it displaying on the PDF? What does it look like when you set it manually within ArcMap and export to PDF?
    – Midavalo
    Jun 5 '16 at 6:48
  • When I set the graduated symbology manually, it displays on each page of the PDF. This is just the cover page.
    – M.Reyes
    Jun 5 '16 at 7:27
  • In your attached code you're outputting your cover page before you set the graduated symbology
    – Midavalo
    Jun 5 '16 at 7:30
  • I tried setting the graduated symbology before outputting the cover page, but it's still not displaying in the PDF.
    – M.Reyes
    Jun 5 '16 at 7:55
  • Please include a screenshot of what you see in output PDF and what you see when you manually do it in ArcMap
    – Midavalo
    Jun 5 '16 at 7:56
2

You cannot change a layer's Symbology Type in arcpy, you can only read it, hence the line in your code that says if lyr.symbologyType=="GRADUATED_SYMBOLS": - it's checking that the layer Symbology Type is already Graduated Symbols. Therefore you need to already have set your layer to be Graduated Symbols, or imported that symbology type from another layer.

Once the symbology type is set in your Layer you can set the other properties lyr.symbology.valueField = "totpop" and lyr.symbology.numClasses = 5 and output them to your PDF.

You need to set these properties before you output to PDF

for lyr in arcpy.mapping.ListLayers(mxd,"County Centroid",mapDF):
    if lyr.symbologyType=="GRADUATED_SYMBOLS":
        lyr.symbology.valueField="totpop"
        lyr.symbology.numClasses=5

# Assigns it to my temporary pdf
arcpy.mapping.ExportToPDF(mxd, tmpPDF)
# And then appends it to the final pdf
final_PDF.appendPages(tmpPDF)

Not the answer you're looking for? Browse other questions tagged or ask your own question.