More scripts: Raster
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# PipelineResampleToGeorefMulti.sml
# Sample standalone script to demonstrate a simple image pipeline application:
# Reprojecting a series of source rasters with control-point georeference to
# align their lines/columns with their coordinate reference system and maintain
# their current cell size
# Script shows how to incorporate an image processing pipeline into a user-defined
# function that is iteratively called to process a series of grayscale rasters
# through the same processing steps, with each iteration producing a single
# output raster.
# 21 February 2008
# Randy Smith, MicroImages, Inc.
# Requires Version 2007:73 or later of the TNT products.
numeric err; # error code returned
# error checking procedure
proc ReportError(numeric linenum, numeric err) {
printf("FAILED -line: %d, error: %d\n", linenum, err);
PopupError(err);
}
numeric i; # loop counter
##################################################################################
################################### Procedures #################################
# function to process input rasters through the resampling pipeline
func rsmpPipe(
class RVC_OBJITEM inObjItem,
class RVC_OBJITEM outObjItem,
numeric count,
class STRINGLIST list)
{
local class STRING imgName$ = list.GetString(count-1);
printf("Processing source image %s\n", imgName$ );
# PIPELINE SOURCE
# set input raster as source
local class IMAGE_PIPELINE_SOURCE_RVC source_In( inObjItem );
err = source_In.Initialize();
if (err < 0)
return -1;
else
printf("Pipeline source for %s initialized\n", imgName$);
# CHECK THAT SOURCE HAS VALID COORDINATE REFERENCE SYSTEM
local class IMAGE_PIPELINE_GEOREFERENCE sourceGeoref;
sourceGeoref = source_In.GetGeoreference();
# get coordinate reference system from the source georeference
local class SR_COORDREFSYS crs; # input raster's coordinate reference system
crs = sourceGeoref.GetCRS();
if (crs.IsDefined() == 0 or crs.IsLocal() ) {
return 0;
}
else {
printf("Coordinate reference system for source %s: %s\n", imgName$, crs.Name );
# get line and column cell sizes from source's georeference
local class POINT2D scaleIn; # line and column cell sizes as x and y values of POINT2D
local class POINT2D locIn; # column and line location for which to obtain cell size
locIn.x = source_In.GetTotalColumns() / 2; # location at center of image
locIn.y = source_In.GetTotalRows() / 2;
sourceGeoref.ComputeScale(locIn, scaleIn, 1); # cell sizes in meters
printf("Source %s image cell sizes: line = %.2f m, col = %.2f m\n", imgName$, scaleIn.y, scaleIn.x);
# PIPELINE FILTER to resample source image
class IMAGE_PIPELINE_FILTER_RESAMPLE filter_rsmp(source_In, crs, scaleIn.y, scaleIn.x, "Nearest");
err = filter_rsmp.Initialize();
if (err < 0)
return -1;
else
printf("Resample filter for source %s initialized.\n", imgName$);
# PIPELINE TARGET: set up the target for the pipeline
class IMAGE_PIPELINE_TARGET_RVC target_rvc(filter_rsmp, outObjItem);
target_rvc.SetCompression("DPCM", 0);
err = target_rvc.Initialize();
if (err < 0)
return -1;
else
printf("Pipeline target for image %s initialized.\n", imgName$);
# EXECUTE pipeline process
print("Processing...");
target_rvc.Process();
printf("Completed processing for image %s.\n\n", imgName$);
return 1;
}
}
####################################################################################
##################################### Main program ###############################
clear();
# set context so script does not exit on error, allowing manual
# handling of errors.
_context.AbortOnError = 0;
# CHOOSE GEOREFERENCED INPUT RASTERS to be resampled.
class RVC_OBJITEM inObjItemList[]; # HASH of RVC_OBJITEMS for unknown number of input rasters to mosaic
numeric numInputs;
# DlgGetObjects populates an RVC_OBJITEM hash with the RVC_OBJITEM of each selected raster
DlgGetObjects("Choose georeferenced rasters to resample:", "Raster", inObjItemList, "ExistingOnly", 2);
numInputs = inObjItemList.GetNumItems();
printf("Processing %d input rasters.\n", numInputs);
# MAKE STRINGLIST OF LABELS FOR DIALOG PROMPTING FOR OUTPUT RASTERS
class STRINGLIST labelList;
for i = 1 to numInputs
{
labelList.AddToEnd( inObjItemList[i].GetDescriptor().GetShortName() );
}
# CHOOSE OUTPUT RASTERS
# DlgGetObjectSet populates a hash of ObjItems for the output; the new raster will then be created
# by the TARGET_RVC with the desired compression. The ObjectPath of the ObjItem is not
# valid until the output raster is actually created by the pipeline process.
class RVC_OBJITEM outObjItemList[];
DlgGetObjectSet("Choose rasters for resampled output", "Raster", labelList, outObjItemList, "NewOnly");
# LOOP THROUGH LISTS OF INPUT AND OUTPUT RVC_OBJITEMS TO RESAMPLE IMAGES
for i = 1 to numInputs
{
# USER-DEFINED FUNCTION TO RESAMPLE AN IMAGE
err = rsmpPipe(inObjItemList[i], outObjItemList[i], i, labelList);
if (err == 0) # invalid coordinate reference system for input
printf("Source image %d has undefined or local coordinater reference system; \n no resampled raster was made.\n\n", i);
else if (err < 0)
ReportError(_context.CurrentLineNum, err);
}
print("Done.");