network.sml

  Download

More scripts: Advanced

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
clear();  #clear console
vector Farms, Plants, TempNetwork;
GetInputVector(Farms); #this vector is modified since tables are written to it
GetInputVector(Plants); 
GetInputVector(TempNetwork); #this vector is modified by adding nodes
class Georef farmgeo, plantgeo, tempgeo;
VectorToolkitInit(TempNetwork,"NoDBStatTable");
VectorToolkitInit(Farms);
numeric numPoints;
numeric numFarms;
numeric numPlants;
numeric numLines;
numeric i;
array numeric xarray[1];
array numeric yarray[1];
numFarms = NumVectorPoints(Farms);
array numeric farms[numFarms];
numeric linenumber;
numeric tempx;
numeric tempy;
numeric a;
numeric b;
numeric distance;
farmgeo = GetLastUsedGeorefObject(Farms);
tempgeo = GetLastUsedGeorefObject(TempNetwork);
printf("The number of farms is %d\n",numFarms);
for i=1 to numFarms {
	SetStatusMessage(sprintf("Processing point %d of %d of farms",i,numFarms));
	tempx = Farms.point[i].Internal.x;
	tempy = Farms.point[i].Internal.y;
	GeorefTrans(farmgeo,tempx,tempy,tempgeo,tempx,tempy);
	linenumber = FindClosestLine(TempNetwork,tempx,tempy);
	ClosestPointOnLine(TempNetwork,linenumber,tempx,tempy,a,b);
	VectorAddNode(TempNetwork,a,b,1);
	farms[i] = FindClosestNode(TempNetwork,a,b);
	}
numPlants = NumVectorPoints(Plants);
array numeric plants[numPlants];
plantgeo = GetLastUsedGeorefObject(Plants);
printf("The number of plants is %d\n",numPlants);
for i=1 to numPlants {
	SetStatusMessage(sprintf("Processing point %d of %d of plants",i,numPlants));
	tempx = Plants.point[i].Internal.x;
	tempy = Plants.point[i].Internal.y;
	GeorefTrans(plantgeo,tempx,tempy,tempgeo,tempx,tempy);
	linenumber = FindClosestLine(TempNetwork,tempx,tempy);
	ClosestPointOnLine(TempNetwork,linenumber,tempx,tempy,a,b);
	VectorAddNode(TempNetwork,a,b,1);
	plants[i] = FindClosestNode(TempNetwork,a,b);
	}
VectorUpdateStdAttributes(TempNetwork);
CloseVector(TempNetwork);  #flush vector
class Network net;
class Route route;
class MultiRoute multiroute;
numeric imp;
net = NetworkInit(GetObjectFileName(TempNetwork),GetObjectName(GetObjectFileName(TempNetwork),GetObjectNumber(TempNetwork)));
NetworkSetDefaultAttributes(net);
numLines = NumVectorLines(TempNetwork);
for i=1 to numLines {
	imp = (TempNetwork.line[i].LINESTATS.Length);
	NetworkLineSetImpedance(net,i,imp,"FromTo");
	NetworkLineSetImpedance(net,i,imp,"ToFrom");
	}
numeric total = numFarms * numPlants;
numeric count;
count = 1;
string tablename$;
class DATABASE db;
class DBTABLEINFO tinfo;
db = OpenVectorPointDatabase(Farms);
numeric recordnumber;
array numeric records[1];
numeric distance;
tinfo = TableCreate(db,"Plant_Num","Created by SML script");
TableAddFieldInteger(tinfo,"Plant",3);
TableAddFieldFloat(tinfo,"xcoord",25,6);
TableAddFieldFloat(tinfo,"ycoord",25,6);
numeric j;
for j=1 to numPlants {
	tempx = Plants.point[j].Internal.x;
	tempy = Plants.point[j].Internal.y;
	GeorefTrans(plantgeo,tempx,tempy,tempgeo,tempx,tempy);
	recordnumber = TableNewRecord(tinfo,j,tempx,tempy);
	records[1] = recordnumber;
	TableWriteAttachment(tinfo,i,records,1);
	}
tablename$ = "Plant_Dist";
tinfo = TableCreate(db,tablename$,"Created by SML Script");
TableAddFieldInteger(tinfo,"Plant",3);
class DBFIELDINFO the_field;
the_field = TableAddFieldFloat(tinfo,"Net_Dist",25,6);
the_field.UnitType = "Distance";
the_field.Units = "kilometers";
string report$;
for j=1 to numPlants {
	printf("Plant %d\n",j);
	SetStatusMessage(sprintf("Calculating all routes from plant %d of %d",j,numPlants));
	NetworkCalculateMultiRoute(net,plants[j],farms,numFarms,multiroute);
	for i=1 to numFarms {
		SetStatusMessage(sprintf("Calculating route %d of %d",count,total));
		count +=1;
		printf("Route from plant %d to farm %d\n",j,i);
		NetworkMultiRouteGetRoute(multiroute,farms[i],route);
		report$ = NetworkRouteGetReport(route);
		distance = StrToNum(GetToken(report$, " ",18));
		recordnumber = TableNewRecord(tinfo,j,distance/1000);  #m/1000 = km
		records[1] = recordnumber;
		TableWriteAttachment(tinfo,i,records,1);
		NetworkRouteClose(route);
		}
	NetworkMultiRouteClose(multiroute);
	}
NetworkClose(net);
CloseVector(Farms);
CloseVector(TempNetwork);
CloseVector(Plants);
printf("Script Ran to Completion");