ArcGis 10.2, arcpy. I have several tools in a Python toolbox that have a feature class parameter. The parameter is defined as parameterType = “GPFeatureLayer”. When running several tools in succession the list of choices for the parameter becomes cluttered with temporary layers created from previously run tools. By temporary I mean layers created with arcpy.MakeFeatureLayer_management(). This list will sometimes include multiple occurrences of the same layer name. I would like to remove all layers from wherever they are coming from so only those layers that are in the TOC show up as choices in the tool parameters. I have tried arcpy.Delete_management() but that resulted in deleting the actual features the layer was generated from rather than the layer (OUCH!). I have also tried emptying out the scratch GDB. Thanks in advance for your input.
2 Answers
After re-reading the documentation on Internal Layers http://resources.arcgis.com/en/help/...0000000m000000, specifically:
When you create a layer or table view using geoprocessing tools, the new layer or table view is stored in an internal layer list, which is a different list from the ArcMap table of contents. This means that geoprocessing actually keeps two lists of layers and table views:
The list of layers in the ArcMap table of contents
The internal list of layers created by geoprocessing tools
I can see what I was doing wrong. I thought I was deleting the temporary layers by getting a list of layers for the data frame and deleting from that list:
def clearLayers():
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)
del mxd
This was not clearing the internal layer list (layers created by other geoprocessing tools) that the python tools use in part to generate parameter lists. Having arcpy.env.addOutputsToMap = False had no effect on the internal layer list either (which it shouldn't).
To delete from the internal list I am using arcpy.Delete_management() on the temporary layer itself as in:
tempLyr = arcpy.MakeFeatureLayer(features, layerName)
...do other stuff
arcpy.Delete_management(tempLyr, 'GPFeatureLayer')
Could you just use the mapping module to list layers in the TOC?
Something like...
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers(df)[0]
Then use this list to get names, data source, etc, and use it in a filter of your GPFeatureLayer parameter.
-
I am using the mapping module to delete the layers created by MakeFeatureLayer_management(), also setting arcpy.env.addOutputToMap = False so they are not in the TOC,but they still show up as parameters. I have not tried to filter the parameter list though. I'll give that a shot Mar 24, 2014 at 15:18
in_memory
workspace?