DB_READ2.sml

  Download

More scripts: Database

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# DB_READ2.sml
# sample script for tutorial "Writing Scripts with SML".
# Demonstrates use of RVC_DATABASE classes for reading
# attribute values from records attached to vector elements.
# version 21 July 2014
clear();
PopupMessage("Select sf_data/HAYWSOIL/hsoils");
class RVC_VECTOR V;
numeric i, acres;
class STRING type$;
numeric err;
GetInputVector(V);
class RVC_DBASE_POLYGON dbSoils;
err = dbSoils.OpenAsSubobject(V, "Read");
if (err<0) then {
	print("Could not open specified database.");   Exit();
	}
class RVC_DBTABLE type, wild;		# instances for SoilType and Wildlife tables
# open SoilType table using table number from object information
numeric tablenum = 1;		# SoilType table = table number 1
err = type.Open(dbSoils, tablenum, "Read");
if (err<0) then {
	print("Could not open table SoilType.");   Exit();
	}
# open Wildlife table using RVC_DESCRIPTOR set with table name
class RVC_DESCRIPTOR descript;
descript.SetName("Wildlife");
err = wild.Open(dbSoils, descript, "Read");
if (err<0) then {
	print("Could not open table Wildlife.");   Exit();
	}
class RVC_ELEMENT elem;		# class for an element
elem.Type = "Polygon";
# arrays of record numbers of records attached to the current element
class RVC_RECORDNUM recNumType[1];	# for SoilType table
class RVC_RECORDNUM recNumWild[1];	# for Wildlife
# class for a table record
class RVC_DBTABLE_RECORD recType, recWild;
for i = 1 to V.$Info.NumPolys
	{ 
	elem.Number = i;	# set the element number
	# get record number of the first attached record in the two tables
	dbSoils.GetAttachedRecordNumbers(elem, type, recNumType, "FirstMatch"); 
	dbSoils.GetAttachedRecordNumbers(elem, wild, recNumWild, "FirstMatch");
	# use the record number to read the record to RVC_DBTABLE_RECORD 
	type.Read(recNumType[1], recType);
	wild.Read(recNumWild[1], recWild);
	acres = recType.GetValue("Acres");
	recWild.GetValueString("SoilName", type$);
	printf("Polygon # %d: Soil type = %s, acres = %d\n", i, type$, acres);
	}
print("Done.");