SlopeAspectDataTip.sml

  Download

More scripts: Enhanced Data Tip

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
### SlopeAspectDataTip.sml
### Sample Display Control Script that creates an Enhanced DataTip
### for a group that includes an elevation raster (DEM) as the first layer, 
### with any type or number of other layers.  Script computes slope and
### aspect for the DEM cell under the cursor and reports them in the DataTip
### under a centered heading indicating that they are computed values.
### Formatting codes enclosed in { } are embedded in the DataTip string
### to set justification, change fonts, and set tab stops.
### Add script to group or layout using Group / Edit Control Script or
### Layout / Edit Control Script.
### Requires TNTmips v7.0
### Randy Smith, MicroImages, Inc.
### version 19 August 2004. 
## variables for DEM raster
class GRE_LAYER_RASTER DEM_layer;
class RASTER DEM;
class TRANSPARM trans;
class POINT2D ptLayer;
class COLOR bgColor;
numeric lin, col;		# line and column position
numeric w, h;			# cell size (width and height)
numeric nullDEM;		# null value for DEM
numeric dzX, dzY;		# derivatives of Z in X and Y directions
numeric slope, aspect;
string slope$, aspect$;
string slopeFmt$, aspectFmt$;
### procedure called when View is created for Group;
### use to get GRE_GROUP class instance so script
### can access the first layer and assign the DEM to
### a raster variable
proc OnGroupCreateView (
   class GRE_GROUP group				## predefined class variable
   ) {
   DEM_layer = group.FirstLayer;
	DispGetRasterFromLayer(DEM, DEM_layer);
	w = ColScale(DEM);
	h = LinScale(DEM);
	nullDEM = NullValue(DEM);
   }
### procedure called when the DataTip event is triggered
func OnViewDataTipShowText (
   class GRE_VIEW view,					## predefined class variables
   class POINT2D point,
   class TOOLTIP datatip				## predefined variable for the DataTip
   ) {
   numeric retval = 1;		### when returned by the procedure, this value suppresses
									### "standard" DataTip text and uses only that created by script
	## define background color and assign it to the DataTip;
	## set margins for the enhanced DataTip
	bgColor.Red = 78; bgColor.Green = 94; bgColor.Blue = 100;
	datatip.BackgroundColor = bgColor;
	datatip.MarginHeight = 4;
	datatip.MarginWidth = 4;
	## create centered heading in red for enhanced DataTip
   datatip.String = "{~CJ~TS12~C100,0,0~FARIALBD.TTF}Computed from DEM:{~LJ~TS13~C0,0,0}\n";
	## get transformation from screen to layer coordinates for DEM layer
	trans = view.GetTransLayerToScreen(DEM_layer, 1);
	ptLayer = trans.ConvertPoint2DFwd(point);		## cursor position in layer object coordinates
	lin = floor(ptLayer.y);		## integer line and column position of DEM cell under cursor
	col = floor(ptLayer.x);
	if ( DEM[lin,col] <> nullDEM ) {		## if cursor is not over a null cell, proceed
		## compute x and y derivatives and slope
		dzX = ( DEM[lin, col + 1] - DEM[lin, col - 1] ) / (2 * w);
		dzY = ( DEM[lin - 1, col] - DEM[lin + 1, col] ) / (2 * h);
		slope = atand( sqrt( sqr(dzX) + sqr(dzY) ) );
		slope$ = sprintf("%.1f degrees", slope);	 ## text string from slope
		## create tabbed and formatted string for slope (2nd) line in DataTip 
		slopeFmt$ = sprintf("{~TABS 6L, 18R}Slope \t{~FARIAL.TTF}= \t%s\n", slope$); 
		## compute aspect
		if (slope == 0) {			
			aspect$ = "Undefined";		## aspect is undefined for flat areas
			}
		else {
			if (dzY != 0)	{		## check for division by zero
				aspect = deg * atan2(-dzX, -dzY);
				if (aspect < 0 ) then
					aspect += 360;
				}
			else
				aspect = 90;	## when dzY = 0
			aspect$ = sprintf("%.1f degrees", aspect);	## aspect text string
			}
		## create tabbed and formatted string for aspect (3rd) line in DataTip
		aspectFmt$ = sprintf("{~TABS 6L, 18R}{~FARIALBD.TTF}Aspect \t{~FARIAL.TTF}= \t%s\n", aspect$);
		## concatenate strings to create final Enhanced DataTip string
		datatip.String = datatip.String + slopeFmt$ + aspectFmt$;
		}
	else
		retval = -1;		## when returned by procedure, this value completely suppresses the DataTip
								## in this case when cursor is over null cell (or outside DEM raster)
	return (retval);
   }