UdateCursor with Select By Location

2123
17
Jump to solution
12-11-2019 12:05 PM
2Quiker
Occasional Contributor II

I am trying to do a select by location and then update the selected features, seems easy enough but i am see to get it figured out. Basically i want to update only the parcels that are within the City limits layer and update the 4 fields. I have tried different variations but i can't seem to get to work. I don't get any errors and also doesn't stop. I would appreciate any help.

Here is what i have.

import arcpy
from datetime import datetime as d
startTime = d.now()
start_time = time.time()

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/Temp/blah.gdb"

fc1 = "C:/Temp/blah.gdb/Parcels"

CITY = "C:/Temp/blah.gdb/City_Limits"

with arcpy.da.UpdateCursor(fc1, ['field1','field2','field3', 'field4']) as cursor:
    for row in cursor:
        #Select all parcels within the city limits layer
        arcpy.SelectLayerByLocation_management(fc1,"HAVE_THEIR_CENTER_IN", CITY)
        result = arcpy.GetCount_management(fc1)
        row [0] =  "IN CITY"
        row [1] =  "IN CITY"
        row [2] =  "IN CITY"
        row [3] =  "IN CITY"
        cursor.updateRow(row)
    del row, cursor
    
print('Done')
try:
    print ('(Elapsed time: ' + str(d.now() - startTime)[:-3] + ')')

except Exception as e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print ("Line %i" % tb.tb_lineno)
    print (e.message)
0 Kudos
17 Replies
DanPatterson_Retired
MVP Emeritus

Not my script

0 Kudos
by Anonymous User
Not applicable

Whoops
Sorry Dan and 2 Quicker

0 Kudos
JoeBorgione
MVP Emeritus

Just curious: Why declare count outside the def, and then make it global inside the def?

That should just about do it....
0 Kudos
by Anonymous User
Not applicable

Umm, no reason.  Since i was coding on the fly i didn't really think about it.  By rights you are correct and that it should be inside the def to make the code cleaner.  Then it would not require the global call.

JoeBorgione
MVP Emeritus

Okay cool...  I avoid global variables and it just kind of jumped out at me!

That should just about do it....
0 Kudos
by Anonymous User
Not applicable

I usually do as well, but i just finished scripting a fairly interesting project where i had to pass variables between three different scripts for threading.  So i guess that i had globals on the brain.

2Quiker
Occasional Contributor II

Thanks everyone that replied. I was able to come with the following code that works for me.

fyi, Michael Boyce your code did work by the way.

import arcpy
from datetime import datetime as d
startTime = d.now()
start_time = time.time()

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/Temp/blah.gdb"

fc1 = "C:/Temp/blah.gdb/Parcels"
arcpy.MakeFeatureLayer_management(fc1, "fc1Lyr")

CITY = "C:/Temp/blah.gdb/City_Limits"
arcpy.MakeFeatureLayer_management(CITY, "CityLyr")

with arcpy.da.SearchCursor("CityLyr", ["SHAPE@"]) as cursor:
    for row in cursor:
        #SELECT PROPERTIES BY CITYLIMITS
        selection = arcpy.SelectLayerByLocation_management("fc1Lyr","HAVE_THEIR_CENTER_IN", "CityLyr")
        result = int(arcpy.GetCount_management("fc1Lyr").getOutput(0)) 
        with arcpy.da.UpdateCursor(selection, ['field1','field2','field3', 'field4']) as cursor:
            for row in cursor:
                # Select all parcels within the city limits layer
                row[0] = "IN CITY"
                row[1] = "IN CITY"
                row[2] = "IN CITY"
                row[3] = "IN CITY"
                cursor.updateRow(row)
            del row, cursor

try:
    print ('(Elapsed time: ' + str(d.now() - startTime)[:-3] + ')')
    print ('Number of features processed: ' + str(result))

except Exception as e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print ("Line %i" % tb.tb_lineno)
    print (e.message)
by Anonymous User
Not applicable

Very nice

Good to see you got a solution

Just a pointer to make your code more error proof:
At the moment the only things in the try/except statement are the print statements. 

This means that an exception will only be called if something goes wrong with the print calls, but an exception will not be handled in any of the calls above.
Either move the try to the top, around line 14 or define a function and place everything in it then call the function inside the try statement at the bottom. 

This way if something goes wrong you will be able to debug a whole lot better.

Happy coding