📄 workspacetorastercatalog.py
字号:
##""********************************************************************************************************************
##TOOL NAME: WorkspaceToRasterCatalog
##SOURCE NAME: WorkspaceToRasterCatalog.py
##VERSION: ArcGIS 9.0
##AUTHOR: Environmental Systems Research Institute Inc.
##REQUIRED ARGUMENTS: Input workspace
## Output raster catalog
## Include subdirectories
##TOOL DESCRIPTION: Insert all rasters in the input workspace and subdirectories to the output raster catalog
##
##DATE: 5/31/2004
##MODIFIED: 4/8/2005
##
##Usage: WorkspaceToRasterCatalog <Input_Directory> <Output_RasterCatalog><Include_SubDirectories>
##*********************************************************************************************************************"""
#Importing ScriptUtils which imports required librarys and creates the geoprocssing object
import win32com.client, os
msgNonExist="does not exist: "
msgSuccess="Successfully inserted: "
msgFail="Failed to insert: "
msgNotGDB="Output location is not GDB type: "
msgDir="Failed to load directory"
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
def insert(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.CopyRaster_management(in_raster_dataset, out_catalog)
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))
def pack_dir(indir):
try:
insert(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)
try:
#The input workspace
workspace = gp.GetParameterAsText(0)
#The output raster catalog
out_catalog = gp.GetParameterAsText(1)
#Whether include subdirectories
include_sub = gp.GetParameterAsText(2)
#Check existence
if not gp.Exists(workspace):
raise Exception, " %s" % (workspace) + msgNonExist
# Check if output raster catalog exists
if not gp.Exists(out_catalog):
raise Exception," %s" % (out_catalog)+ msgNonExist
if include_sub=='true':
pack_dir(workspace)
else:
insert(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 + -