📄 workspacetorasterdataset.py
字号:
##""********************************************************************************************************************
##TOOL NAME: WorkspaceToRasterDataset
##SOURCE NAME: WorkspaceToRasterDataset.py
##VERSION: ArcGIS 9.0
##AUTHOR: Environmental Systems Research Institute Inc.
##REQUIRED ARGUMENTS: Input workspace
## Output raster dataset
## Include subdirectories
##OPTIONAL ARGUMENTS: Mosaic method (defaults to LAST)
## Color map mode
## Ignore background value
## Nodata value
## Convert one bit data to 8 bit data during mosaic?
## Tolerance
##TOOL DESCRIPTION: Mosaic all image files in the input workspace and subdirectories to the output raster dataset
##
##DATE: 5/31/2004
##MODIFIED: 4/8/2005
##
##Usage: WorkspaceToRasterDatset <Input_Directory> <Output_RasterDataset><Include_SubDirectories>[Mosaic_Method]
## [Color_Map_Mode][Ignore_Background_Value][Nodata_Value][Convert_1_to_8_bit][Tolerance]
##*********************************************************************************************************************"""
#Importing ScriptUtils which imports required librarys and creates the geoprocssing object
import win32com.client, os
msgNonExist="does not exist: "
msgSuccess="Successfully loaded: "
msgFail="Failed to load: "
msgNotGDB="Output location is not GDB type: "
msgDir="Failed to load directory: "
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
# The mosaic function mosaics all of the images files within the current workspace into
# the output raster dataset.
def mosaic(ws):
try:
gp.workspace = ws
#The raster datasets in the input workspace
in_raster_dataset_list = gp.ListRasters()
#Loop through the array copying each raster to the output workspace
in_raster_dataset = in_raster_dataset_list.next()
while in_raster_dataset <> "":
try:
gp.Mosaic_management(in_raster_dataset,out_raster, mosaic_method,colormap,ignore_bg,nodata_value,one_to_8,tolerance)
gp.AddMessage(msgSuccess + " %s " % (in_raster_dataset))
except Exception, ErrorDesc:
# Except block for the loop. If the tool fails to convert one of the Rasters, it will come into this block
# and add warnings to the messages, then proceed to attempt to convert the next input Raster.
WarningMessage = (msgFail + " %s" %(ws + os.sep + in_raster_dataset))
gp.AddWarning(WarningMessage)
in_raster_dataset = in_raster_dataset_list.next()
gp.AddMessage(msgSuccess + " %s " % (ws))
except Exception, ErrorDesc:
gp.AddWarning(msgFail + " %s " % (ws))
## The pack_dir function recursively traverses the subdirectories of the current
## workspace mosaicking all of the image files it finds.
def pack_dir(indir):
try:
mosaic(indir)
for sub_dir in os.listdir(indir):
if os.path.isdir(indir + os.sep + sub_dir):
curdir = indir + os.sep + sub_dir
pack_dir(curdir)
except Exception, ErrorDesc:
gp.AddWarning(msgDir + " %s " % (indir))
try:
#The input workspace
workspace = gp.GetParameterAsText(0)
#The output raster dataset
out_raster = gp.GetParameterAsText(1)
#Whether include subdirectories
include_sub = gp.GetParameterAsText(2)
#Optional mosaic method (defaults to LAST)
mosaic_method = gp.GetParameterAsText(3)
#Optional color map mode
colormap = gp.GetParameterAsText(4)
#Optional ignore background value
ignore_bg = gp.GetParameterAsText(5)
# Optional nodata value
nodata_value = gp.GetParameterAsText(6)
# Convert one bit data to 8 bit data during mosaic?
one_8 = gp.GetParameterAsText(7)
if one_8 == "true":
one_to_8 = "OneBitTo8Bit"
else:
one_to_8 = "NONE"
# Mosaicking tolerance
tolerance = gp.GetParameterAsText(8)
# Derive raster dataset for connectivity in model builder
gp.SetParameterAsText(9,out_raster)
#Check existence
if not gp.Exists(workspace):
raise Exception, " %s" % (workspace) + msgNonExist
# Check if output raster dataset exists
if not gp.Exists(out_raster):
raise Exception," %s" % (out_raster)+ msgNonExist
# If subdirectories are to be searched, recursively traverse the subdirectories
# mosaicking all image files found. Otherwise just mosaic the image files found
# in the current workspace.
if include_sub=="true":
pack_dir(workspace)
else:
mosaic(workspace)
except Exception, ErrorDesc:
# Except block if the tool could not run at all.
# For example, not all parameters are provided, or if the output path doesn't exist.
gp.AddError(str(ErrorDesc))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -