VIEWGD.sml

  Download

More scripts: SML Fundamentals

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# VIEWGD.sml
# Sample script for Getting Started: Writing Scripts with SML
# Demonstrates using a graphic tool in a view to access information
# from the geodata layers in the view.
# Requires TNTmips version 2008:74 or higher.
# Requires input objects in VIEWGD.rvc.
# version 4 December 2009.
clear();
#########################################################
# Class instances needed in procedures.
class GRE_GROUP gp;			# spatial group for display.
class GRE_VIEW view;			# view that displays the group.
class GUI_DLG dlgview;		# dialog class instance for the dialog window
class GRE_TOOL_POINT myPtTool;		# point tool added to view
class GUI_CTRL_EDIT_STRING msgText;		# text control to display messages
class RVC_RASTER DEM;
class RVC_VECTOR Soil;
class RVC_GEOREFERENCE georefDEM, georefSoil;	# georeference info for the input objects
class SR_COORDREFSYS groupCRS, crsDEM, crsSoil;
class GRE_LAYER_RASTER demLayer;			# class for raster layer
class GRE_LAYER_VECTOR soilLayer;		# class for vector layer
numeric err;		# variable for error checking
#########################################################
# Define procedures.
# Procedure called when tool is activated (button on)
proc OnToolActive ()
	{
	myPtTool.Managed = 1;
	msgText.SetValueStr("Left-click in the window to place the point tool.");
	}
# Procedure called when tool is deactivated (button off)
proc OnToolDeactivated ()
	{
	myPtTool.Managed = 0;
	msgText.SetValueStr("");
	}
# Procedure called when user left-clicks to set tool position
proc OnToolSet () 
	{
	# unhighlight any polygon previously highlighted
	soilLayer.UnhighlightAllElements(1);
	msgText.SetValueStr("Right-click to select the enclosing polygon.");
	}
# Procedure called when point tool is applied by right-clicking.
proc OnToolApply () 
	{
	clear();	# clear the console
	local class POINT2D point;
	local numeric inverse = 1;
	local numeric elementNum;
	local class STRING soilSymbol$, soilName$;
	# get current point tool location in screen coordinates
	point = myPtTool.Point;
	# transform point position from screen to view coordinates
	class TRANS2D_MAPGEN transScreenToView;
	transScreenToView	= view.GetTransViewToScreen(inverse);
	point = transScreenToView.ConvertPoint2DFwd(point);
	# Transform point position from view to vector object coordinates of the soil layer.
	point = view.TransPointViewToLayer(soilLayer, point);
	# find the element number of the polygon enclosing the current point position
	elementNum = FindClosestPoly(Soil, point.x, point.y);
	printf("Element number of the selected polygon is %d\n", elementNum);
	# highlight the polygon exclusively in the view
	soilLayer.Poly.HighlightSingle(elementNum, "Replace");
	# get some attribute values of the selected polygon and print to the console
	soilSymbol$ = Soil.Poly[elementNum].DESCRIPTN.SYMBOL$;
	soilName$ = Soil.Poly[elementNum].DESCRIPTN.NAME$;
	printf("Soil symbol for this polygon = %s\n", soilSymbol$);
	printf("Description: %s\n", soilName$);
	# make a region from the selected polygon; requires old-style GEOREF class for Soil vector
	# to get region in map coordinates
	local class REGION2D reg;
	local class GEOREF georef = GetLastUsedGeorefObject(Soil);
	reg = ConvertVectorPolyToRegion(Soil, elementNum, georef);
	reg.CoordRefSys = crsSoil;
	printf("CRS of region = %s\n", reg.CoordRefSys.Name); 
	# reproject the region to the CRS of the DEM
	reg.ConvertTo(crsDEM);
	printf("Region converted to %s\n", reg.CoordRefSys.Name);
	# get DEM cell values in the region and compute the average elevation for the soil polygon
	local numeric lin, col;
	local numeric sum, count, average;
	for each DEM[lin, col] in reg
		{ 
		++count;
		sum += DEM[lin, col];
		}
	if (count <> 0) then
		average = sum / count; 
	printf("Number of DEM cells in polygon = %d\n", count);
	printf("Average elevation for polygon = %.1f meters\n", average);
	msgText.SetValueStr("Left-click in the window to position the point tool.");
	}	# end OnToolApply
# Procedure to close window.
# Called when the Close button is pressed or the "X"
# close icon button on the title bar is pressed.
proc onClose() 
	{
	dlgview.Close(0);
	Exit();
	}
########################################################################
### Procedure called when the view dialog window opens
proc onViewOpen()
	{
	view.Redraw();
	}
