In this example, we use VBScript to take a table where each record has a name, scale, latitude and longitude and for each record create a Location component in the project. The script is useful if we want to take a geocoded table that contains a list of many locations and from that table create Locations components in our project.
Must read info: All programmers should review the API Documentation online. The API Documentation provides total details on the API and also provides hundreds of examples, many of which are provided side-by-side in three versions, in C#, in VBScript and in IronPython.
To fit into this documentation, illustrations show an artificially small Manifold desktop, with only a few panes, docked to the right side. In real life we use a much larger Manifold desktop, and all panes would be turned on, with some panes docked to the left and others docked to the right.
The table we start with is the table created in the Example: Create a Table from Locations topic. For shorter names, we have renamed it from locations table as it was in that topic to simply locations.
Opening the table and Shift-clicking the title tab to undock it, we see that each record has a Name field that gives the name of the location, the Scale to use and Longitude and Latitude values that mark the center of each location. The Scale, Longitude, and Latitude fields are float64 numeric data types.
We choose File - Create - New Script.
In the New Script dialog we choose VBScript as the Language and we provide the desired Name for the script.
A new script component appears in the Project pane. We double-click it open to see it contains the standard, default stub script for VBScript as illustrated in the Scripts topic.
We Shift-click the Create Locations title tab to undock the script. We replace the stub text with the following script:
The text of the script is:
' VBScript
Sub Main
Set app = Manifold.Application
Set db = app.GetDatabaseRoot() ' opened MAP file
Set table = db.Search("locations")
' get list of records
Set records = table.SearchAll(Array("Name", "Scale", "Longitude", "Latitude"))
If (records Is Nothing) Then
Exit Sub ' no records
End If
Do While records.Fetch
' read record values
Set values = records.GetValues()
name = values(0).Data
scale = CDbl(values(1).Data)
lon = CDbl(values(2).Data)
lat = CDbl(values(3).Data)
' compose location definition
Set props = app.CreatePropertySet()
props.SetProperty "Center", app.CreatePointObj(lon, lat)
props.SetProperty "Scale", scale
def = props.ToJson()
' create location
created = db.Insert(name, "location")
db.SetProperty created, "Text", def ' set location definition
db.SetProperty created, "Folder", "Locations" ' put location into folder
app.Log "Created location: " & created
Loop
app.OpenLog
End Sub
The script finds all records in a table named location and then loops through each record getting the values for name, scale, longitude and latitude to create each location, putting all the locations into a folder called Locations. Fields must be named Name, Scale, Longitude, and Latitude, but the data types of the scale, longitude and latitude fields could be text or numeric. The script logs the creation of a Location component from each record as it loops through the records.
Run the script by pressing the ! run button.
The log reports the creation of each location. A new folder appears in the Project.
Opening the folder, we see it is full of the created locations.
We can verify the new locations have been created correctly by using them. In the illustration above we have created a Google satellite image server data source and have opened the image server image.
To see Grand Prismatic Spring, we right-click onto the Grand Prismatic location in the Project pane and choose View in Active Window.
Another way to zoom to the location is to
click the Locations pull
down button and to choose the Grand Prismatic Spring location. The
Locations button tends to be quicker with fewer locations, while using
the Project pane is quicker when there are very many locations, organized
within folders in the Project pane.
The image window immediately pans and zooms into a fine view of the Grand Prismatic Spring in Yellowstone National Park, in the US.
Do not open log at end - The script as written opens the Log Window at the end to show the log. To prevent the log window from being opened, either remove app.OpenLog just after the Loop, or comment it out by putting a single quote ' character at the beginning of the line:
' app.OpenLog
Uses - Locations are a great way to immediately jump to predefined views. Suppose we have a list of real estate sites that we have plotted on a map. We would like a quick way for users to jump to a predefined view of each site by using a location. The script in this example makes that easy to do. In this example each record has a different scale, but we could easily fill the Scale field for each record with some standard value, such as a scale of 3000.
Adding a Description Property - Components in the Project pane that have a text Description property will automatically show that text in a tooltip when the mouse hovers over that component. If our locations source table also has a Description text field we can harvest that field's contents and put them into a Description property for each Location that is created by making a slightly adjustment in our script:
' VBScript
Sub Main
Set app = Manifold.Application
Set db = app.GetDatabaseRoot() ' opened MAP file
Set table = db.Search("locations")
' get list of records
Set records = table.SearchAll(Array("Name", "Scale", "Longitude", "Latitude", "Description"))
If (records Is Nothing) Then
Exit Sub ' no records
End If
Do While records.Fetch
' read record values
Set values = records.GetValues()
name = values(0).Data
scale = CDbl(values(1).Data)
lon = CDbl(values(2).Data)
lat = CDbl(values(3).Data)
descr = values(4).Data
' compose location definition
Set props = app.CreatePropertySet()
props.SetProperty "Center", app.CreatePointObj(lon, lat)
props.SetProperty "Scale", scale
def = props.ToJson()
' create location
created = db.Insert(name, "location")
db.SetProperty created, "Text", def ' set location definition
db.SetProperty created, "Folder", "Locations" ' put location into folder
db.SetProperty created, "Description", descr ' set description
app.Log "Created location: " & created
Loop
' app.OpenLog
End Sub
The above script is used to add a Description property to the created location in the Example: Create Many Locations from a Drawing topic.
See the video version of this topic in the Manifold 9 - Mass Produce Locations with a Script video.
Editing Queries, Scripts and Comments
Example: Locations - Save Locations and use saved Locations to quickly navigate to desired views in windows.
Example: Create Many Locations from a Drawing - Given a drawing of points, we quickly create a folder with Locations for all of the points, allowing us to quickly pan and zoom to a local view around each point.
Example: Create a Table from Locations - Create a table that contains, as records, all of the Locations components in a project. Each record contains the Name, Latitude, Longitude, and Scale of a location. We use simple, point-and-click operations using the Select and Transform panes.
Example: Create and Run a JScript.NET Script - How to create and run simple JScript.NET scripts.