Exporting points to GPX with Cartographica


This morning, I had a request from a user to be able to send Point features from a layer in a Cartographica map document to a GPS. Although Cartographica is able to import data directly from GPS devices, we don't currently have a built-in method to export layer data to GPS devices. However, as with the complex label question, we can solve this one reasonably well using AppleScript. This article describes how we did this and give the AppleScript for doing it.

For those unfamiliar with AppleScript, we had some more in-depth commentary on it in our Advanced label techniques in Cartographica article from earlier this month.

To solve this particular problem, we are going to use AppleScript to export a GPX file from Cartographica and then use LoadMyTracks (ClueTrust's free GPS upload/download App) to send the file to the GPS device.

The Technique

We are using AppleScript to access the front-most Cartographica document and then spin through each feature in a particular layer, extracting the latitude and longitude information and optionally a name and comment field. This is then written to the specified file and is ready to send with LoadMyTracks. The script is available for download from our ClueTrust servers.

Note: This technique will only work with layers that are naturally in WGS84 coordinates. If you have projected coordinates, you will need to either reproject the layer, or create a copy of the layer and reproject that, using the reprojected layer as the export layer. Otherwise, you'll end up with coordinates outside of the range of the GPX file format, and likely beyond the range of your GPS device.

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.

Unlike the previous example, this one isn't tied to a particular document. In this case, we've created a generic script that talks to the front-most Cartographica document and prompts the user for the layer and fields to export.

First, we look through the layers and find ones that are points (shape type = 1), and we make a list of them.

-- get the layers that qualify (i.e. they're points)
set layerNames to {}
set allLayers to every layer in thedoc
repeat with aLayer in allLayers
	if the base shape type of aLayer = 1 then
		set layerNames to layerNames & the name of aLayer
	end if
end repeat

Then we prompt the user for their choice

-- now that we have a list of qualified layers
choose from list layerNames with prompt "Create GPX File from which Point Layer?"

We then figure out which columns are valid for use (the example here actually does more work than necessary, finding all Number and String columns, but then actually prompting for all columns. You may want to constrain the column choices, but since we coerce the data to text before we use it, any column may be used.

The user is prompted for the name and comment columns

set nameColumn to ""
choose from list allColumns with prompt "Waypoint name from which column?"
    if the result is not false then
        set nameColumn to item 1 of the result
    end if
set commentColumn to ""
choose from list allColumns with prompt "Waypoint comment from which column?"
if the result is not false then
    set commentColumn to item 1 of the result
end if

And then the file name

choose file name with prompt "Output GPX file name:" default name layerName & ".gpx"

Then comes the meat of the script. We go through each of the features in the selected layer getting the first point of the feature

repeat with dataPoint in every feature in pointLayer
   set sourceGeopoint to geopoint 1 of part 1 of geometry of dataPoint

and adds the waypoint information and optionally the comment and name information to our output (only the name is shown here, as the comment process is exactly the same).

   set output to output & "<wpt lat='" & y of sourceGeopoint & "' lon='" & x of sourceGeopoint & "'>"
   -- name
   if nameColumn = "" then
       -- skip it
   else
       set output to (output & "<name>" & value of field data named nameColumn of dataPoint as string) & "</name>"
   end if

we then close up the waypoint XML and continue to the next feature.

When that's all done, we take the accumulated information and stick it into the GPX file:

set fileRef to open for access outputFile with write permission
write output to fileRef
close access fileRef

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 is just another example of what you can do with Cartographica, even when the software itself doesn't contain a particular feature.

Editor: updated to post-1.2 use of feature instead of entity for geographic features


← Cartographica 1.1.2 is now available | Advanced label techniques in Cartographica →
More in Cartographica
← Cartographica 1.1.2 is now available | Mapping Al`Qaida Screencast →