Hi,I have a script that just needs slight tweaking (I hope/think). It iterates through a workspace containing rasters, adds a new field called Proportion, then sums the raster value COUNT field in order to then determine the proportion of each raster value type. This is for land cover data - trying to calculate the proportion of land cover type per raster.I think my syntax in calculate field is not quite right:
import sys
import string
import os
try:
#for 9.2 and above
import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.AddMessage("\n" + "Using ArcMap 9.2 or above with arcgisscripting..." + "\n")
except:
#for 9.0/9.1
import win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.AddMessage("\n" + "Using ArcMap 9.0/9.1 or above with win32com.client.Dispatch..." + "\n")
#
# Check for the necessary product
Licensed = gp.ProductInfo()
gp.addmessage("You are using the following product " + str(Licensed))
try:
gp.SetProduct(str(Licensed))
gp.addmessage("Successfully set the product type to " + str(Licensed))
except:
gp.addmessage("Could not find an available ArcGIS License")
# Load required toolboxes...
gp.toolbox = "management"
# Set the Workspace
gp.Workspace = sys.argv[1]
Workspace = str(gp.Workspace)
Workspace = string.replace(Workspace, "\\", "/")
# Set the Output Folder
OutFolder = sys.argv[2]
OutFolder = str(OutFolder)
OutFolder = string.replace(OutFolder, "\\", "/")
LogFile = open(OutFolder + "/" + "log.txt", "w")
try:
#Generate a list of the grids in the grid folder
rasters = gp.ListRasters("*", "ALL")
#Iterate through each raster, add a new field called "Proportion", calculate the proportion of land cover type based on those two fields
for raster in rasters:
try:
#Add the new Proportion field
gp.addfield (raster, "PROPORTION", "FLOAT", "2")
#Use the searchcursor method to sum the values of the count field for calculating the proportions
x = 0
rows = gp.SearchCursor(raster)
row = rows.Next()
field = "COUNT"
while row:
x += row.getvalue(field)
row = rows.next()
print str(x) + " " + raster
gp.calculatefield(raster,"PROPORTION","!COUNT! / float(x)","PYTHON")
#rows = gp.SearchCursor(raster)
#row = rows.Next()
logfile.write("Just finished processing" + raster + "\n")
except:
gp.AddMessage(gp.GetMessages(2))
ErrorMsg = gp.GetMessages(2)
print ErrorMsg
LogFile.write(ErrorMsg + "\n")
except:
gp.AddMessage(gp.GetMessages(2))
ErrorMsg = gp.GetMessages(2)
logfile.write(ErrorMsg + "\n")
LogFile.close()
Specifically this part of the code: gp.calculatefield(raster,"PROPORTION","!COUNT! / float(x)","PYTHON")
I'm storing the sum of the COUNT field in the variable "x", and then want to use that in gp.calculatefield to divide the values in the COUNT field by the sum of the values stored in "x" in memory to determine the proportion of each land cover typeany help would be greatly appreciated - my programming experience is in VBA and VBscript, but I'm trying to break into Pythonthanks,Tom BurleyU.S. Geological SurveyAustin, TX