15

I have an ArcPy based script where I'm trying to intersect two layers where one layer must have its centroid in the other layer.

Since I need to know that one layer (feature_layer below) includes the centroid of the other, I'm using the arcpy.SelectLayerByLocation_management() tool.

I do not have a Map Document (*.mxd) open so in order to do this, I have to use Make Feature Layer to feed into it.

The documentation notes that the feature layer will disappear when the program exits, but I need it to disappear sooner since I have it in a loop similar to below.

for feature_class in large_list_of_feature_classes:
 some_function(feature_class)
def some_function():
 feature = "{path to feature class}"
 feature_layer = "feature_layer"
 HUCs_layer = "all_HUCs" #HUCs being Hydrologic Unit Codes
 
 arcpy.MakeFeatureLayer_management(feature, feature_layer)
 arcpy.MakeFeatureLayer_management(feature, HUCs_layer) 
 arcpy.SelectLayerByLocation_management(HUCs_layer, "HAVE_THEIR_CENTER_IN", feature_layer, selection_type="NEW_SELECTION")
 arcpy.CopyFeatures_management(HUCs_layer, selection_name)

I'm looking for a function to remove feature layers and I haven't been able to find one.

I realize I could put some sort of count in there or something to make each one unique within the scope of the program, but that's not ideal unless there is no way to remove feature layers.

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Feb 15, 2011 at 1:10
0

5 Answers 5

22

The Delete tool should accept the name of a feature layer to remove it from the TOC.

Permanently deletes data from disk. All types of geographic data supported by ArcGIS, as well as toolboxes and workspaces (folders, geodatabases), can be deleted. If the specified item is a workspace, all contained items are also deleted.

and under the Syntax section lists the Data Types that can be deleted as:

Data Element; Graph; Layer; Table View

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Feb 15, 2011 at 2:00
3
  • i don't know why i should use the delete tool twice to be sure that the feature layers is removed, and sometimes this function gimme errors, despite the feature layer not really removed , this errors occurs when i try to delete and create feature layers with the same name Commented Jun 22, 2012 at 19:43
  • 2
    This isn't really related, but it's the way geoprocessing works -- it expects each layer to have a unique name because that's how it refers to them. Commented Jun 23, 2012 at 22:30
  • 3
    Perhaps the use of the Delete tool to remove a layer from the TOC could be better documented at resources.arcgis.com/en/help/main/10.1/index.html#//… - it says Layer is a data type that can be deleted, but no other references are made to layers and its opening sentence "Permanently deletes data from disk" would scare many users away from trying Delete to remove a layer. Commented Aug 14, 2012 at 0:57
7

You can invoke arcpy.mapping.RemoveLayer to remove a layer from the TOC.

Provides the ability to remove a layer within a data frame in a map document (.mxd).

jbalk
7,6971 gold badge19 silver badges42 bronze badges
answered Feb 15, 2011 at 5:39
1
  • Thanks for the suggestion - I didn't clarify that I was looking for something to do this outside of the mapping context - this is just an analysis script and I don't have (or at least don't know) an explicit TOC. Jason's suggestion below worked though. Thanks! It'll still be a useful function for me later on. Commented Feb 15, 2011 at 19:02
4

Use mxd.save() if you're using a map document that is not CURRENT to make sure the layer stays gone. Also, add a print statement in your script:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for i in arcpy.mapping.ListLayers(mxd , "TerraColor_SanFrancisco_US_15m.tif"):
 print "Deleting layer", i
 arcpy.mapping.RemoveLayer(df , i)
arcpy.RefreshActiveView()

Just to make sure it's finding a layer to delete at all.

answered Jul 31, 2012 at 2:07
3

This is what I use to remove layers.

arcpy.mapping.RemoveLayer("DataFrame", "Layer Name")

Or looping:

mxd = arcpy.mapping.MapDocument("CURRENT")
for df in arcpy.mapping.ListDataFrames(mxd):
 for lyr in arcpy.mapping.ListLayers(mxd, "", df):
 arcpy.mapping.RemoveLayer(df, lyr)
answered May 17, 2011 at 9:46
-1

try this:

import arcpy
from arcpy import env
env.workspace = r"F:\Projects\ohad\derech_atankim\gis" 
for mxdname in arcpy.ListFiles("*.mxd"):
 print mxdname 
 mxd = arcpy.mapping.MapDocument(r"F:\Projects\ohad\derech_atankim\gis\\" + mxdname)
 df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
 for lyr in arcpy.mapping.ListLayers(mxd, "", df):
 if lyr.dataSource == r"F:\Projects\ohad\derech_atankim\gis\layers\roads.lyr":
 arcpy.mapping.RemoveLayer(df, lyr)
 mxd.save()
del mxd
answered Jul 13, 2014 at 10:46

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.