import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" fc = "Cities" list = [] rows = arcpy.SearchCursor(fc) for row in rows: pop = row.getValue("POPULATION") list.append(pop) print sum(list)
inWorkspace = "C:/TEMP/Tooldata/Florida.gdb" arcpy.env.workspace = inWorkspace inPoly = "C:/TEMP/Tooldata/Florida.gdb/Counties" inPoint = "C:/TEMP/Tooldata/Florida.gdb/Cities" destField = "URBPOP" msg = "Workspace is " + arcpy.env.workspace arcpy.AddMessage(msg) fcList = arcpy.ListFeatureClasses() msg = "\nFeature classes in " + inWorkspace + ":" arcpy.AddMessage(msg) for anFC in fcList: msg = " - " + anFC arcpy.AddMessage(msg) fldList = arcpy.ListFields(inPoly) msg = "\nFields in " + inPoly + ":" 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(inPoly,destField,"DOUBLE") msg = "\nNew Field " + destField + " created in " + inPoly arcpy.AddMessage(msg) arcpy.MakeFeatureLayer_management(inPoly,"allPolys") arcpy.MakeFeatureLayer_management(inPoint,"allPoints") polyRows = None polyRow = None polyRows = arcpy.UpdateCursor(inPoly) msg = '\nUpdate cursor created for ' + inPoly + '. Records are:' arcpy.AddMessage(msg) for polyRow in polyRows: firstPart = '"NAME_1" =' lastPart = "'" + '' + "'" where_clause = firstPart + lastPart arcpy.MakeFeatureLayer_management(inPoly,"oneCounty",where_clause) arcpy.SelectLayerByLocation_management("allPoints", "WITHIN", "oneCounty") list= [] fc = "allPoints" rows = arcpy.SearchCursor(fc) for row in rows: pop = row.getValue("POP_98") list.append(pop) msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + sum(list) UrbanPop= sum(list) arcpy.AddMessage(msg) polyRow.URBPOP = int(UrbanPop) arcpy.Delete_management("oneCounty") if polyRow: del polyRow if polyRows: del polyRows arcpy.Delete_management("allPolys") arcpy.Delete_management("allPoints")
msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + sum(list)
msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + str(sum(list))
for polyRow in polyRows: firstPart = '"NAME_1" =' lastPart = "'" + '' + "'" where_clause = firstPart + lastPart arcpy.MakeFeatureLayer_management(inPoly,"oneCounty",where_clause) arcpy.SelectLayerByLocation_management("allPoints", "WITHIN", "oneCounty") list= [] fc = "allPoints" rows = arcpy.SearchCursor(fc) for row in rows: pop = row.getValue("POP_98") list.append(pop) msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + sum(list) UrbanPop= sum(list) arcpy.AddMessage(msg) polyRow.URBPOP = int(UrbanPop) polyRows.updateRow(polyRow) arcpy.Delete_management("oneCounty")
func()
def func(): sum = 0 fc = "H:/GIS_Data/TEMP.gdb/points" # the path to your feature class rows = arcpy.SearchCursor(fc) for row in rows: sum = row.THEFIELDTOSUM + sum # enter the fieldname in this line return sum
inWorkspace = "C:/TEMP/Tooldata/Florida.gdb" arcpy.env.workspace = inWorkspace inPoly = "C:/TEMP/Tooldata/Florida.gdb/Counties" inPoint = "C:/TEMP/Tooldata/Florida.gdb/Cities" destField = "URBPOP" fieldPresent = False for aFld in fldList: if aFld.name == destField: fieldPresent = True if not fieldPresent: arcpy.AddField_management(inPoly,destField,"DOUBLE") msg = "\nNew Field " + destField + " created in " + inPoly arcpy.AddMessage(msg) arcpy.MakeFeatureLayer_management(inPoly,"allPolys") arcpy.MakeFeatureLayer_management(inPoint,"allPoints") polyRows = None polyRow = None polyRows = arcpy.UpdateCursor(inPoly) msg = '\nUpdate cursor created for ' + inPoly + '. Records are:' arcpy.AddMessage(msg) for polyRow in polyRows: firstPart = '"NAME_1" =' lastPart = "'" + '' + "'" where_clause = firstPart + lastPart arcpy.MakeFeatureLayer_management(inPoly,"oneCounty",where_clause) targetCities = arcpy.SelectLayerByLocation_management("allPoints", "WITHIN", "oneCounty") list= [] rows = arcpy.SearchCursor(targetCities) for row in rows: pop = row.getValue("POP_98") list.append(pop) msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + str(sum(list)) arcpy.AddMessage(msg) UrbanPop= sum(list) polyRow.URBPOP = int(UrbanPop) polyRows.updateRow(polyRow) arcpy.Delete_management("oneCounty") if polyRow: del polyRow if polyRows: del polyRows arcpy.Delete_management("allPolys") arcpy.Delete_management("allPoints")
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" env.overwriteOutput = True cities = "Cities" counties = "Counties" arcpy.MakeFeatureLayer_management(cities, "cities_feat") list = [] # Get max OBJECTID for loop rows = arcpy.SearchCursor(counties) for row in rows: OID = row.getValue("OBJECTID") 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) x += 1 del row, rows, row2, rows2