1

I already looked up topics for rewriting the erase and symmetric difference functions, but I don't get it running.

I got the following situation: My Input FC1 and FC2 are my polygon input feature classes. FC1 consists of two polygons, FC2 only of one. I want following output: enter image description here So I want two feature class outputs: OUT1 and OUT2, which looks pretty much like a symmetric difference. So I did arcpy.Union_analysis((FC1,FC2), union). After this I worked with arcpy.SelectLayerByLocation_management(union, "CONTAINS_CLEMENTINI", FC1, "", "NEW_SELECTION") (also tried "CONTAINS"), but this gives me an empty output feature class after executing arcpy.CopyFeatures_management(union, FC1_only). Normally I would proceed with arcpy.SelectLayerByLocation_management(union, "", "", "", "SWITCH_SELECTION") and then already get OUT2. Why does this not work? And is this a right way to rewrite the Erase function only available for Advanced ArcGIS?

EDIT: Ok, instead of going with the arcpy.SelectLayerByLocation_management stuff, I used the arcpy.Select_analysis on the union output with the query = '"FID_fc1" > -1 AND "FID_fc2" > -1'. Now I got the purple part shown above. What is the best way to get two separate feature classes, one with the leftovers of fc1 and one with the leftovers of fc2?

EDIT2: This is the code I have so far... I thought of maybe replacing the search cursor with an update cursor and handling the feature class output while still in the Loop by deleting the rows I don't want.... or do I have to create two queries? One with FID_fc1 > -1 AND FID_fc2 = -1 and one with FID_fc1 = -1 AND FID_fc2 > -1 ?

 elif desc.shapeType == "Polygon":
        # Initiate variables
        poly_union = outputFolder + "poly_union.shp"
        select_poly = outputFolder + "select_poly.shp"
        fid1 = ""
        fid2 = ""
        # Perform union on both polygon inputs
        arcpy.Union_analysis([new_data,old_data], poly_union)
        # Get the names of the FID fields for building the query later on
        with arcpy.da.SearchCursor(poly_union, ["NAME", "NAME_1"]) as cursor:
            for row in cursor:
                # If the NAME field is empty, just do nothing
                if row[0] == "" or row[0] == " ":
                    pass
                else:
                    # Use the name for building the FID_ field name
                    fid1 = "FID_{0}".format(row[0][0:6])
                # If the NAME_1 field is empty, just do nothing
                if row[1] == "" or row[1] == " ":
                    pass
                else:
                    # Check for the same names for the polygons to get correct FID_ field names
                    if row[0][0:6] == row[1][0:6]:
                        fid2 = "FID_{0}_1".format(row[1][0:4])
                    else:
                        fid2 = "FID_{0}".format(row[1][0:6])
        # Build the query with the extracted FID_ field names
        query = '"{0}" > -1 AND "{1}" > -1'.format(fid1,fid2)
        # Perform selection of the intersection of the polygons
        arcpy.Select_analysis(poly_union, select_poly, query)
        # ToDo: Delete intersection to get symmetric difference and output 2 feature classes
  • 3
    To select the overlap, after Union, can't you select FID_fc1 > -1 and FID_fc2 > -1. You will then Delete these Polygons because they represent the overlap between the feature classes. – klewis Jul 6 '17 at 15:05
  • @klewis I think your comment should be an answer. – PolyGeo Jul 6 '17 at 19:27
  • @klewis ok, i executed following query '"FID_fc1" > -1 AND "FID_fc2" > -1' (ArcMap told me all the time that the query was invalid, but Catalog told me that the error was elsewhere and when I fixed it, ArcMap suddenly accepted the query ... strange) The result now is only the intersecting area, how should I "delete" this now to get two separate Feature class outputs with the non-intersecting areas? – Flugmango Jul 7 '17 at 6:16
0

Ok, I got a working version... the whole trick was using the FID_ fields created by the Union_analysis function to build two queries. Each of the queries then selects one whole input feature but without every intersection. This is only possible, because the Union_analysis function creates separate polygons for the intersection areas.

elif desc.shapeType == "Polygon":
    # Initiate variables
    poly_union = outputFolder + "poly_union.shp"
    select_poly = outputFolder + "select_poly.shp"
    old_filename = basename(old_data)
    new_filename = basename(new_data)
    # Perform union on both polygon inputs
    arcpy.Union_analysis([new_data,old_data], poly_union)
    # Get the names of the FID fields for building the query later on
    fid1 = "FID_{0}".format(old_filename[0:6])
    fid2 = ""
    if old_filename[0:6] == new_filename[0:6]:
        fid2 = "FID_{0}_1".format(new_filename[0:4])
    else:
        fid2 = "FID_{0}".format(new_filename[0:6])
    # Remove old things that dont intersect
    query_only_new = '"{0}" > -1 AND "{1}" = -1'.format(fid1,fid2)
    arcpy.Select_analysis(poly_union, select_poly, query_only_new)
    arcpy.CopyFeatures_management(select_poly, new_diffs)
    # Remove new things that dont intersect
    query_only_old = '"{0}" = -1 AND "{1}" > -1'.format(fid1,fid2)
    arcpy.Select_analysis(poly_union, select_poly, query_only_old)
    arcpy.CopyFeatures_management(select_poly, old_diffs)

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.