canvquakes.sml

  Download

More scripts: Advanced

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# canvquakes.sml
# Sample script demonstrating:
#		1) use of the HTTP_CLIENT class to download from the Internet a text file with spatial point locations and attributes 
#		2) import of point data from the text file, extraction by area, and set-up of styling and DataTips for the points
#		3) adding the vector points to an existing layout and rendering to a KML file that can be viewed in Google Earth
# This script can be run manually or run automatically on a schedule via TNTmips Job Processing. 
# In a scheduled application, the script can automatically create an updated KML file
# that can be viewed on demand in a web page using the Google Earth browser plugin, as in the demonstration at
# 		www.geoprovisioning.com/canvquakes/
# requires TNTmips Pro v2010
# also requires resource files as documented in script comments.
numeric err;
######################################################
#  Step 1: Download USGS daily global earthquake summary file
print("Downloading daily earthquake file.");
class STRING address$ = "earthquakes.usgs.gov";
class STRING url$ = "http://earthquakes.usgs.gov/eqcenter/catalogs/eqs1day-M1.txt";
class STRING textfile$ = _context.ScriptDir + "/Download/eqs1day-M1.txt";
class HTTP_CLIENT http;
clear();
# connect to the web server
http.SetTimeOut(15);
err = http.Connect(address$, 80);
printf("Connect returned %d.\n", err);
# download the text file
http.DownloadFile(url$, textfile$);
#print("File downloaded.");
printf("filepath to downloaded file = %s\n\n", textfile$);
##########################################################################
#  Step 2: Import the comma-delimited text file of earthquake epicenters to points in a vector object
print("Importing the earthquake epicenter file.");
class RVC_OBJECT tempfile;
tempfile.MakeTempFile(1);
# set up new vector object in tempfile
class RVC_OBJITEM fqObjItem;		# object item for vector with global daily earthquake epicenters
class RVC_DESCRIPTOR fqDescript;
fqDescript.SetName("GlobalQuakes");
fqDescript.SetDescription("");
fqObjItem.CreateNew(tempfile.GetObjItem(), "VECTOR", fqDescript);
# set up class for import/export of vector from/to text; the class method to import the object takes an
# RVC_OBJITEM rather than an RVC_VECTOR class instance.
class MieTEXTVECTOR mieTV;
# set existing format file that records all import settings including additional database fields and the
# coordinate reference system for the earthquake data
class STRING formatFilename$ = _context.ScriptDir + "/resources/eqFormat.fmt";
printf("format file file path: %s\n", formatFilename$);
mieTV.FormatFilename = formatFilename$;
err = mieTV.ImportObject(textfile$, fqObjItem);
printf("mie ImportObject returned %d\n\n", err);
###########################################################
#  Step 3: Copy epicenter points within the specified latitude/longitude range
#               to a new temporary vector object
print("Getting the epicenters for California and Nevada.");
# create new temporary vector object for the California-Nevada epicenter points
class RVC_VECTOR CaNv_Quakes;
CreateTempVector(CaNv_Quakes, "Planar");
class STRING ptQry;		# query string to select epicenter points within desired latitude-longitude range
ptQry =  "if (CLASS.Lat > 32.5 && CLASS.Lat < 42.0 && CLASS.Lon > -124.75 && CLASS.Lon < -114.0) return true;";
# RVC_VECTOR class instance for the global earthquake vector object, needed for the VectorCopy Elements function
class RVC_VECTOR GlobalQuakes;
# open the global earthquake vector object
GlobalQuakes.OpenByName(fqObjItem.GetFilePath(), fqObjItem.GetObjectPath(), "Read");
# use query to copy epicenters from global earthquake vector to temporary California-Nevada epicenter vector object 
err = VectorCopyElements(GlobalQuakes, CaNv_Quakes, "RemExRecords", ptQry);
printf("VectorCopyElements returned %d\n\n", err);
GlobalQuakes.Close();		# close the global earthquake vector object
#############################################################
#  Step 4: Set up a multiline DataTip field in the CLASS table in the CaNv_Quakes vector
print("Setting up multiline DataTip field for the CaNv epicenter points.");
class RVC_DBASE_POINT ptDB;
ptDB.OpenAsSubobject(CaNv_Quakes, "Write");
class RVC_DBTABLE classTbl;
classTbl.Open(ptDB, 0, "Write");
class RVC_DBFIELDINFO dtField;
dtField.SetName("Datatip");
dtField.SetType("String");
dtField.SetFlag("ComputeOnRead");
dtField.SetDispWidth(256);
# Add the Datatip field to the table
classTbl.InsertFieldInfo(10, dtField);
# Set the string expression for the Datatip field to create a multiline Datatip; 
# read expression from saved file to avoid syntax check problems with Table.Field references in the expression
class STRING dtQry;
dtQry = TextFileReadFull(sprintf("%s/resources/CompFldExp.txt", _context.ScriptDir) );
classTbl.SetComputedFieldQuery(10, dtQry);
ptDB.Close();		# close the vector point database
####################################################################
#  Step 5: Read page layout with major fault lines and add the earthquake epicenter vector to it.
print("Reading page layout with fault lines and add earthquake layer.");
class STRING layoutfile$ = _context.ScriptDir + "/resources/QuakeResources.rvc";		# filename for the layout
class GRE_LAYOUT layout;
layout.Read(layoutfile$, "FaultLayout");
class GRE_GROUP group;
group = layout.FirstGroup;			# get handle for the single group in the layout
# add the earthquake epicenter vector to the group in the layout
class GRE_LAYER_VECTOR quakeLayer;
quakeLayer = GroupQuickAddVectorVar(group, CaNv_Quakes);
#####################################################################
#  Step 6: Set the multiline Datatip field created earlier as the source for the point DataTip;
#          Render to KML automatically turns the DataTip into Google Earth balloon info
print("Setting DataTip for earthquake layer.");
# set the DataTip for the earthquake epicenter layer to Datatip field we created in its table
quakeLayer.Point.DataTip.TableName = "CLASS";
quakeLayer.Point.DataTip.FieldName = "Datatip";
quakeLayer.Point.DataTip.Shown = 1;
quakeLayer.Point.DataTip.Prefix = "";
#####################################################################
#  Step 7: Set the earthquake epicenter point styles by script using a previously saved style
#        script file that uses point styles  already created and saved in a style object in a reference 
#        Project File.  These styles use the predefined filled circle symbol with different colors assigned 
#        by earthquake depth; Render to KML automatically converts these predefined circle symbols into
#        a shaded sphere symbol in Google Earth.
print("Setting styles for epicenter points.");
# set previously-created style object with point styles to use
class STRING styleFile$ = _context.ScriptDir + "/resources/QuakeResources.rvc";
class FILEPATH styleFilepath(styleFile$);
class RVC_STYLE ptStyles;
ptStyles.OpenByName(styleFilepath, "PtStyles.STYLE", "Read");
quakeLayer.StyleObject = ptStyles;
# set up earthquake point styling by script;  
# read style script from saved file to avoid syntax check problems with Table.Field references in the query
class STRING ptStyleQry;
ptStyleQry = TextFileReadFull( sprintf("%s/resources/QuakeStyle.qry", _context.ScriptDir) );
quakeLayer.Point.StyleMode = "ByScript";
quakeLayer.Point.Script = ptStyleQry; 
########################################################################
#  Step 8: Render the layout to a KML file.
print("Rendering updated layout to KML.");
# get current datetime and convert to UTC
class DATETIME dt;
dt.SetCurrent();
dt.ConvertToUTC();
class STRING datetime$;		# string with datetime for web page
datetime$ = dt.GetString();
datetime$ += " UTC";
printf("update datetime: \n", datetime$);
# open text file and write datetime string (overwrite)
class STRING dtFilename$ =  _context.ScriptDir + "/datetime.txt";
class FILE dtFile = fopen(dtFilename$, "w");
fwritestring(dtFile, datetime$);
fclose(dtFile);
# KML file information
class STRING kmlName$ = _context.ScriptDir + "/eqs1day_canv.kml";
class FILEPATH kmlPath(kmlName$);
# render the modified layout to KML
class KML kml;
kml.SetPath(kmlPath);
kml.SetLayout(layout);
kml.SetResolution(450);  # target resolution 450 m
kml.Write();
# close temporary objects
CaNv_Quakes.Close();
tempfile.Close();
print("Done.");