1

I have attached my code (Python). I am trying to loop trough each FC in same file GDB and subtract a FC from remaining FCs not equal to input FC. Then want to create/generate FC from erased result function. Also, I want to give specific FC name to the output. I can get to the end of my script, except last line.

Example: GDB has [A,B,C,D] - 4 FCs.
1st round:
input FC: A (Do not use A-A)
Output should be A-B and called Product_By_AB
should be A-C and called Product_By_AC
should be A-D and called Product_By_AD
2nd round:
input FC:B (Do not use B-B)
Output should be B-A and called Product_By_BA
should be B-C and called Product_By_BC
should be B-D and called Product_By_BD
and so on.

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'C:\input.gdb'

Match_FC = arcpy.ListFeatureClasses()

for fclass in Match_FC:
  for fc in Match_FC:
    if fc != fclass:
        fclass_lyr = arcpy.MakeFeatureLayer_management(fclass, "fclass_lyr")
        fc_lyr = arcpy.MakeFeatureLayer_management(fc, "fc_lyr")
        arcpy.SelectLayerByLocation_management(fclass_lyr, 'INTERSECT', fc_lyr)
        desc = arcpy.Describe("fclass_lyr")
        fcIntersect=[]
        if desc.FIDSet:

             output='{}'.format(fclass)

            arcpy.Erase_analysis(fclass, fc,output, '')


        else:
           arcpy.Delete_management(fclass_lyr)
        arcpy.Delete_management(fc_lyr)

    else:
        "Skipping FC-{}".format(fc)
  • Your indenting is inconsistent. You need to format your code properly to follow the looping. As it stands your code is unreadable. Do you have any specific error mesage? I don't understand why Matched=fclass[len(fclass)-20:] .. surely that doesn't exist in the database. – Michael Stimson May 5 '16 at 2:57
3

If I'm understanding what you're trying to do, the following modification to your code should do the trick.

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'C:\input.gdb'

Match_FC = arcpy.ListFeatureClasses()

for fclass in Match_FC: # Loop through the feature classes you want to erase from
    print "Feature Class = {}".format(fclass)
    for fc in Match_FC: # Loop through the erasing feature classes
        if fc != fclass: # If they aren't the same feature class
            print "Erase Feature Class = {}".format(fc)
            output = r'C:\output.gdb\Product_by_{}{}'.format(fclass, fc) # Name the output feature class
            arcpy.Erase_analysis(fclass, fc, output, '') # Erase the features and store in the output
        else: # If they are they same feature class, then skip
            print "Skipping matching Feature Class {}".format(fc)

Basically it

  1. loops through all your feature classes to erase from,
  2. then inside that loop it loops through the same FCs to find the FCs to use to erase.
  3. If they're not the same FC then perform the erase into a new output FC (using the naming convention you specified),
  4. or if they are they same FC then skip it.

You don't need to use Feature Class to Feature Class as the Erase tool already outputs to a new feature class for you.


To check for overlap and skip if no overlap, try this:

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'C:\input.gdb'

Match_FC = arcpy.ListFeatureClasses()

for fclass in Match_FC:
    print "Feature Class = {}".format(fclass)
    for fc in Match_FC:
        if fc != fclass:
            print "Erase Feature Class = {}".format(fc)

            fclass_lyr = arcpy.MakeFeatureLayer_management(fclass, "fclass_lyr")
            fc_lyr = arcpy.MakeFeatureLayer_management(fc, "fc_lyr")
            arcpy.SelectLayerByLocation_management(fclass_lyr, 'INTERSECT', fc_lyr)
            desc = arcpy.Describe("fclass_lyr")
            if desc.FIDSet:
                output = r'C:\output.gdb\Product_by_{}{}'.format(fclass, fc)
                arcpy.Erase_analysis(fclass, fc, output, '')
            else:
                print "Skipping - No Features Overlap"
            arcpy.Delete_management(fclass_lyr)
            arcpy.Delete_management(fc_lyr)

        else:
            print "Skipping matching Feature Class {}".format(fc)
  • Could you help me how can I add condition and lets tool check before executing Erase function check for if my polygons are overlapped or no, then execute the erase for just overlaps FC. – Koush May 5 '16 at 5:27
  • adding a Select Features By Location before your erase, and then check for selected features. If nothing selected then don't erase, if something is selected then clear the selection and do the erase. I'll try to update my answer to include – Midavalo May 5 '16 at 5:30
  • Should I put after: if fc != fclass: # and indent inside or no! – Koush May 5 '16 at 5:32
  • 1
    yes inside the if fc != fclass: , so indented. Should be Select Layer by Location (not Feature like I said in my earlier comment). You'll need to Make Feature Layer – Midavalo May 5 '16 at 5:34

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.