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
- confirmed that that layer in question is visible in the ArcMap map that I'm starting with,
- tried making sure that that layer is on top,
- instructed my code to make that layer the only visible one before exporting, and
- 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\\Documents\Heat Maps\\"
myMapPath = r"C:\Users\\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
No comments:
Post a Comment