Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

In order to do this, I have to create a 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.

share|improve this question
    
thanks a lot PolyGeo for your tips, i've changed the "photo.mxd" to "CURRENT" to debug in Arcmap, it works. but to execute from command line i still have the problem –  geogeek Jul 31 '12 at 0:17
add comment

4 Answers

up vote 13 down vote accepted

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

share|improve this answer
    
Thanks Jason - worked like a charm. When I get enough reputation, I'll thank you with an upvote. –  nicksan Feb 15 '11 at 18:55
    
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 Jun 22 '12 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. –  Jason Scheirer Jun 23 '12 at 22:30
    
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 Aug 14 '12 at 0:57
add comment

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

share|improve this answer
    
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 Feb 15 '11 at 19:02
add comment

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)
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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