Hi,
I am attempting to put together a script (pasted in at the bottom of this missive) that will generate a constant value raster based on the extent of an input raster. The objective is to automate creation of a bounding raster band (i.e. a ribbon of constant value, one or two cells in width for the length of one side of an extent) for either mosaic-ing or multiplying related rasters to the dump to ASCII files for subsequent editing and input into USGS MODFLOW groundwater model. I'd like to make this script into tool for use in model builder, but I 'm stumped on getting the script to run successfully.
I think my issue boils down to how I am passing arguments to the script, but I'm not certain. Despite poring over the Help System, I am still feeling pretty unclear about sys.argv vs GetParmaterAsText vs GetParameter. I am definitely confused about when I need to pass a quoted string or not. I don't really understand the distinction between an object and text in the ArcGIS sense, and when one uses one or the other.
In any event, the script below works up until the point when I want it to run CreateConstantRaster, when it bugs out with either,
the very vague: ERROR 999999: Error executing function,
or the slightly less vague: ERROR 010327: Unable to set analysis window. ERROR 010067: Error in executing grid expression.
Reviewing the Help Text on these error messages was only mildly revealing.
Since I have some print calls in there I can see that my if/else statement to generate the extent parameter is working. My sense is that my parameter types in general are off. I've tried various combos of the aforementioned argument passing approaches with no success, and seemingly weird type conversions (e.g. float() and int()). The current version of the script is only one of dozens.
Any insight is welcome, thanks!
Here is my script:
# import modules
#
import arcpy, sys, os
from arcpy.sa import *
# Check out any necessary licenses
#
arcpy.CheckOutExtension("Spatial")
# the directory to work in
#
try:
workspace = arcpy.env.workspace = arcpy.GetParameterAsText(0)
except Exception as e:
print e.message
# the raster dataset to use for analysis
#
try:
raster = workspace + arcpy.GetParameterAsText(1)
except Exception as e:
print e.message
# the coordinate system of the output is set by the coordinate system of the input
#
try:
spatialRef = arcpy.Describe(raster).spatialReference
except Exception as e:
print e.message
try:
arcpy.env.outputCoordinateSystem = spatialRef
except Exception as e:
print e.message
# names for the constant raster variables
#
constantValue = float(arcpy.GetParameterAsText(2))
dataType = arcpy.GetParameterAsText(3)
cellSize = int(arcpy.GetParameterAsText(4))
outRasterName = arcpy.GetParameterAsText(5)
# define the width of the constant raster band
#
try:
desc = arcpy.Describe(raster)
except Exception as e:
print e.message
try:
extent = desc.Extent
print extent
except Exception as e:
print e.message
# assign the raster extents names
#
xminWidth = float(arcpy.GetParameter(6))
yminWidth = float(arcpy.GetParameter(7))
xmaxWidth = float(arcpy.GetParameter(8))
ymaxWidth = float(arcpy.GetParameter(9))
# calculate the width of the constant raster band to be created
#
if xminWidth > 0:
xmin = extent.XMin
ymin = extent.YMin
xmax = extent.XMin + float(xminWidth)
ymax = extent.YMax
elif yminWidth > 0:
xmin = extent.XMin
ymin = extent.YMin
xmax = extent.XMax
ymax = extent.YMin + float(yminWidth)
elif xmaxWidth > 0:
xmin = extent.XMax - float(xmaxWidth)
ymin = extent.YMin
xmax = extent.XMax
ymax = extent.YMax
else:
xmin = extent.XMin
ymin = extent.YMax - float(ymaxWidth)
xmax = extent.XMin
ymax = extent.YMax
#outExtent = Extent(xmin, ymin, xmax, ymax)
outExtent = Extent(100.123, 100.123, 100.123, 100.123)
print outExtent
# create the constant raster object
#
try:
outConstRast = CreateConstantRaster(constantValue, dataType, cellSize, outExtent)
except Exception as e:
print e.message
# save the constant raster object
#
try:
outConstRast.save(workspace + outRasterName)
except Exception as e:
print e.message