samedi 24 janvier 2015

Remove a layer from TOC in my printed PDF?


I am new at writing Python code so I'm just grasping the basics. I am trying to clip two shapefiles so that they are within the map boundaries. After that, I want to remove the unclipped layers and add the clipped layers to the TOC. Finally, print the map to a PDF. As of now, my clipping seems to work, but when the PDF is printed, the layers I do not want on the map are still visible.



import arcpy
from arcpy import env

#Local Variables

RI_Schools = "E:\\Python Lab 2\\PythonInArcGIS\\RhodeIsland.gdb\\RI_Schools"
RI_Sewers = "E:\\Python Lab 2\\PythonInArcGIS\\RhodeIsland.gdb\\RI_Sewers"
Providence = "E:\\Python Lab 2\\PythonInArcGIS\\RhodeIsland.gdb\\Providence"
Schools_shp = "E:\\Python Lab 2\\PythonInArcGIS\\Schools.shp"
Sewers_shp = "E:\\Python Lab 2\\PythonInArcGIS\\Sewers.shp"

#Process: Clip

arcpy.Clip_analysis(RI_Schools, Providence, Schools_shp, "")
arcpy.Clip_analysis(RI_Sewers, Providence, Sewers_shp, "")

mxd = arcpy.mapping.MapDocument("E:\\Python Lab 2\\PythonInArcGIS\\Providence.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

newLayer1 = arcpy.mapping.Layer("E:\\Python Lab 2\\PythonInArcGIS\\Schools.shp")
print newLayer1
arcpy.mapping.AddLayer(df, newLayer1)

newLayer2 = arcpy.mapping.Layer("E:\\Python Lab 2\\PythonInArcGIS\\Sewers.shp")
print newLayer2
arcpy.mapping.AddLayer(df, newLayer2)

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

#Remove RI_Schools and RI_Sewers from TOC

names = [RI_Schools, RI_Sewers]

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

for layer in layers:
if layer.name in names:
layer.visible = False

arcpy.RefreshTOC()
arcpy.RefreshActiveView()

#Export to PDF

arcpy.mapping.ExportToPDF(mxd, ""E:\\Python Lab 2\\PythonInArcGIS\\Providence1.pdf")
del mxd




Aucun commentaire:

Enregistrer un commentaire