Anda di halaman 1dari 3

#-------------------------------------------------------------------------------

# Name: zonalStats.py
# Purpose: Create a table holding flood depth grid zonal statistics for each
admin district.
# Summary workflow:
# 1. itearte through each admin district (23 total)
# 2. convert selected admin district to feature layer
# 3. make a table to hold the selected admin districts zonal stats
# 4. run zonal stats for each flood depth grid (484 total) within
# the selected districts feature layer
# 5. append zonal stats from each grid to the districts zonal stats
# table (step #3).
#
# NEXT: convert output dbf tables to csv for statistical analysis in R
#
# Author: mbs7038
#
# Created: 02/05/2014
# Copyright: (c) mbs7038 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import arcpy
import os
from arcpy import env
from arcpy.sa import*
# raster workspace
arcpy.env.workspace=r"C:\Users\mbs7038\Documents\Remove_Gauge_Outliers\CountryAn
alysis\AvgDepth\depth_grids_raw.gdb"
arcpy.env.overwriteOutput=True
arcpy.CheckOutExtension("Spatial")
# List depth grid rasters
gridList=arcpy.ListRasters("*")
newTabpath=r"C:\Users\mbs7038\Documents\EDA"
tablePath=r"C:\Users\mbs7038\Documents\EDA\zonTabs"
# global field to add to all new tables
addField="DATENUM"
adminDist=r"C:\Users\mbs7038\Documents\EDA\district_Test.gdb\BGD_adm2"
adminLyr=adminDist+"_lyr"
# ==============================================================================
# Setup index for selecting each administrative district for zonal stats (OBJECT
ID 1-23)
print 'beginning admin district loop'
i=1
for i in range(24):
print i
# Select district with OBJECT ID == i & make a layer from the feature class
print 'selecting district & converting to layer'
distLyr=arcpy.MakeFeatureLayer_management(adminDist, adminLyr, "\"OBJECTID\"
={}".format(str(i)))
# Select the current feature layer
arcpy.SelectLayerByAttribute_management(distLyr, "NEW_SELECTION")
# Get the name of the selected admin district
print 'applying search cursor'
dists=arcpy.SearchCursor(distLyr)
for dist in dists:
distName=dist.getValue("NAME_2")
print distName
# ======================================================================
# Create district table for holding zonal statistics
for g in range(1):
print gridList[g]
polyfield="ID_0"
try:
saveTable=tablePath + "/" + "zonTab_" + gridList[g][:-4]
newTableName=distName+".dbf"
#Calculate zonal statistics
print 'running zonal stats on {0}'.format(gridList[g])
outRow=arcpy.sa.ZonalStatisticsAsTable(distLyr,polyfield,
gridList[g],saveTable,"DATA","ALL")
#Create Table
print 'creating new zonal table based off schema from 1st zonal
table {0}'.format(saveTable)
arcpy.CreateTable_management(newTabpath,newTableName,saveTable,
"")
except:
print arcpy.GetMessages()
# Get the fullpath to the newly created district table
finalTable=os.path.join(newTabpath,newTableName)
# add field that will hold the serial date number
print 'adding "SerialDateNum" field to final table'
arcpy.AddField_management(finalTable,addField,"DOUBLE",6,"","","","")
# ======================================================================
# Run zonal statistics for all flood depth grids within corresponding
# district.
# Add field to hold serial date number.
# Append each zonal stat table to the district table.
rastCount=len(gridList)
print 'There are {0} depth grids'.format(rastCount)
# sort the depth grid list
gridList.sort()
# create grid index
for g in range(484):
print g
print gridList[g]
polyfield="ID_0" # field to calculate zonal stats on
try:
#Calculate zonal statistics
print 'running zonal stats on {0}'.format(gridList[g])
outRow=arcpy.sa.ZonalStatisticsAsTable(distLyr,polyfield,
gridList[g],saveTable,"DATA","ALL")
#add field to hold serial date number
print 'adding "SerialDateNum" field to {0}'.format(saveTable)
arcpy.AddField_management(saveTable,addField,"DOUBLE",6,"","",
"","")
# get serial date number from matching feature class
print 'getting serial date number string'
num=gridList[g][1:-4]
print str(num)
# add serial date number to "DATENUM" field
print 'adding serial date number'
arcpy.CalculateField_management(saveTable,addField,str(num),
"PYTHON_9.3")
#Append to final output table
print 'appending table: {0}'.format(finalTable)
arcpy.Append_management(saveTable,finalTable,"NO_TEST","","")
except:
print arcpy.GetMessages()
print 'script is complete'