Select to view content in your preferred language

Python and Relates

1905
5
Jump to solution
10-30-2014 10:33 AM
CynthiaKozma
Frequent Contributor

I am new to Python, so this may have a simple answer.  This is something that I used to write in AML in 10 minutes.  I want to relate a feature class to a table, select all records in the feature class - which would select related records in the table, do a switch selection on the table and create a new feature from the table.  My script does what it is supposed to when I use a small sample set, but totally crashes ArcMap so that it closes completely about 1/4 of the way through the full (13,000) records.  I believe it has something to do with the nested cursors that I have going.  How can I keep it from crashing and closing and finish the script?  Any help would be great!  Or if there is an easier way to do a relate in python??

This is the piece where is does a "relate" and ArcMap closes..........

# Create a search cursor to loop through the building_pt_sewer table

arcpy.SelectLayerByAttribute_management("building_clip", "NEW_SELECTION", "OBJECTID > 0")

buildings = arcpy.SearchCursor("building_clip")

parc_field = "parcels_ASSESSOR_N_1"

for building in buildings:

    #Get Parcel Number of building

    building_ID = building.getValue(parc_field)

    # Create search cursor for looping through the buildings

    sewers = arcpy.SearchCursor(DestinationTable)

    sewer_field = "parcel_id"

    for sewer in sewers:

        #Get parc of sewer

        sewer_parc = sewer.getValue(sewer_field)

        query = "parcel_id = " + "'%s'" %building_ID

        print query

        arcpy.SelectLayerByAttribute_management(DestinationTable, "ADD_TO_SELECTION", query)

  #     break

del buildings, sewers

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Also, there is some good information on this topic in StackExchange:  How can I more efficiently select related records?

View solution in original post

0 Kudos
5 Replies
JoshuaBixby
MVP Esteemed Contributor

Are you using ArcGIS 10.1 or greater?  If so, I encourage you to give the cursors in the arcpy data access (arcpy.da) module a try.  The data access cursors are more robust and may not crash as easily.

CynthiaKozma
Frequent Contributor

We do...we are running 10.2.1.  I will check that out!  Thank you!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Also, there is some good information on this topic in StackExchange:  How can I more efficiently select related records?

0 Kudos
CynthiaKozma
Frequent Contributor

That worked like a charm --- and was about 100x faster processing!  Thank you!

JoshuaBixby
MVP Esteemed Contributor

Glad it worked out for you.  I used that same discussion last year to sort through a similar issue, figured it better to point you there than repeat it all here.  Cheers.

0 Kudos