More scripts: Vector
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# DB2.SML
# Sample script for tutorial "Writing Scripts with SML".
# Illustrates creating database table, fields,
# and attaching records.
# Script creates a new vector with implied georeference
# read from the input vector. A point database table called
# "SoilTypes" is created for the new vector with fields
# "SYMBOL" (string), "SOIL" (string), and "WHEAT_YLD"
# (integer). In the main processing loop the script reads
# centroid location for each polygon in the input vector
# and creates a point at that location. The value for the
# SYMBOL field is read from the Class polygon table, the
# value for the SOIL field is read from the DESCRIPTN polygon
# table, and the value for the WHEAT_YLD field is read from
# the YIELD polygon table. These values are read into a new
# record for the SoilTypes table and the record is
# attached to the current vector point.
### Declarations
class VECTOR VectIn, VectOut;
clear();
PopupMessage("Select / CB_DATA / CB_SOILS.RVC / CBSOILS_Lite" );
### Get input vector object with polygons.
GetInputVector(VectIn);
### Get georeference parameters for input vector.
class GEOREF georefV;
georefV = GetLastUsedGeorefObject( VectIn );
### Get information about "DESCRIPTN" polygon table;
### This is needed later to check if attached records exist.
class DATABASE polydb; # handle for polygon database.
polydb = OpenVectorPolyDatabase( VectIn );
class DBTABLEINFO descriptn; # handle for DESCRIPT table.
descriptn = TableGetInfo( VectIn.poly.DESCRIPTN );
### Get new output vector object initialized for use with
### with the Vector Toolkit, and create implied
### georeference using parameters from input vector.
GetOutputVector( VectOut, "VectorToolkit,Planar" );
CreateImpliedGeoref( VectOut, georefV.Projection );
# Create point database for VectOut.
class DATABASE pdb; # handle for point database
pdb = OpenVectorPointDatabase( VectOut );
# Create blank table "SoilTypes" in point database.
class DBTABLEINFO soiltype; # handle for table
soiltype = TableCreate(pdb,"SoilTypes","Table created by SML script");
soiltype.OneRecordPerElement = 1; # set attachment type for records
# Create string field "SYMBOL" in "SoilTypes".
class DBFIELDINFO symbol; # handle for field
symbol = TableAddFieldString(soiltype,"SYMBOL",6,6);
# Create string field "SOIL" in "SoilTypes".
class DBFIELDINFO soil; # handle for field
soil = TableAddFieldString(soiltype,"SOIL",80,80);
# Create integer field "WHEAT_YLD" in "SoilTypes".
class DBFIELDINFO wheat; # handle for field
wheat = TableAddFieldInteger(soiltype,"WHEAT_YLD",10);
array numeric polyrecords[1]; # array to hold polygon record number
array numeric ptrecords[1]; # array to hold point record number for attachment.
numeric numpolys; # number of polygons in input vector
numeric i; # counter for processing loop
string soil$; # value for soil field
numeric wheatnum; # value for wheat field
string symbol$; # value for symbol field
numeric x, y; # object coordinates of polygon centroid
numeric mapX, mapY; # map coordinates of polygon centroid
numeric pointnum; # counter for number of points added to outut vector
numeric recordnum; # record number created by TableNewRecord() function call
numpolys = NumVectorPolys(VectIn);
# Main processing loop
for i = 1 to numpolys {
soil$ = "";
wheatnum = 0;
x = VectIn.poly[i].POLYSTATS.CentX;
y = VectIn.poly[i].POLYSTATS.CentY;
ObjectToMap( VectIn, x, y, georefV, mapX, mapY );
VectorAddPoint( VectOut, mapX, mapY );
pointnum = pointnum + 1;
symbol$ = VectIn.poly[i].CLASS.Class$;
# polygons in WATER class don't have records in the
# DESCRIPTN and YIELD tables so check for attached
# record before trying to read values.
if (TableReadAttachment( descriptn, i, polyrecords) > 0 ) {
soil$ = VectIn.poly[i].DESCRIPTN.NAME$;
wheatnum = VectIn.poly[i].YIELD.WHEAT;
}
# Create new record in SoilType table with current field
# values.
recordnum = TableNewRecord( soiltype, symbol$, soil$, wheatnum );
ptrecords[1] = recordnum;
# Attach this new record to current point element.
TableWriteAttachment( soiltype, pointnum, ptrecords, 1, "point");
}
VectorValidate (VectOut);
print(TableReadFieldStr(soiltype, "SOIL", 371)); # Internal Automated Tests Check
CloseVector( VectOut );
# End