I am creating a python tool which must be run in and open MXD in ArcMap (10.2, ArcFM). The MXDs we use have a LOT of layers, and often multiple layers from the same datasource. In the script I need to intersect two layers, and use the resulting layer in a selection by location (so it must be added as a layer when created). After the tool has completed the analysis I would like to remove the extra layers from the editor's TOC so they can continue working unimpeded, but not delete the feature class.

Example of geoprocessing function, where Main and Buff are layers selected from the MXD as tool inputs. I would like to remove "IntersectingGasMain" from the TOC at the end of the tool:

MainBuff_Int = arcpy.Intersect_analysis([Main, Buff], "temp_intersect")

So far what I have found is that this can be accomplished by iterating through the layers in the active dataframe, comparing the layer datasource property to the result object output:

myOut = MainBuff_Int.getOutput(0) # this equals C:\Workspace\test.gdb\"temp_intersect"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.acriveDataFrame
for layer in arcpy.mapping.ListLayers(mxd, "", df):
    if layer.dataSource == myOut:
        arcpy.mapping.RemoveLayer(df, layer)

While I do realize that adding a wildcard in the list layers line would reduce what I have to cycle through, I'm really looking for how to do this using the result object or referencing the featureclass/layer specifically without having to search through the TOC at all... is this possible?

Additionally I can only use funcionality available at the basic license level as the editors cannot access advanced licenses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.