Solved! Go to Solution.
rows = arcpy.SearchCursor(featureClass) row = rows.next() for row in rows:
rows = arcpy.SearchCursor(featureClass) row = rows.next() for row in rows:
import arcpy import os # Set environment settings outWorkspace = r"C:\Project\_Forums\_json\fgdb\crawlAHN2.gdb" arcpy.env.workspace = outWorkspace arcpy.env.overwriteOutput = True # reference to the data fc = os.path.join(outWorkspace, "crowns") raster = os.path.join(outWorkspace, "AHN2r") zoneField = "ID_OK" # temporal output ZS table will be (over)written to in memory workspace outTable = os.path.join("IN_MEMORY", "stat_zs") # name of feature layer (lives in memory) flyr_name = 'a_crown' # get list of zones (this is used for the loop) lst_zones = [row[0] for row in arcpy.da.SearchCursor(fc, (zoneField))] # create an empty dictionary to store the statistics results dct_stats = {} # create a dictionary that holds the statistics type and the corresponding output field names dct_stat_fld = {'MEAN': 'cr_mean', 'MAX': 'cr_max', 'MIN': 'cr_min', 'STD': 'cr_std'} # list of fields in ZS table that hold the statistics I'm interested in ['MEAN','MIN','MAX','STD'] lst_flds = [fld for fld in dct_stat_fld.iterkeys()] # get sa license arcpy.CheckOutExtension("Spatial") # now loop through zones and make a featurelayer with only that zone feature (crown) cnt = 0 for zone in lst_zones: cnt += 1 # give some feedback on progress if cnt % 10 == 0: print "Processing crown: {0} ({1})".format(cnt, zone) # create the where clause for that crown where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(fc, zoneField), zone) # make the featurelayer of current crown (zone) arcpy.MakeFeatureLayer_management(fc, flyr_name, where) # calculate the zonal statistics for that crown arcpy.sa.ZonalStatisticsAsTable(flyr_name, zoneField, raster, outTable, "NODATA", "ALL") # read the ZS table and write to dictionary with arcpy.da.SearchCursor(outTable, lst_flds) as curs: for row in curs: i = 0 for fld in lst_flds: key = (zone, fld) # looks like ('crown ID', 'statistics type like MIN') dct_stats[key] = row i += 1 # return sa license arcpy.CheckInExtension("Spatial") # create update field collection (field for the update cursor) flds = [zoneField] flds.extend([dct_stat_fld[fld] for fld in lst_flds]) # do an update cursor to write the results from dictionary to crown featureclass with arcpy.da.UpdateCursor(fc, flds) as curs: for row in curs: zone = row[0] i = 1 for fld in lst_flds: key = (zone, fld) if key in dct_stats: row = dct_stats[key] i += 1 curs.updateRow(row)