PTCOORD.sml

  Download

More scripts: Georeference

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# PTCOORD.SML
# Sample script for Getting Started: Writing Scripts with SM
clear();
#########################################################
# Class instances needed in procedures.
class GRE_GROUP gp;			# spatial group for display.
class GRE_VIEW view;			# view that displays the group.
class XmForm pcwin;		# parent form for dialog window.
class GRE_TOOL_POINT myPt;	# class for tool used to return a 3D point.
class RASTER Pan;
class GRE_LAYER_RASTER panLayer;
class VECTOR Soil;
class GRE_LAYER_VECTOR soilLayer;
class POINT2D ptscreen;		# point location in screen coordinates.
class POINT2D ptrastlayer;	# point location in raster coordinates
class POINT2D ptrastmap;	# point location in raster map coordinates.
class POINT2D ptvectlayer;	# point location in vector object coordinates.
class POINT2D ptvectmap;	# point location in vector map coordinates.
class TRANS2D_MAPGEN transViewToScreen;		# coordinate translation parameters.
class TRANS2D_MAPGEN transViewToRastlayer;
class TRANS2D_MAPGEN transViewToVectlayer;
class TRANS2D_MAPGEN transViewToRastMap;
class TRANS2D_MAPGEN transViewToVectMap;
class MAPPROJ rastmapproj;		# Coordinate system / projection parameters.
class MAPPROJ vectmapproj;
#########################################################
# Other global variables
string rastproj$, rastzone$;
string vectproj$;
string groupproj$;
#########################################################
# Define procedures.
# Procedure called when user left-clicks to set tool position
proc OnToolSet () {
	clear();
	ViewSetMessage(view,"Right-click to show coordinates in console.");
}
# Procedure called when point tool is applied by right-clicking.
proc OnToolApply () {
	# VIEW COORDINATES.
	# Read view coordinates directly from current tool position.
	# These are stored in the point tool class member myPt.Point.
	# Print view coordinates to console.
	printf("View coordinates: x = %5.2f, y = %5.2f\n",myPt.Point.x,myPt.Point.y);
	# SCREEN COORDINATES.
	# Use transformation parameters from view to screen coordinates
	# to transform current point location (returns a point class).
	ptscreen = TransPoint2D(myPt.Point,transViewToScreen);
	printf("Screen coordinates: x = %d, y = %d\n",ptscreen.x,ptscreen.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 = TransPoint2D(myPt.Point,transViewToRastlayer);
	printf("Raster layer (object) coordinates: x = %5.2f, y = %5.2f\n",ptrastlayer.x,ptrastlayer.y);
	# VECTOR LAYER (OBJECT) COORDINATES.
	# Use easier method to transform single point coordinates from view to layer.
	ptvectlayer = ViewTransPointViewToLayer(view,soilLayer,myPt.Point);
	printf("Vector layer (object) coordinates: x = %5.2f, y = %5.2f\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 = TransPoint2D(myPt.Point,transViewToRastMap);
	printf("Raster layer map coordinates: x = %5.2f, y = %5.2f \n\t in %s Zone %s\n",ptrastmap.x,ptrastmap.y,rastproj$,rastzone$);
	# VECTOR LAYER MAP COORDINATES.
	# Use transformation parameters from view to map for vector layer.
	ptvectmap = TransPoint2D(myPt.Point,transViewToVectMap);
	printf("Vector layer map coordinates: x = %5.2f, y = %5.2f \n\t in %s\n",ptvectmap.x,ptvectmap.y,vectproj$);
	# GROUP COORDINATES.
	# In a view of a single group, view coordinates are approximately equivalent to
	# group coordinates, subject to a best-fit affine transformation from map
	# coordinates to the screen.  Thus there are no functions to explicitly find
	# group coordinates.
	printf("Group coordinates = View coordinates for group view.\n");
	printf("Group coordinate system = %s\n",groupproj$); 
	# Reset status message in view window.
	ViewSetMessage(view,"Left-click in the window to locate a point.");
}
# 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() {
	DialogClose(pcwin);
	DestroyWidget(pcwin);
	CloseRaster(Pan);
	CloseVector(Soil);
}
# Procedure called when dialog is destroyed.
# Can be either by the "Close" menu item or by us
# calling DestroyWidget() above.
proc OnDestroy() {
	Exit();
}
#########################################################
# Main program.
#########################################################
##### Get input objects and create group to display them.
# Use script context to determine path for named data file.
string datafile$;
# The following segment of code is executed unless the script 
# is being run internally at MicroImages as an auto-test.
$ifndef _MI_INTERNAL_AUTOTEST_MODE  
	datafile$ = _context.ScriptDir + "/PTCOORD.RVC";
$endif
OpenRaster(Pan,datafile$,"SPOT_PAN");
OpenVector(Soil,datafile$,"CBSOILS_Lite");
gp = GroupCreate();
####################### 
# Create dialog window.
pcwin = CreateFormDialog("Find Point Coordinates");
WidgetAddCallback(pcwin.Shell.PopdownCallback, OnClose);
WidgetAddCallback(pcwin.DestroyCallback, OnDestroy);
# Create pushbutton item for Close.
class PushButtonItem btnItemClose;
btnItemClose = CreatePushButtonItem(" Close ",OnClose);
# Create button row for Close button.
class XmForm btnrow;
btnrow = CreateButtonRow(pcwin,btnItemClose);
btnrow.BottomWidget = pcwin;
btnrow.RightWidget = pcwin;
btnrow.LeftWidget = pcwin;
# Create view in pcwin form to display input raster and vector.
# A view has its own XmForm widget accessed as a class member "Form".
# It is automatically attached to the parent form at the top.
view = GroupCreateView(gp,pcwin,"",380,380,
		"NoLegendView,NoScalePosLine,DestroyOnClose");
pcwin.BottomWidget = btnrow;
# Note about attachments: The widget created and attached last gets
# resized automatically if the window is resized.  So in a window
# with a view and other widgets, create and attach the other widgets
# (such as icon buttons) first before creating and attaching the view.
# This order lets the view widget resize with the window rather than
# the last icon button or other widget. 
# Add point tool to view.
myPt = ViewCreatePointTool(view,"Point Tool","point_select","standard");
ToolAddCallback(myPt.ActivateCallback,OnToolApply);
ToolAddCallback(myPt.PositionSetCallback,OnToolSet);
#myPt.DialogPosition = "RightCenter";
ViewAddToolIcons(view);
######################
# Open window, load geodata layers, and redraw window.
DialogOpen(pcwin);
panLayer = GroupQuickAddRasterVar(gp,Pan);
soilLayer = GroupQuickAddVectorVar(gp,Soil);
ViewRedrawFull(view);
# Get map projection info from geodata layers and group.
rastproj$ = panLayer.Projection.System;
rastzone$ = panLayer.Projection.Zone;
vectproj$ = soilLayer.Projection.System;
groupproj$ = gp.Projection.System;
# Get coordinate transformation parameters.  The last parameter
# in the ViewGetTransLayerToView() function is a flag that if
# set to 1 returns the inverse transformation parameters.
transViewToScreen = ViewGetTransViewToScreen(view);
transViewToRastlayer = ViewGetTransLayerToView(view,panLayer,1);
transViewToRastMap = ViewGetTransMapToView(view,panLayer.Projection,1);
transViewToVectMap = ViewGetTransMapToView(view,soilLayer.Projection,1);
#ViewActivateTool(view,myPt);
myPt.Managed = 1;
ViewSetMessage(view,"Left-click in the window to locate a point.");
$ifndef _MI_INTERNAL_AUTOTEST_MODE
WaitForExit();
$endif
# End of script.