MapInfo Workspaces:
Customization and Automation
Philip Woods
Port Blakely Tree Farms L.P.
Seattle, WA
July 2003
Introduction
MapInfo workspaces are a simple text based scripting resource that can be used to increase work efficiencies within MapInfo. The aim of this paper is to discuss some of the tasks that can be accomplished through the use of workspaces.
What is a workspace (John Schlosser, May 2002)
Typical workspace structure
A typical workspace will be comprised of some or all of the following sections. However, the only mandatory section is the header information.
o Query information saved from session
The sections (with the exception of header and table sections) are repeated for each query, map, thematic, layout, etc. When programming with workspaces only the header section is mandatory.
-- Sample workspace WA-Simple --
Header section
!Workspace
!Version 600
!Charset WindowsLatin1
Table section
Open Table "USA\USA_MAPS\USZIPBDY" As USZIPBDY Interactive
Open Table "USA\USA_MAPS\LANDMRKS" As LANDMRKS Interactive
Open Table "USA\USA_MAPS\US_CNTY" As US_CNTY Interactive
Cut here .Cut here
Map settings
Map From USCTY_8K,USCTY153,USA_CAPS,LANDMRKS,US_HIWAY,USZIPBDY,USA,US_CNTY
Position (0.0520833,0.0520833) Units "in"
Width 6.14583 Units "in" Height 4.47917 Units "in"
Set Window FrontWindow() ScrollBars On Autoscroll On
Set Map
CoordSys Earth Projection 1, 33
Center (-120.7334628,47.40479464)
Zoom 434.4235605 Units "mi"
Preserve Zoom Display Zoom
Distance Units "mi" Area Units "sq mi" XY Units "degree"
Set Map
Map layer settings
Layer 1
Display Off
Global Symbol (35,0,12)
Label Line Arrow Position Right Font ("Arial",0,9,0) Pen (1,2,0)
With City
Parallel On Auto Off Overlap Off Duplicates On Offset 2
Visibility On
Layer 2
Display Off
Global Symbol (35,0,12)
Label Line Arrow Position Right Font ("Arial",0,9,0) Pen (1,2,0)
With City
Parallel On Auto Off Overlap Off Duplicates On Offset 2
Visibility On
Layer 3
Display Off
Global Symbol (35,0,12)
Label Line Arrow Position Right Font ("Arial",0,9,0) Pen (1,2,0)
With Capital
Parallel On Auto Off Overlap Off Duplicates On Offset 2
Visibility On
Cut here . Cut here
Layer 7
Display Global
Global Pen (2,2,0) Brush (1,16777215,16777215) Symbol (35,0,12)
Label Line None Position Center Font ("Arial",257,12,0,16777215) Pen (1,2,0)
With State_Name
Parallel On Auto On Overlap On Duplicates On Offset 2
Visibility On
Activate Using URL On Objects Relative Path Off
Layer 8
Display Global
Global Brush (2,16772272,16777215) Symbol (35,0,12)
Label Line None Position Center Font ("Arial",257,8,11579568,16777215) Pen (1,2,0)
With County
Parallel On Auto On Overlap On Duplicates On Offset 2
Visibility On
Layout settings
Layout
Position (4.69792,3.19792) Units "in"
Width 5.29167 Units "in" Height 3.32292 Units "in"
Set CoordSys Layout Units "in"
Create Frame (3.3132,1.6889) (39.5153,27.7993)
Pen (2,63,0)
Brush (2,9502608,16777215)
Title "USCTY_8K,USCTY153,...,US_CNTY Map"
FillFrame On
Create Text
"Washington State, USE"
(16.7688,29.4451) (24.1389,30.2285)
Font ("Arial",1,48,0)
Create Text
"Date: 07/03/2003"
(17.4257,30.5535) (22.5389,31.3201)
Font ("Arial",0,48,0)
Create Text
"Mapsheet# 15"
(17.8021,31.5764) (22.2118,32.3431)
Font ("Arial",0,48,0)
Set Layout Ruler On Pagebreaks On Frame Contents Active
Zoom 8.54867 Center (28.4564,16.9983) Extents To Fit
Set Window FrontWindow() Autoscroll On
Set CoordSys Earth
-- End sample workspace WA Simple ---
Creating workspaces
Workspaces can be created automatically via MI or manually through a text editor.
Programming experience
Programming experience is not needed to work with workspaces. The most important aspects are an understanding of how workspaces work and some knowledge of MapBasic command statements.
When are workspaces used?
o At program startup
o To set session defaults
o To open map templates, layout templates, etc.
o A workspace runs another workspace
o A compiled MapBasic program calls a workspace
What are workspaces used for?
o Setting system parameters
o User notification
o Pre-set templates for maps, layouts, themes
o Complex standard map compilation
o Date processing
Workspace functionality
Workspaces are simple text based scripts that employ MapBasic commands. Any MapBasic command that can be entered into the MapBasic window can be used in a workspace. The MapBasic window is a good place to pick up useful command strings. Text can be copied from this window into a workspace file or saved directly to a text file via the MapBasic menu item, available when the MapBasic window is visible.
.
Extending the life of workspaces
Workspaces contain pointers to the data, not the data itself. This means that a workspace that accesses corporate data can be shared by many people. When the data is updated, the workspace will reflect the latest updates next time it is opened.
File paths
Problems can occur if data is moved, or drives are not mapped in consistent fashion. To ensure a higher level of application stability the file paths may be set a users drive mapping does not create problems.
Instead of using drive letters:
F:\CompanyMaps\MonthlyUpdates\OfficeLocations.tab
Use either the full server name
\\Seattle\Admin\ CompanyMaps\MonthlyUpdates2003\OfficeLocations.tab
Or a relative path if the data is in the same folder hierarchy
..\MonthlyUpdates2003\OfficeLocations
Automatically update text elements on the layout.
With the use of MapBasic commands some map text fields can be updated automatically to reflect current information.
To update the current date automatically change the layout text string to use MapBasic commands.
Change:
Create Text
"Date: 07/03/2003"
(17.4257,30.5535) (22.5389,31.3201)
Font ("Arial",0,48,0
To:
Create Text
Str$(CurDate())
(17.4257,30.5535) (22.5389,31.3201)
Font ("Arial",0,48,0
Naming windows
Normally the name of a window is based on either the content of the window; or the name of the window that it is a view of. A way to make workspaces easier to manually manage is to change the window name to something more meaningful that is not dependent on the layers currently loaded. To accomplish this use the Title command to the Set Window line of the workspace
Set Window FrontWindow() Title "MapWindow" ScrollBars On Autoscroll On
Now change the layout title to match the ma window title
Layout
Position (4.69792,3.19792) Units "in"
Width 5.29167 Units "in" Height 3.32292 Units "in"
Set CoordSys Layout Units "in"
Create Frame (3.3132,1.6889) (39.5153,27.7993)
Pen (2,63,0)
Brush (2,9502608,16777215)
Title "MapWindow"
FillFrame On
The above examples are included in the WA Simple.wor file.
Using variables to increase workspace utility
What is a variable?
A reserved memory location that is accessed by a program using a programmer determined name. A variable is used to stored information for later use by the program. All variables declared in a workspace are global in scope, which means that they are available anytime, anywhere within the current MI session. The use of variables gives more control over display parameters and allows a more sophisticated level scripting.Variables must be declared before they can be used, and must be assigned a variable type such as string, integer, float, etc.
Dim TodaysDate As String
TodaysDate = Str$(CurDate())
UnDim TodaysDate
-- Sample workspace WA-Simple3
!Workspace
!Version 600
!Charset WindowsLatin1
Declare variables and assign data type
Dim TodaysDate As String
Dim UserName As String
Assign values to the variables
TodaysDate = Str$(CurDate())
UserName = "MapMaker"
Open Table "USA\USA_MAPS\USZIPBDY" As USZIPBDY Interactive
Cut here .cut here
Create Text
Insert the variable in place of text
"Date: " + TodaysDate
(17.4257,30.5535) (22.5389,31.3201)
Font ("Arial",0,48,0)
Cut here .cut here
Create Text
"Map Compiler: " + UserName
(3.2736,29.2715) (12.6438,30.0382)
Font ("Arial",0,48,0)
Cut here .cut here
Undeclare the variables to clean up and free up memory
UnDim TodaysDate
UnDim UserName
-- End sample workspace WA Simple3 ---
Using map templates with variables
By using multiple workspace files it is possible to reduce the overhead of large map projects. A simple scenario as discussed here, is to use three separate workspaces. One workspace calls the next when it is finished processing. The call to the next workspace must be the last line in the file and uses the Run Application command.
Run Application "WA-Simple4Map.wor"
This methodology is quite useful as multiple definition files can call the same map and layout files. The methodology can be used effectively to produce corporately standard maps of different areas. If changes are necessary fewer files will need to be edited to accomplish the task.
Definition workspace
The definition workspace sets up the necessary variables and values such as;
Dim TodaysDate As String
TodaysDate = Str$(CurDate())
Map Workspace
The map workspace contains information on the map layers and how they are displayed such as;
Set Map
Layer 1
Display Off
Global Symbol (35,0,12)
Label Line Arrow Position Right Font ("Arial",0,9,0) Pen (1,2,0)
With City
Parallel On Auto Off Overlap Off Duplicates On Offset 2
Visibility On
Layout workspace
The layout contains only information necessary for layout production and the statements that undeclare the variables such as;
Create Text
"Map Compiler: " + UserName
UnDim UserName
Example workspaces for the above scenario are:
WA-Simple4Def.wor
WA-Simple4Map.wor
WA-Simple4Layout.wor
WA-Simple5Def.wor
Task Automation with workspaces
Workspaces can be used to set system settings, supply information to a user; and do some data processing functions.
System Settings
The following sample changes the distance and area units of measure for the current map window
Define an integer variable
Dim nWinNum As Integer
Set the name of the current map window, must be the active window
Set Window FrontWindow() Title "MapWindow"
Get the MI identifier for the active map window
nwinNum = FrontWindow()
Set the are and distance units for the map window
Set Map Window nwinNum Area Units "acre" Distance Units "survey ft"
Undefine the variable to release system memory and keep things tidy
UnDim nwinNum
Scripts such as the one above can be written by hand, compiled from existing scripts (workspaces) and by copying statements from the MapBasic window.
Example code from SetUnits.wor
Task Automation
Common tasks can be automated so that all the user needs to do is run a workspace.
The following workspace script performs a selection from the US_CNTY table, adds the selection result to the map and themes the map by County attribute.
!Workspace
!Version 400
!Charset WindowsLatin1
Declare and assign value
Dim nWinNum As Integer
nwinNum = FrontWindow()
Select date from base table
select * from US_CNTY where State = "WA" into WACounties
Add selection result to the current map window
set map redraw off
Add Map Layer WACounties Set Map Order 2,3,4,5,6,1,7,8,9
Set Map Layer 6 Display Graphic Editable Off Selectable On Global Line (1,2,0) Global Pen (1,2,0) Global Brush (2,16777215,16777215) Global Symbol (35,0,12) Global Font ("Arial",0,9,0) Zoom (0, 100000) Units "mi" Off Label Line None Position Center Font ("Arial",0,9,0) Pen (1,2,0) With County Parallel On Auto Off Overlap Off PartialSegments Off Duplicates On Offset 2 Max Visibility On Nodes Off Arrows Off Centroids Off
Theme the selection table with the County attribute. Note the use of the nwinNum variable to identify the correct window
shade window nwinNum 6 with County values "Adams" Brush (2,16711680,16777215) Pen (1,2,0) ,"Asotin" Brush
Cut here .cut here
(2,6324352,16777215) Pen (1,2,0) ,"Whatcom" Brush (2,16711680,16777215) Pen (1,2,0) ,"Whitman" Brush (2,65280,16777215) Pen (1,2,0) ,"Yakima" Brush (2,255,16777215) Pen (1,2,0) default Brush (1,0,16777215) Pen (1,2,0) # color 1 #
Set the legend window. nwinNum is used once more
set map redraw on
set legend window nwinNum layer prev display on shades on symbols off lines off count on title auto Font ("Arial",0,9,0) subtitle auto Font ("Arial",0,8,0) ascending on ranges Font ("Arial",0,8,0) auto display off ,auto display on ,auto display on ,auto
Cut here .cut here
Create the cartograohic legend information. We have to use the nwinNum to id the correct map window
Create Cartographic Legend From Window nwinNum Behind Frame From Layer 6
Undeclare the variable
UnDim nwinNu
To generate the command list, the task was first performed interactively with the MapBasic window open. As each step was carried out, the command text was written to the MapBasic window. The resulting text was then copied into a text file; the workspace header information added; and a variable declared and initialized to obtain the map window identifier.

Conclusion
Text based scripting with MapInfo is a very useful tool. It is a relatively simple and inexpensive way to automate many tasks within MapInfo. In addition processes may actually execute faster when run from a workspace rather than from a compiled program. A workspace can be created and edited by any user, so workspace files need to be protected by windows security features if user editing is not desirable. Finally, the true power of workspace scripting is accessed by combining workspaces and compiled MapBasic programs. Workspaces add editable user defined templates to MapBasic programs, and MapBasic programs add a higher level of functionality and control to workspace scripts and the MapInfo program environment.
Additional Information
All data used for this discussion can be found on the MI 6.5 install CD. Other useful information can be found in passed NW MI User Group papers. In particular see the following papers on the user web site at http://www.sgsi.com
Workspaces, Tips & Tricks Sept 98
MapBooks and Atlases Jan 02
Workspaces, Tips & Tricks May 02
Layouts, Tips & Tricks Sept 02