1

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
2
  • 1
    Without running this to confirm, i think you're adding the full path of the layers named RI_Schools & RI_Sewers to your list names. Try wrapping them in quotes RI_Schools
    – Andy
    Commented Jan 25, 2015 at 9:23
  • I agree with @AndyHarney. You are searching for layers with the full path as their names in the TOC. Either wrap the items in your names list in quotes as mentioned above or change your if statement to: if layer.dataSource in names:
    – Adam
    Commented Jan 25, 2015 at 15:23

3 Answers 3

1

On first glance I would recommend that after toggling the layer visibility to False and refreshing...that you the save the mxd before exporting. Either use the save() method or the saveACopy method on the MapDocument object.

Consider also trying the RemoveLayer function as an alternative. You likely need to save after this operation as well before exporting. Notice how in both python examples on the Remove Layer page a copy of the mxd is saved before the script completes.

2
  • Your suggestion for saveACopy worked great. The new mxd file contains the clipped shapefiles and it turned the visibility off for RI_Schools and RI_Sewers. However, the pdf I want to print from the saveACopy mxd is not working. The pdf still contains the layers I do not want and it isn't including the clipped shapefiles even though when I open the saveACopy mxd on ArcMap everything is in order. Here is my code for the export to a pdf.
    – Monty
    Commented Jan 26, 2015 at 0:31
  • # save copy and export to pdf
    – Monty
    Commented Jan 26, 2015 at 0:32
0

Per my comment.

You are looping through and checking the layer.name property against the file paths declared in your #local variables section and added to your names list.

The 2 options I can see to fix your code as-is are,

  1. Wrap RI_Schools & RI_Sewers in quotes, e.g.

    names = ["RI_Schools", "RI_Sewers"] or

  2. Use the layer.dataSource property (more info) suggested by @amarinel.

0
# save mxd and export to pdf

for df in arcpy.mapping.ListDataFrames(mxd):
  mxd.activeView = df.name
  mxd.title = df.name
  mxd.saveACopy("E:\\Python Lab 2\\PythonInArcGIS\\Providence_1.mxd" + df.name + ".mxd")

mxd = arcpy.mapping.MapDocument("E:\\Python Lab 2\\PythonInArcGIS\\Providence_1.mxd" + df.name + ".mxd")
# I also tried this code
mxd = arcpy.mapping.MapDocument("E:\\Python Lab 2\\PythonInArcGIS\\Providence_1.mxdLayers")
# Layers is my df.name

arcpy.mapping.ExportToPDF(mxd, "E:\\Python Lab 2\\PythonInArcGIS\\Providence1.pdf")
del mxd 
2
  • This is the code I'm having trouble with. - Monty
    – Monty
    Commented Jan 26, 2015 at 0:55
  • This "answer" seems to be related to the "question" but it appears that asker Monty and answerer user45498 may be the same person. Unfortunately, I am unable to append the answer into the question. To avoid further confusion please ask to have your user accounts merged by using these instructions.
    – PolyGeo
    Commented Jan 26, 2015 at 3:59

Your Answer

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

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