More scripts: Raster
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# PipelineResampleToGeoref.sml
# Sample standalone script to demonstrate a simple image pipeline application:
# resampling/reprojecting a source raster to match its georeference at the
# specified cell size.
# 21 December 2007
# revised 13 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);
}
##################################### Main program ##############################
clear();
# set context so script does not exit on error, allowing manual
# handling of errors.
_context.AbortOnError = 0;
# CHOOSE INPUT RASTER to be resampled.
class RVC_OBJITEM riObjItem; # objItem for input raster
DlgGetObject("Select raster to resample:", "Raster", riObjItem, "ExistingOnly");
# PIPELINE SOURCE
# set input raster as source
class IMAGE_PIPELINE_SOURCE_RVC source_In( riObjItem );
err = source_In.Initialize();
if (err < 0)
ReportError(_context.CurrentLineNum, err);
else
print("Pipeline source initialized.");
printf("Source image has %d lines and %d columns.\n", source_In.GetTotalRows(), source_In.GetTotalColumns() );
# CHECK THAT SOURCE HAS VALID COORDINATE REFERENCE SYSTEM
class IMAGE_PIPELINE_GEOREFERENCE sourceGeoref;
sourceGeoref = source_In.GetGeoreference();
# get coordinate reference system from the source georeference
class SR_COORDREFSYS crs; # input raster's coordinate reference system
crs = sourceGeoref.GetCRS();
if (crs.IsDefined() == 0 or crs.IsLocal() ) {
PopupMessage("Source coordinate reference system is undefined or local; exiting script.");
Exit();
}
else
printf("Coordinate reference system: %s\n", crs.Name );
# CHOOSE OUTPUT RASTER
# DlgGetObject creates only an ObjItem 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 rastOutObjItem;
DlgGetObject("Choose raster for resampled output", "Raster", rastOutObjItem, "NewOnly");
# get line and column cell sizes from source's georeference
class POINT2D scaleIn; # line and column cell sizes as x and y values of POINT2D
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 image cell sizes: line = %.2f m, col = %.2f m\n", scaleIn.y, scaleIn.x);
# PROMPT USER TO ENTER DESIRED OUTPUT LINE AND COLUMN CELL SIZES
numeric lineCellSize, colCellSize; # specified cell size for the output raster
string prompt$ = "Enter desired line cell size for output raster:";
lineCellSize = PopupNum(prompt$, scaleIn.y, 0, 1000, 2);
prompt$ = "Enter desired column cell size for output raster:";
colCellSize = PopupNum(prompt$, scaleIn.x, 0, 1000, 2);
# SET RESAMPLING METHOD BASED ON RELATIVE CELL SIZE OF INPUT AND OUTPUT
numeric inCellArea, outCellArea;
inCellArea = scaleIn.x * scaleIn.y;
outCellArea = colCellSize * lineCellSize;
string rsmpMethod$;
if (outCellArea > 2 * inCellArea) then rsmpMethod$ = "Average";
else rsmpMethod$ = "CubicConvolution";
printf("Resampling method: %s\n", rsmpMethod$);
# PIPELINE FILTER to resample source image
class IMAGE_PIPELINE_FILTER_RESAMPLE filter_rsmp(source_In, crs, lineCellSize, colCellSize, rsmpMethod$);
err = filter_rsmp.Initialize();
if (err < 0)
ReportError(_context.CurrentLineNum, err);
else
print("Resample filter initialized.");
# PIPELINE TARGET: set up the target for the pipeline
class IMAGE_PIPELINE_TARGET_RVC target_rvc(filter_rsmp, rastOutObjItem);
target_rvc.SetCompression("DPCM", 0);
err = target_rvc.Initialize();
if (err < 0)
ReportError(_context.CurrentLineNum, err);
else
print("Pipeline target initialized.");
# EXECUTE pipeline process
print("Processing...");
target_rvc.Process();
print("Done.");