Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using the arcpy module to loop through a number of map features (community areas), zoom to them, title the map with the name of the community area, and export to pdf. The trouble is that while the title, legend, and line features export, a symbolized polygon layer doesn't. It still shows up in the legend, but the map doesn't show it.

Trying a couple of sanity checks, I have

  1. confirmed that that layer in question is visible in the ArcMap map that I'm starting with,
  2. tried making sure that that layer is on top,
  3. instructed my code to make that layer the only visible one before exporting, and
  4. had Python list the set of layers that are visible, right before exporting.

None of those tests give me anything better, or give me any insight to the problem.

My code is below. Since I'm also a relative newbie to coding with arcpy, any suggestions that you have for solutions or more elegant coding would be very welcome!

-Nick

My code is:

import arcpy, os
myPath    = r"C:\Users\<myname>\Documents\Heat Maps\\"
myMapPath = r"C:\Users\<myname>\Documents\Heat Maps\Map Output\\"

arcpy.env.workspace = myPath
myMap = arcpy.mapping.MapDocument(myPath + "Heat Maps of Need - v4-0 - Added Streets for Zoomed in Maps.mxd")

AllLayers = arcpy.mapping.ListLayers(myMap)
df = arcpy.mapping.ListDataFrames(myMap)[0]
lyrList = ["Needs Index"]
nComm = 77

# Clear visibility of all layers except for the community areas layer

for lyr in AllLayers:
    if lyr.name = "Needs Index":
        print "Turning on Layer " + lyr.name
        lyr.visible = True
    else:
        lyr.visible = False

# Loop through specified layers and export to pdf

if os.path.exists(myPath + "Heat Maps of Need Indicators.pdf"):
    os.remove(myPath + "Heat Maps of Need Indicators.pdf")
PDFdoc = arcpy.mapping.PDFDocumentCreate(myPath + "Heat Maps of Need Indicators.pdf")

for l in lyrList:

    # Turn on proper heat layer
    for lyr in AllLayers:
        if lyr.name == l:
            lyr.visible = True
    arcpy.RefreshActiveView()

    # Check which layers are visible
    for lyr in AllLayers:
        if True == lyr.visible:
            print "Layer " + lyr.name + " is visible"

    # Loop through all community areas
    for c in range(1, nComm + 1): # Add 1, since the top value of the specified range is not included

        # Run tasks specific to the community area in question
        for lyr in AllLayers:
            if lyr.name == "Community Areas":

                # Get name of community area
                SqlWhere = " \"AREA_NUMBE\" = \'" + str(c) + "\'"

                CommunityRowObj = arcpy.SearchCursor(lyr, SqlWhere)
                for row in arcpy.SearchCursor(lyr, SqlWhere):
                    CommunityName = row.getValue("COMMUNITY")
                    CommunityName = CommunityName.title()

                # Zoom in to community area
                arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", SqlWhere)
                df.zoomToSelectedFeatures()
                arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")

        for x in arcpy.mapping.ListLayoutElements(myMap):
            if x.name == "Title":
                x.text = "Heat Map of Needs Indicators\n" + l + " - " + CommunityName

        arcpy.RefreshActiveView()

        # Export the layer to PDF
        arcpy.mapping.ExportToPDF(myMap, myMapPath + "Needs Heat Map - " + l + " - " + CommunityName + ".pdf")
        PDFdoc.appendPages(myMapPath + "Needs Heat Map - " + l  + " - " + CommunityName + ".pdf")

    # Turn off current Needs Layer to clear it from the next one
    for lyr in AllLayers:
        if lyr.name == l:
            lyr.visible = False

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
PDFdoc.saveAndClose()

del PDFdoc
del AllLayers
share|improve this question
1  
What you are trying to do should work because I've done similar things lots over the last 12 months. Rather than me try to debug your code I suggest that you put in quite a few AddMessage/print statements to confirm that the settings are as you think they are when the script runs. I suspect that may show up whatever is going astray. –  PolyGeo Nov 9 '12 at 3:09
1  
I've had that problem several times, even when just exporting one map as as PDF through the File menu - one of my layers would randomly not show up in the pdf. I normally accept the default resolution when saving to pdf, but when this problem pops up, I lower it to 150dpi, or a bit lower, and then my layer shows in the pdf. –  Arabella Nov 9 '12 at 5:07
1  
Does it work when you use the built-in "export to PDF" tool? –  Jakub Nov 12 '12 at 20:10
add comment

2 Answers

If my Comment does not help, or in any event, can I suggest that the following may provide a slightly more elegant way to zoom to the extent of a where clause result. I did it your way at first but have been doing it as below for quite a while.

lyr.definitionQuery = SqlWhere
df.extent = lyr.getSelectedExtent(False)
share|improve this answer
add comment

I believe that I solved my own problem. Doing a little more thinking about the difference between which layers were and weren't exporting, I realized that the layer that WASN'T showing up was displaying symbolized values of a field that were joined to that layer from a table in the map document. When I exported the joined layer as its own .shp file and remade my map to display that layer, everything exported as it should.

Not sure if there are other solutions (i.e. so that joined-to layers can be successfully exported to pdf with less fuss), but this definitely worked.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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