import arcpy arcpy.env.workspace = r"C:\temp\python\test.gdb" arcpy.env.overwriteOutput = True cities = "Cities" counties = "Counties" city_layer = "cities_feat" county_layer = "counties_feat" arcpy.MakeFeatureLayer_management(cities, city_layer) for row in arcpy.SearchCursor(counties): county_OID = row.OBJECTID arcpy.MakeFeatureLayer_management(counties, county_layer, 'OBJECTID = ' + str(county_OID)) arcpy.SelectLayerByLocation_management(city_layer, 'WITHIN', county_layer) list2 = [r.POPULATION for r in arcpy.SearchCursor(city_layer)] sumlist = sum(list2) county_rows = arcpy.UpdateCursor(county_layer ) for layer_row in county_rows: layer_row.URBPOP = sumlist county_rows.updateRow(row2) del county_rows, row, layer_row
import arcpy from arcpy import env cities = "Cities" counties = "Counties" destField= "URBPOP" env.workspace = "C:\TEMP\Tooldata\Florida.gdb" env.overwriteOutput = True fldList = arcpy.ListFields(counties) msg = "\nFields in " + counties + ":" arcpy.AddMessage(msg) for aFld in fldList: msg = " - " + aFld.name arcpy.AddMessage(msg) fieldPresent = False for aFld in fldList: if aFld.name == destField: fieldPresent = True if not fieldPresent: arcpy.AddField_management(counties,destField,"DOUBLE") msg = "\nNew Field " + destField + " created in " + counties arcpy.AddMessage(msg) arcpy.MakeFeatureLayer_management(cities, "cities_feat") list = [] rows = arcpy.SearchCursor("Counties") for row in rows: OID = row.getValue("NAME_1") list.append(OID) maxOID = list[-1] del row, rows x = 1 while x <= maxOID: list2 = [] arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x)) arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat") rows = arcpy.SearchCursor("cities_feat") for row in rows: pop = row.getValue("POP_98") list2.append(pop) sumlist = sum(list2) rows2 = arcpy.UpdateCursor("counties_feat") for row2 in rows2: row2.URBPOP = sumlist rows2.updateRow(row2) msg = " - " + row2.NAME_1 + " - " + "Urban Population: " + str(sumlist) arcpy.AddMessage(msg) x += 1 del row, rows, row2, rows2 arcpy.Delete_management("counties_feat") arcpy.Delete_management("cities_feat")
while x <= maxOID: list2 = [] arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x)) arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat") rows = arcpy.SearchCursor("cities_feat") for row in rows: pop = row.getValue("POP_98") list2.append(pop) sumlist = sum(list2) rows2 = arcpy.UpdateCursor("counties_feat") for row2 in rows2: row2.URBPOP = sumlist rows2.updateRow(row2) msg = " - " + row2.NAME_1 + " - " + "Urban Population: " + str(sumlist) arcpy.AddMessage(msg) x += 1 del row, rows, row2, rows2 arcpy.Delete_management("counties_feat") arcpy.Delete_management("cities_feat")
Try placing the syntax to delete the cursors outside of the while loop:while x <= maxOID: list2 = [] arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x)) arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat") rows = arcpy.SearchCursor("cities_feat") for row in rows: pop = row.getValue("POP_98") list2.append(pop) sumlist = sum(list2) rows2 = arcpy.UpdateCursor("counties_feat") for row2 in rows2: row2.URBPOP = sumlist rows2.updateRow(row2) msg = " - " + row2.NAME_1 + " - " + "Urban Population: " + str(sumlist) arcpy.AddMessage(msg) x += 1 del row, rows, row2, rows2 arcpy.Delete_management("counties_feat") arcpy.Delete_management("cities_feat")
try: del row, rows, row2, rows2 except NameError: pass
I ran into a situation where the results of an UpdateCursor was empty. After a "for row in rows:" loop, the "del row" generated and error and prevents the script from finishing. I tried using the None object without sucess, i.e. "if row == None: del row". I'm pretty basic when it comes to python, so this try statement really helped me out. Thanks!
I need to automatically calculate a field based on values from previous rows?
I need to automatically calculate a field based on values from previous rows such that once the total of all the previous rows is greater than 450 and less than 500, the next row starts
counting from 0 or the balance of what made the previous row greater than 500.
The script below just calculates the sum of the cities point that falls within the county, I want something that would make the value of a row (i.e. row2) the addition of the value of row1 + value of row2 and so on till when it gets to 500, where it starts from 1 again. Kindly help me please.
cities = "Cities" counties = "Counties" destField= "URBPOP" env.workspace = "C:\Users\KUNLE\Documents\ArcGIS\Default.gdb" env.overwriteOutput = True fldList = arcpy.ListFields(counties) msg = "\nFields in " + counties + ":" arcpy.AddMessage(msg) for aFld in fldList: msg = " - " + aFld.name arcpy.AddMessage(msg) fieldPresent = False for aFld in fldList: if aFld.name == destField: fieldPresent = True if not fieldPresent: arcpy.AddField_management(counties,destField,"DOUBLE") msg = "\nNew Field " + destField + " created in " + counties arcpy.AddMessage(msg) arcpy.MakeFeatureLayer_management(cities, "cities_feat") list = [] rows = arcpy.SearchCursor("Counties") for row in rows: OID = row.getValue("POP") list.append(OID) maxOID = list[-1] del row, rows x = 1 while x <= maxOID: list2 = [] arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x)) arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat") rows = arcpy.SearchCursor("cities_feat") for row in rows: pop = row.getValue("POPULATION") list2.append(pop) sumlist = sum(list2) rows2 = arcpy.UpdateCursor("counties_feat") for row2 in rows2: row2.URBPOP = sumlist rows2.updateRow(row2) #msg = " - " + row2.POP + " - " + "Urban Population: " + str(sumlist) #arcpy.AddMessage(msg) x += 1 del row, rows, row2, rows2 arcpy.Delete_management("counties_feat") arcpy.Delete_management("cities_feat")