SHAPE.sml

  Download

More scripts: Convert

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# SHAPE.sml
# sample script for tutorial Writing Scripts with SML
# Reguires TNT version 2009:75 dated 11 November 2009 or later
clear();
class RVC_SHAPE ShapeIn;
class RVC_OBJITEM objItemIn;  # ObjItem for the shape object returned by DlgGetObjects();
class STRING prompt$;
# get the RVC_OBJITEM of the input shape object containing quadrangle boundaries
prompt$ = "Select input Quads.shp from the SML directory";
DlgGetObject(prompt$, "Shape", objItemIn, "ExistingOnly");
# use the objitem to open the shape object for reading as our declared shape class instance
ShapeIn.Open(objItemIn, "Read");
# get georeference from the input shape object
class RVC_GEOREFERENCE shapeGeoref;
ShapeIn.GetDefaultGeoref(shapeGeoref);
class SR_COORDREFSYS crs;
crs = shapeGeoref.GetCoordRefSys();
printf("Shape Coordinate Reference System = %s\n", crs);
# get the output region object and set its Coordinate Reference System
class REGION RegionOut;
GetOutputRegion(RegionOut);
RegionOut.CoordRefSys = shapeGeoref.GetCoordRefSys();
printf("Output region Coordinate Reference System = %s\n", RegionOut.CoordRefSys );
# open input shape database and main table (table number = 0) for reading
# (needed to find out the element type and number of elements)
class RVC_DBASE_SHAPE dbIn;
dbIn.OpenAsSubobject(ShapeIn, "Read");
class RVC_DBTABLE table;
table.Open(dbIn, 0, "Read");
numeric numrecords;
numrecords = table.GetNumRecords();
printf("Number of records and elements = %d\n", numrecords);
numeric i;
class REGION2D Region1, Region2;
class POINT2D centroid;
Region2.CoordRefSys = crs;
printf("Region2 CRS = %s\n", Region2.CoordRefSys);
# The main table for a shape object has "Implied one-to-one" record attachment.
# Another term for this is "One record per element, record number equals element number."
# Thus a record number can be substituted for the corresponding element number
# in the processing loop below. 
# get the region for each quadrangle polygon and add it to an "accumulator" region
for i = 1 to numrecords	 
	{
	Region1 = ShapeIn.ReadPolygons(i);
	Region1.CoordRefSys = crs;		# set CRS for Region 1 after it is read from the Shape
	Region2.UnionRegion(Region1);	# add region for current polygon to the "accumulator" region
	printf("Area of output region after polygon %d = %.1f\n", i, Region2.GetArea() );
	Region1.Clear();		# delete the data and CRS for Region1
	}
# write the "accumulator" region to the output region object
RegionOut.Write(Region2);
print("Done");