Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# OBJMAP.sml
# sample script for tutorial "Writing Scripts with SML.
# Demonstrates how to get the object-map coordinate transformation
# parameters for a georeferenced object and how to transform points
# or extents rectangle coordinates between these systems.
# 5 November 2009
clear();
class RVC_RASTER Rast;
class RVC_VECTOR Vect;
GetInputRaster(Rast);
GetInputVector(Vect);
# get georeference and coordinate reference system for input raster object
class RVC_GEOREFERENCE rGeoref;
Rast.GetDefaultGeoref(rGeoref);
printf("Raster georeference type = %s\n", rGeoref.GetGeorefType() );
printf("Raster CRS = %s\n", rGeoref.GetCoordRefSys().Name);
# get georeference and coordinate reference system for input vector object
class RVC_GEOREFERENCE vGeoref;
Vect.GetDefaultGeoref(vGeoref);
printf("Vector georeference type = %s\n", vGeoref.GetGeorefType() );
printf("Vector CRS = %s\n\n", vGeoref.GetCoordRefSys().Name);
# get extents of the input raster in object (line and column) coordinates
class RECT3D extents;
Rast.GetExtents(extents);
# get coordinate transformation from raster object to map coordinates from the georeference
# Value 0 for the second parameter of the GetTransParm() class method sets an
# object-to-map transformation; value 1 sets map-to-object transformation.
# The method also requires the transformation model used in the georeference (such as
# affine, plane projective, etc.), so simply read the model used in the georeference.
class TRANS2D_MAPGEN rTransObjToMap;
rGeoref.GetTransParm(rTransObjToMap, 0, rGeoref.GetCalibModel() );
# get map coordinates for lower left corner of the raster extents;
# because the origin for raster object coordinates is the upper left corner,
# this is x1, y2 in the raster extents rectangle
class POINT2D objPt, mapPt;
objPt.x = extents.x1; objPt.y = extents.y2;
mapPt = rTransObjToMap.ConvertPoint2DFwd(objPt);
print("Raster extents corner coordinates:");
printf("Lower left: ObjX = %d, ObjY = %d, MapX = %.2f, MapY = %.2f\n",
objPt.x, objPt.y, mapPt.x, mapPt.y);
# get map coordinates for upper right corner of the raster exents
# (x2, y1 in the raster extents rectangle)
objPt.x = extents.x2; objPt.y = extents.y1;
mapPt = rTransObjToMap.ConvertPoint2DFwd(objPt);
printf("Upper right: ObjX = %d, ObjY = %d, MapX = %.2f, MapY = %.2f\n",
objPt.x, objPt.y, mapPt.x, mapPt.y);
# convert raster extents RECT3D to map coordinates and convert
# center map coordinates back to object coordinates
extents = rTransObjToMap.ConvertRectFwd(extents);
mapPt = extents.center;
printf("Map coordinates at center: MapX = %.2f, MapY = %.2f\n", mapPt.x, mapPt.y);
objPt = rTransObjToMap.ConvertPoint2DInv(mapPt);
printf("Raster object coordinates at center: ObjX = %.1f, ObjY = %.1f\n\n", objPt.x, objPt.y);
# get coordinate transformation from vector object to map coordinates from the georeference
# if vector has implied georeferencing, transformation will be identity
class TRANS2D_MAPGEN vTransObjToMap;
vGeoref.GetTransParm(vTransObjToMap, 0, vGeoref.GetCalibModel() );
# get the line from the vector object as a polyline
class POLYLINE line;
line = GetVectorLine(Vect, 1);
objPt = line.GetVertex(0);
mapPt = vTransObjToMap.ConvertPoint2DFwd(objPt);
print("Coordinates of vector line start and end nodes:");
printf("Start object coordinates: ObjX = %.1f, ObjY = %.1f\n", objPt.x, objPt.y);
printf("Start map coordinates: MapX = %.1f, MapY = %.1f\n", mapPt.x, mapPt.y);
objPt = line.GetVertex( line.GetNumPoints() - 1 );
mapPt = vTransObjToMap.ConvertPoint2DFwd(objPt);
printf("End object coordinates: ObjX = %.1f, ObjY = %.1f\n", objPt.x, objPt.y);
printf("End map coordinates: MapX = %.1f, MapY = %.1f\n", mapPt.x, mapPt.y);