Advanced label techniques in Cartographica

Cartographica contains some powerful features, but there are times when you need to combine things that aren't obviously available in current versions. One such situation came up with a customer who was interested in combining two or more fields to create a label. Although future versions will likely have ways to do simple label combinations, we wanted to show how you can use AppleScript and Cartographica today to make labels that not only combine the contents of more than one field, but that do so in ways that provide extra flexibility.

Because some of the techniques used in this post are pretty complex, we're going to make use of not only the blog posting, but also a screencast of the application and AppleScript.

AppleScript

Many of you who are new to the Macintosh may not realize that there is a sophisticated, multi-application scripting language built right in to OS X called AppleScript. If you go to the Utilities folder and look for AppleScript Editor, you will find the primary method of writing and running AppleScript scripts (or programs, if you are so inclined to call them).

AppleScript was designed as a system that allows application developers, like ClueTrust, to publish a series of commands for use with their programs, thus providing a single, system-wide mechanism for controlling not just one program, but any scriptable program on the Macintosh. Over the years, people have used these techniques to automate workflow tasks and add subtle feature to existing programs. Today, we're going to do a little of both.

The Goal

The goal of today's posting is to create complex labels for a Cartographica document. For this example, we're going to take the DC Metro system map and add line information to the station names. First, we'll create a two-line label that has the station name on the first line and the line names on the second line. Then, we'll use a more sophisticated technique to create a one-line label that has the first initial of each line name put together in parenthesis after the station name.

We will be using the test data that we acquired from the DC GIS site and which is warehoused with our Screencasts. If you already have our tutorial test data, you don't need to download it again.

The technique

We are using AppleScript to access the frontmost Cartographica document and then spin through each feature in a particular layer, adding a column if necessary and then changing the data for that column to be our new label. After this, you can then use that new column as the label in the layer. The script is available for download from our ClueTrust servers.

The script

We've broken the script down into smaller chunks to explain how it works. If you open the script in AppleScript, you can see how things work. We encourage you to play around with it once you have figured out how it functions.

Basically, it finds the MetroEntPt layer in the frontmost document

	-- Work on the frontmost document
 
set thedoc to the document of the front window
	-- get the layer we'll be changing
set theLayer to layer named "MetroEntPt" in thedoc

creates the DisplayText column if one doesn't exist.

	try
set resultColumn to column named "DisplayText" of theLayer
on error errMsg number errNum
set resultColumn to make new column at end of columns of theLayer with properties
{name:"DisplayText", type:"String", length:250, precision:0, displayName:"Display Text"}
end try

Sets the one line or two line label (make sure you show changing this, because it's cool for people to see how they can change the script and have the labels change automatically.... I'd run the script the first time, set the labels so that they're visible on the screen and then go back to applescript, move the "--" from in front of the second line to in front of the first line in this pair, and then hit run again).

	set labelType to "TwoLine"
--
set labelType to "OneLine"

Then it cycles through every feature in the layer:

	repeat with entryPoint in every feature in theLayer

and in the case of the simple 2 line version, just put's the two fields together with a line in between them:

			set labelText to the value of field data "NAME" of entryPoint & "
(" & the value of field data "LINE" of entryPoint & ")"

The other version of the change is more involved, basically looking for the line name in the LINE field, and if it's there, it adds a single character to represent the line. Look at just one of the pieces of code that do this:

			if ((offset of "red" in lineList) is not 0) then
set labelText to labelText & "R"
end if

See how it looks to see if "red" is in lineList? (We have to use offset of to determine if it's in the string... if the offset is 0, it's not in there), and then it sets the label to the label with R at the end of it.At the very end, it sets the field value to the computed label:

		set the value of field data "DisplayText" of entryPoint to labelText

And that's pretty much it. We have used AppleScript in Cartographica since the beginning in order to prototype new features and help customers who have particular needs. This example just scratches the surface of what's available when using AppleScript with Cartographica.

Editor:Updated for post-1.2 use of feature instead of entity