4

Let's say that I have 4 features classe:

  • Layer1
  • Layer2
  • Layer3
  • Layer4

I want to erase my LayerX with all the LayerYwhere Y<X. So for example the erase feature for the Layer3 will be Layer2 and Layer1.

I'm using the function Erase_analysis.

Erase_analysis (in_features, erase_features, out_feature_class)

I don't want to create tousand of new layers, so during my iteration I try to use the same input name and output name.

My code using arcpy:

arcpy.env.overwriteOutput = True

for x in n:
    for y in n[:(x-1)]:
        arcpy.Erase_analysis('layer'+str(x), 'layer'+str(y), 'layer'+str(x))

But, and it was expected, the python consol give me this error

Output Feature Class is same as Input Features, Failed to execute (Erase).

Is there a solution using arcpy ?

Is there a more pythonic way to solve this problem ?

2

Copying your input to in_memory, prior to using Erase, should work:

for x in n:
    for y in n[:(x-1)]:
        arcpy.CopyFeatures_management('layer'+ str(x), "in_memory\layer" + str(x))
        arcpy.Erase_analysis("in_memory\layer" + str(x), 'layer'+ str(y), 'layer'+ str(x))
1

You can use arcpy.Delete_management to delete the input after the erase. I also think merging the erasefeatures prior to erase will speed everything up:

import arcpy

arcpy.env.workspace = r'C:\database.gdb'
arcpy.env.overwriteOutput = 1
featurelist = arcpy.ListFeatureClasses()

for feature in featurelist:
    mergelist=[i for i in featurelist if int(i[-1])<int(feature[-1])]
    arcpy.Merge_management(inputs=mergelist, output=r'in_memory\merge')
    arcpy.Erase_analysis(in_features=feature, erase_features=r'in_memory\merge', 
                        out_feature_class='{0}_erased'.format(feature))
    arcpy.Delete_management(feature)

Your Answer

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

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