Example: VBScript to Create Locations from a Table

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.  

 

tech_ravi_sm.png

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, for example:

 

 

eg_vbs_locations_from_table01_01.png

 

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.

 

eg_vbs_locations_from_table01_02.png

 

Opening the table and undocking 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.

 

eg_vbs_locations_from_table01_03.png

 

We choose File - Create - New Script.

 

eg_vbs_locations_from_table01_04.png

 

In the New Script dialog we choose VBScript as the Language and we provide the desired Name for the script.

 

 

eg_vbs_locations_from_table01_05.png

 

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 undock the script and then replace the stub text with the following script:

 

eg_vbs_locations_from_table01_06.png

 

The text of the script is:

 

'VBScript

SubMain

        Setapp=Manifold.Application

        Setdb=app.GetDatabaseRoot()'openedMAPfile

        Settable=db.Search("locations")

        'getlistofrecords

        Setrecords=table.SearchAll(Array("Name","Scale","Longitude","Latitude"))

        If(recordsIsNothing)Then

                ExitSub'norecords

        EndIf

        DoWhilerecords.Fetch

                'readrecordvalues

                Setvalues=records.GetValues()

                name=values(0).Data

                scale=CDbl(values(1).Data)

                lon=CDbl(values(2).Data)

                lat=CDbl(values(3).Data)

                'composelocationdefinition

                Setprops=app.CreatePropertySet()

                props.SetProperty"Center",app.CreatePointObj(lon,lat)

                props.SetProperty"Scale",scale

                def=props.ToJson()

                'createlocation

                created=db.Insert(name,"location")

                db.SetPropertycreated,"Text",def'setlocationdefinition

                db.SetPropertycreated,"Folder","Locations"'putlocationintofolder

                app.Log"Createdlocation:"&created

        Loop

        app.OpenLog

EndSub

 

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.

 

eg_vbs_locations_from_table01_07.png

 

Run the script by pressing the ! run button.  

 

eg_vbs_locations_from_table01_08.png

 

The log reports the creation of each location.   A new folder appears in the Project.

 

 

eg_vbs_locations_from_table01_09.png

 

Opening the folder, we see it is full of the created locations.

 

 

eg_vbs_locations_from_table01_10.png

 

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.   We will right-click onto the Grand Prismatic location and choose View in Active Window.

 

eg_vbs_locations_from_table01_11.png

 

The image window immediately pans and zooms into a fine view of the Grand Prismatic Spring in Yellowstone National Park, in the US.

 

Notes

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.

 

Videos

yt_mass_produce.png

 

See the video version of this topic in the Manifold 9 - Mass Produce Locations with a Script video.

See Also

Getting Started

 

User Interface

 

Tables

 

Maps

 

Locations

 

Editing Queries, Scripts and Comments

 

Scripts

 

Command Window

 

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 panels.

 

Example: Run JavaScript in the Command Window - How to run a simple V8 JavaScript script in the Command window.