5

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!

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 8, 2012 at 20:41
3
  • 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. Commented Nov 9, 2012 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. Commented Nov 9, 2012 at 5:07
  • 1
    Does it work when you use the built-in "export to PDF" tool? Commented Nov 12, 2012 at 20:10

3 Answers 3

1

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)
answered Nov 9, 2012 at 3:16
0
1

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.

answered Nov 9, 2012 at 17:36
0

Another issue that may cause missing layers in an exported map is a bug in ESRI's 64-bit implementation of the tool. It's been around since 10.0 and hasn't been fixed as of 10.2.2.

I've found that running my script using the 32-bit python installation resolved it for me.

The simplest way to do this is to open the command prompt and then, for the default ArcGIS Python installation path, enter, for example:

C:\Python27\ArcGIS10.2\python C:\Temp\MyScript.py

answered Nov 9, 2015 at 17:34
1
  • Another benefit of this fix is that the PDFs generated from ESRI's 32-bit implementation are also readable by PyPDF2. PDFs exported with arcpy's 64-bit ExportToPDF function bring up that obnoxious EOF error. Commented Nov 9, 2015 at 18:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.