DEBUG.sml

  Download

More scripts: Advanced

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# DEBUG.SML
# sample script for tutorial "Writing Scripts with SML"
# Version of VIEWSHED.SML showing use of $define, $ifdef, $endif
# preprocessor commands to define a DEBUG mode during script development.
# Computes binary viewshed raster from elevation Raster input
# use sample 8-bit input elevation raster CB_TM.RVC / ELEVATION
# use sample input vector object VIEWSHED.RVC / VROAD
# computes viewshed for all points on all lines
# of a corresponding Vector object
# could use to get viewshed for all points
# on one or more roads
### Declarations
array numeric xArray[10];	 # use to hold line points
array numeric yArray[10];
raster Rin, Rout;
vector V;
class GEOREF georefR, georefV; 
numeric numLines, numPoints;
numeric thisLine, numPointsInThisLine;
class MATRIX matX, matY;
numeric currentPoint, i;
numeric xVector, yVector, rCol, rLine;
numeric percent, height, zScale;
####
clear(); 	# clear the console
$define DEBUG
# get the input and output raster and Vector object
GetInputRaster( Rin );
GetInputVector( V );
GetOutputRaster( Rout, NumLins( Rin ), NumCols( Rin ), "binary" );
# get georef object id's for raster and vector
georefR = GetLastUsedGeorefObject( Rin );
georefV = GetLastUsedGeorefObject( V );
# first count total number of points in all lines
numLines = NumVectorLines( V );
numPoints = 0;
for thisLine = 1 to numLines {
	numPointsInThisLine = GetVectorLinePointList( V, xArray, yArray, thisLine );
	numPoints += numPointsInThisLine;
	$ifdef DEBUG
		printf( "Line= %d, NumVertices= %d\n", thisLine, numPointsInThisLine );
	$endif
	} 
# now create matrices large enough to hold points
matX = CreateMatrix(1, numPoints );
matY = CreateMatrix(1, numPoints );
# now loop through lines and fill in matrices with points
currentPoint = 0; 	# this will be our matrix index
for thisLine = 1 to numLines {
	numPointsInThisLine = GetVectorLinePointList( V, xArray, yArray, thisLine );
	for i = 1 to numPointsInThisLine {
		# convert vector coordinates to georef;
		ObjectToMap( V, xArray[i], yArray[i], georefV, 
					xVector, yVector );
		# convert vector coordinates to raster coordinates
		# note the order of rCol and rLine
		# col = x coord, row = y coord
		MapToObject( georefR, xVector, yVector, 
					Rin, rCol, rLine );
		# force to upper left corner
		rLine = floor( rLine ); 
		rCol = floor( rCol );
		# want matrix to index from zero to numPoints
		SetMatrixItem( matX, 0, currentPoint, rCol );
		SetMatrixItem( matY, 0, currentPoint, rLine );
		$ifdeg DEBUG
			printf( "i= %d, xVect= %.4f, yVect= %.4f, rLine= %d, rCol= %d\n", i, xVector, yVector, rLine, rCol );
		$endif
	
		currentPoint += 1;
		} # end of for i
	} # end of for each line in V
	# set default values - want any one point to make "visible"
	percent = 100 / numPoints;
	height = 2;
	zScale = 1;
	# now create the viewshed Raster object
	RasterToBinaryViewshed(  Rin, Rout, numPoints, matX, matY, percent, height, zScale );
	# clean up
	GeorefFree( georefR );	 # must free up id's
	GeorefFree( georefV );
	DestroyMatrix( matX );
	DestroyMatrix( matY );
	CloseRaster( Rin );
	CloseRaster( Rout );