Thursday, 16 March 2017

Overwrite the input feature with the output feature with ArcPy


Let's say that I have 4 features classe:



  • Layer1

  • Layer2

  • Layer3

  • Layer4


I want to erase my LayerX with all the LayerYwhere Y. 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 ?



Answer



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))

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...