#####################################################################
### Procedure called when View dialog window is initialized.  Used to create
### view within the window and to create a group to view the selected raster.
proc OnInitDialog () 
	{
	local class GUI_LAYOUT_PANE viewpane; 	# class instance for layout pane to contain the view
	local class WIDGET viewpaneWidget;		# class for widget for layout pane to serve as parent for the view
	viewpane = dlgview.GetPaneByID("viewpane");	# get handle for layout pane from dialog
	viewpaneWidget = viewpane.GetWidget();			# get Xm widget for pane
	gp = GroupCreate();
	demLayer = GroupQuickAddRasterVar(gp, DEM);	# add the Pan raster to the group
	soilLayer = GroupQuickAddVectorVar(gp, Soil);	# add the Soil vector to the group
	# create 2D view of group in dialog using viewpane's widget as parent
	view = gp.CreateView(viewpaneWidget, "View", 400, 200, "NoDftMarkButtons,NoStatusBar,NoLegend");
	# set white as background color for view
	class COLOR color;
	color.red = 100;	color.green = 100;	color.blue = 100;
	view.BackgroundColor = color;
	# add point tool with icon
	myPtTool = ViewCreatePointTool(view, "Point Tool", "point_select", "standard");
	view.AddToolIcons();			# adds the icon button to the toolbar
	myPtTool.Managed = 1;	# set to be the active tool when dialog opens 
	# set callbacks procedures for tool
	ToolAddCallback(myPtTool.ManagedCallback, OnToolActive);
	ToolAddCallback(myPtTool.UnmanagedCallback, OnToolDeactivated);
	ToolAddCallback(myPtTool.PositionSetCallback, OnToolSet);
	ToolAddCallback(myPtTool.PositionClearedCallback, OnToolDeactivated);
	ToolAddCallback(myPtTool.ApplyCallback, OnToolApply);
	# get handle for the message text control
	msgText = dlgview.GetCtrlByID("msgText");
	msgText.SetValueStr("Left-click in the window to locate a point.");
	}	# end OnInitDialog
######################  MAIN PROGRAM  ###############################
##### Get input objects from the accompanying Project File.
# Use script context to determine path for named data file. 
string datafile$ = _context.ScriptDir + "/VIEWGD.RVC";
OpenRaster(DEM, datafile$, "DEM");
OpenVector(Soil, datafile$, "CBSOILS");
# get Coordinate Reference System information for the raster and vector objects
DEM.GetDefaultGeoref(georefDEM);
Soil.GetDefaultGeoref(georefSoil);
crsDEM = georefDEM.GetCoordRefSys();
crsSoil = georefSoil.GetCoordRefSys();
printf("CRS of DEM: %s\n", crsDEM.Name);
printf("CRS of Soil Layer: %s\n", crsSoil.Name);
class STRING xml$;	# string variable containing the dialog specification in XML
xml$ = '<?xml version="1.0"?>
	<root>
		<dialog id="dlgview" Title="Select Polygon" Buttons="" HorizResize="Expand" VertResize="Expand" OnOpen="onViewOpen()">
			<pane id="viewpane" Orientation = "vertical" HorizResize="Expand" VertResize="Expand"/>
			<pane Orientation="horizontal" HorizAligh="right" HorizResize="Expand" VertResize="Expand">
				<edittext id="msgText" Width="50" ReadOnly="true"/> 
			</pane>
			<pane Orientation="horizontal" HorizAlign="right" HorizResize="Expand" VertResize="Expand">
				<pushbutton Name="  Close  " VertResize="Fixed" VertAlign="Bottom" OnPressed="onClose()"/> 
			</pane>
		</dialog>
	</root>';
# parse XML text for the dialog into memory; 
# return an error code (number < 0 ) if there are syntax errors
class XMLDOC doc;		# class instance for the parsed XML text containing the view dialog specification
err = doc.Parse(xml$);
if (err < 0) 
	{
	PopupError(err); 	# Popup an error dialog. "Details" button shows syntax errors.
	Exit();
	}
# get the dialog element from the parsed XML document and
# show error message if the dialog element can't be found
class XMLNODE dlgnode;	# class instance for the dialog node in the XML structure for the dialog window
dlgnode = doc.GetElementByID("dlgview");
if (dlgnode == 0) 
	{
	PopupMessage("Could not find dialog node in XML document");
	Exit();
	}
# Set the XML dialog element as the source for the GUI_DLG class instance
# we are using for the dialog window.
dlgview.SetXMLNode(dlgnode);
dlgview.SetOnInitDialog(OnInitDialog);
# create and open the view as a modeless dialog so results can be written to console.
dlgview.CreateModeless();
dlgview.Open();
WaitForExit();