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.

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

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
    – geogeek
    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.
    – PolyGeo
    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).

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.
    – nicksan
    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.

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)
-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

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.