2

I am trying to figure out how to delete the layer that is created during the Buffer_analysis line. I have searched all over the internet and have honestly tried about 100 different methods and cannot seem to find one that works. If I was to type a layer "name" I can delete the layer, but in this case the layer name will be dependent upon the user input at "outPath = arcpy.GetParameterAsText(1)"

This is only my second script so this is kicking my butt, I have been googling the heck out of this just to get this far, we have had zero teaching in our online class (just told to read a chapter and write a code).

UPDATE: Here is my full script.

# William Herzig
# Week 6 tool creation
# 6 Aug 2019

import arcpy
from arcpy import env

arcpy.env.workspace = "\\\\SNHU-LABS-UTIL2.snhu-labs.com\\userprofiles$\\william.herzig\\Documents\\programming\\module_5_data\\week_5.gdb"           

arcpy.env.overwriteOutput = True

# List of all variables for the script
# User input variables (parameters)
# Input parameters for buffer tool
inPath = arcpy.GetParameterAsText(0)
outPath = arcpy.GetParameterAsText(1)
bufferDistance = arcpy.GetParameterAsText(2)

inPath1 = arcpy.GetParameterAsText(3)
outPath1 = arcpy.GetParameterAsText(4)
bufferDistance1 = arcpy.GetParameterAsText(5)

inPath2 = arcpy.GetParameterAsText(6)
outPath2 = arcpy.GetParameterAsText(7)
bufferDistance2 = arcpy.GetParameterAsText(8)

# Input parameter Master Shape, used for clip tool, erase tool and Master Shape layer
natPark = arcpy.GetParameterAsText(9)

# Input variable for the final output feature class
finalOutput = arcpy.GetParameterAsText(10)

# Pre-defined variables
buffered_area = "buffered_area"
clip = "clip"
clip1 = "clip1"
clip2 = "clip2"
layer_name = finalOutput
layer_name1 = "Master Shape"
mxd = arcpy.mapping.MapDocument("CURRENT")
df=arcpy.mapping.ListDataFrames(mxd, "*")[0]
layerList = arcpy.mapping.ListLayers(mxd, "", df)

# Clip tool, clipping the 3 input feature classes to the Master Shape.  This will reduce clutter and time due to less features for the buffer tool
arcpy.Clip_analysis(inPath, natPark, clip, "")
arcpy.Clip_analysis(inPath1, natPark, clip1, "")
arcpy.Clip_analysis(inPath2, natPark, clip2, "")


# Buffer tool, places a user defined buffer around each of the 3 input feature classes and exports to the user defined feature classes, also adds as a layer to the map
buffer1 = arcpy.Buffer_analysis(clip, outPath, bufferDistance)
buffer2 = arcpy.Buffer_analysis(clip1, outPath1, bufferDistance1)
buffer3 = arcpy.Buffer_analysis(clip2, outPath2, bufferDistance2)

# Union tool makes a single feature class by combining the 3 buffer outputs into one
arcpy.Union_analysis([buffer1, buffer2, buffer3], buffered_area, "ALL", "", "GAPS")

# Erase tool erases the Union features from the Master Shape (natPark parameter) and saves as the finalOutput parameter feature class
arcpy.Erase_analysis(natPark, buffered_area, finalOutput, "")

# Adds a layer for the Master Shape (natPark parameter)
arcpy.MakeFeatureLayer_management(natPark, layer_name1)
add_layer1 = arcpy.mapping.Layer(layer_name1)
arcpy.mapping.AddLayer(df, add_layer1)

# Adds a layer for the finalOutput parameter (the result of the erase tool)
arcpy.MakeFeatureLayer_management(finalOutput, layer_name)
add_layer = arcpy.mapping.Layer(layer_name)
arcpy.mapping.AddLayer(df, add_layer)
  • 3
    If you dont use the layer set arcpy.env.addOutputsToMap=False prior to buffer – BERA Aug 6 at 5:07
  • this does not work – Bogey-1 Aug 6 at 12:28
  • Try using a refresh active view or arcpy.RefreshTOC(). – dslamb Aug 6 at 14:50
  • Please provide an example of what outPath would contain. – Emil Brundage Aug 6 at 21:15
  • The user will just select (or create) a feature class in the geodatabase. So, for example the user will select "campsite_buffer" and the path would be C:\documents\python\week_6.gdb I can add the full script if that might help. – Bogey-1 Aug 7 at 0:48
0

Instead of name, go with the layer's catalogPath property. This will return the full path of the layer's feature class.

If outPath is the full path of the buffer output:

for layer in layerList:
    if arcpy.Describe (layer).catalogPath.lower () == outPath.lower ():
         arcpy.mapping.RemoveLayer(df, layer)

If outPath is the feature class name that gets saved in the workspace:

import os
for layer in layerList:
    if arcpy.Describe (layer).catalogPath.lower () == os.path.join (arcpy.env.workspace, clip).lower ():
         arcpy.mapping.RemoveLayer(df, layer)
  • these did not work either – Bogey-1 Aug 6 at 19:10
  • I added standardizing paths with .lower (). That might work. You might also have issues comparing paths if you use forward slashes in outPath. – Emil Brundage Aug 6 at 19:13
  • Emil, I appreciate the help but none of these methods seem to work – Bogey-1 Aug 6 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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