3
\$\begingroup\$

I am running a script to identify polygons that overlap with other polygons within the same layer. I have managed to do this with the code below but the problem is that it is very slow (about 3 seconds per feature).

Is there anything I can do to improve its performance?

arcpy.CreateFeatureclass_management("results.gdb", "Overlapping_Polygons", "POLYGON", "lyr_existing_cadastre", "DISABLED", "DISABLED", arcpy.Describe("lyr_existing_cadastre").spatialReference)

arcpy.MakeFeatureLayer_management("results.gdb/Overlapping_Polygons", "lyr_Overlapping_Polygons")

arcpy.CreateFeatureclass_management("results.gdb", "Remaining_Polygons", "POLYGON", "lyr_existing_cadastre", "DISABLED", "DISABLED", arcpy.Describe("lyr_existing_cadastre").spatialReference)

arcpy.MakeFeatureLayer_management("results.gdb/Remaining_Polygons", "lyr_Remaining_Polygons")

arcpy.CreateFeatureclass_management("in_memory", "row", "POLYGON", "lyr_existing_cadastre", "DISABLED", "DISABLED", arcpy.Describe("lyr_existing_cadastre").spatialReference)

arcpy.MakeFeatureLayer_management("row", "lyr_row")

arcpy.AddSpatialIndex_management("lyr_existing_cadastre")

with arcpy.da.SearchCursor("lyr_existing_cadastre", ["OBJECTID"]) as search_cursor:
    for row in search_cursor:
        log("\t {}".format(row[0]))
        # Get the OBJECTID of row and use it to select row
        arcpy.SelectLayerByAttribute_management("lyr_existing_cadastre", "NEW_SELECTION", "\"OBJECTID\" = " + str(row[0]))

        # Copy row into a new feature class called row
        arcpy.Append_management("lyr_existing_cadastre", "lyr_row", "NO_TEST")

        # Remove row from the input
        arcpy.DeleteFeatures_management("lyr_existing_cadastre")

        # Select all features that intersect with row and copy them into the row feature class
        arcpy.SelectLayerByLocation_management("lyr_existing_cadastre", "WITHIN", "lyr_row", selection_type="NEW_SELECTION")
        arcpy.Append_management("lyr_existing_cadastre", "lyr_row", "NO_TEST")

        # If there is more than one feature in the row feature class
        # meaning there was something else in addition to row then delete those overlaps
        # from the input and append everything in row into the output feature class
        if (int(arcpy.GetCount_management("lyr_row").getOutput(0)) > 1):
            arcpy.DeleteFeatures_management("lyr_existing_cadastre")
            arcpy.Append_management("lyr_row", "lyr_Overlapping_Polygons", "NO_TEST")
        else:
            arcpy.Append_management("lyr_row", "lyr_Remaining_Polygons", "NO_TEST")

        arcpy.SelectLayerByAttribute_management("lyr_existing_cadastre", "CLEAR_SELECTION")
        arcpy.TruncateTable_management("lyr_row")
del search_cursor
\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Feb 1 at 13:13
  • 2
    \$\begingroup\$ Thank you. I have made the edits. I hope it's better now. \$\endgroup\$ – Bhekani Khumalo Feb 1 at 13:32

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.