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()
gp.calculatefield(raster,"PROPORTION","!COUNT! / float(x)","PYTHON")
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", "LONG", "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! / " + long(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()