5

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.

6
  • 1
    What version is the toolbox that these tools are in? It seems not like it is not 10.x.
    – Barbarossa
    Mar 22, 2014 at 14:25
  • ArcGIS 10.2.1 Python 2.7 Mar 22, 2014 at 19:50
  • I am unaware of a GPFeatureClass. Do you mean GPFeatureLayer?
    – Barbarossa
    Mar 22, 2014 at 21:38
  • Have you tried clearing the in_memory workspace?
    – Paul
    Mar 22, 2014 at 22:24
  • Yes, the parameter type is GPFeatureLayer. My mistake. Thanks for pointing that out. I did try clearing the in_memory workspace which did not have the desired effect. Mar 23, 2014 at 9:58

2 Answers 2

3

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')
1
  • 1
    404 on your link for internal layers. For 10.2: here
    – gm70560
    Mar 25, 2014 at 14:59
0

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.

1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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