Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# PTCOORD2.sml
# Sample script for Getting Started: Writing Scripts with SML
# Demonstrates coordinate transformations between coordinate systems
# used by graphic tools, the view, and view layers and their contained
# spatial objects.
# Requires TNTmips version 2008:74 or higher.
# Requires input objects in PTCOORD2.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 Pan;
class RVC_VECTOR Soil;
class RVC_GEOREFERENCE georefPan, georefSoil; # georeference info for the input objects
class STRING crsPan$, crsSoil$;
class SR_COORDREFSYS groupCRS;
class GRE_LAYER_RASTER panLayer; # 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 locate a point.");
}
# 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 ()
{
msgText.SetValueStr("Right-click to show coordinates in console.");
}
# Procedure called when point tool is applied by right-clicking.
proc OnToolApply ()
{
clear(); # clear the console
local class POINT2D ptview; # point location in screen coordinates.
local class POINT2D ptrastlayer; # point location in raster object coordinates
local class POINT2D ptrastmap; # point location in raster map coordinates.
local class POINT2D ptvectlayer; # point location in vector object coordinates.
local class POINT2D ptvectmap; # point location in vector map coordinates.
# SCREEN COORDINATES
# A graphic tool returns position in coordinates of the drawing area of the
# view (screen coordinates) in pixels with an origin at the upper left corner.
printf("Screen coordinates: x = %5.1f, y = %5.1f pixels from upper left.\n", myPtTool.Point.x, myPtTool.Point.y);
# Get coordinate transformation parameters. The numeric parameter
# in the each method is a flag that if set to 1 returns the
# inverse transformation parameters.
local numeric inverse = 1;
class TRANS2D_MAPGEN transScreenToView;
transScreenToView = view.GetTransViewToScreen(inverse);
class TRANS2D_MAPGEN transViewToRastlayer;
transViewToRastlayer = view.GetTransLayerToView(panLayer, inverse);
class TRANS2D_MAPGEN transViewToRastMap;
transViewToRastMap = view.GetTransMapToView(georefPan.GetCoordRefSys(), inverse);
class TRANS2D_MAPGEN transViewToVectMap;
transViewToVectMap = view.GetTransMapToView(georefSoil.GetCoordRefSys(), inverse);
# VIEW COORDINATES
# For a single group view, view coordinates are the group map coordinates.
# Use inverse of View to Screen transformation to transform current point location.
ptview = transScreenToView.ConvertPoint2DFwd(myPtTool.Point);
printf("View coordinates: x = %5.1f, y = %5.1f\n", ptview.x, ptview.y);
# RASTER LAYER (OBJECT) COORDINATES
# Use transformation parameters from view to raster layer (object)
# coordinates to transform current point location.
# General method to get object coordinates for point, line, or region data.
ptrastlayer = transViewToRastlayer.ConvertPoint2DFwd(ptview);
printf("Raster layer (object) coordinates: x = %5.1f, y = %5.1f\n", ptrastlayer.x, ptrastlayer.y);
# VECTOR LAYER (OBJECT) COORDINATES.
# Use easier method to transform single point coordinates from view to layer
# using method on GRE_VIEW.
ptvectlayer = view.TransPointViewToLayer(soilLayer, ptview);
printf("Vector layer (object) coordinates: x = %5.1f, y = %5.1f\n",ptvectlayer.x,ptvectlayer.y);
# RASTER LAYER MAP COORDINATES.
# Use transformation parameters from view to map for raster layer.
# to transform current point location.
ptrastmap = transViewToRastMap.ConvertPoint2DFwd(ptview);
printf("Raster layer map coordinates: x = %5.1f, y = %5.1f \n\t in %s \n",
ptrastmap.x, ptrastmap.y, crsPan$);
# VECTOR LAYER MAP COORDINATES.
# Use transformation parameters from view to map for vector layer.
ptvectmap = transViewToVectMap.ConvertPoint2DFwd(ptview);
printf("Vector layer map coordinates: x = %5.1f, y = %5.1f \n\t in %s\n",
ptvectmap.x, ptvectmap.y, crsSoil$);
groupCRS = gp.GetUsedCoordRefSys();
printf("Group Coordinate Reference System = \n\t %s\n", groupCRS.Name);
msgText.SetValueStr("Left-click in the window to locate a point.");
} # 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();
panLayer = GroupQuickAddRasterVar(gp, Pan); # 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 + "/PTCOORD2.RVC";
OpenRaster(Pan, datafile$, "SPOT_PAN");
OpenVector(Soil, datafile$, "CBSOILS");
# get Coordinate Reference System information for the raster and vector objects
Pan.GetDefaultGeoref(georefPan);
Soil.GetDefaultGeoref(georefSoil);
crsPan$ = georefPan.GetCoordRefSys().Name;
#crsSoil$ = sprintf("%s / %s", georefSoil.GetCoordRefSys().Datum.GetAbbr(), georefSoil.GetCoordRefSys().Name );
crsSoil$ = georefSoil.GetCoordRefSys().Name;
class STRING xml$; # string variable containing the dialog specification in XML
xml$ = '<?xml version="1.0"?>
<root>
<dialog id="dlgview" Title="Show Point Coordinates" 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();