November 2, 2021

Exploring a Python Script in Global Mapper Pro

Written by: Mackenzie Mills

 

Those who know Python or are looking to explore the use of this programming language in the context of GIS work, will be pleased to hear that Global Mapper Pro now integrates with the Python programming language. The new integration allows users to execute Global Mapper processes through scripts written in Python. In addition, Python scripts can be created and run in the new user interface Script Editor, or outside of the Global Mapper program in a Python environment.

Getting Started with Python in Global Mapper Pro

The creation and execution of Python scripts in Global Mapper requires both Global Mapper Pro and Python 3.9 to be installed on the machine. If you already have Python 3.9 installed, you can use the existing installation, or you can use the option while installing Global Mapper to get an embedded version of Python used specifically by Global Mapper.

Being a common programming language, Python opens opportunities to interweave Global Mapper functions with commands and operations from third-party libraries. Using Python to incorporate Global Mapper Pro into existing or new scripted processes expands the usability of the program.

Importing Global Mapper

To begin writing Global Mapper tools into Python scripts, you must import the Global Mapper module, globalmapper. This module contains the Global Mapper functions allowing you to load, create, analyze, and export data, and will be used to call commands from Global Mapper. This import is often shortened by users with the command import globalmapper as gm.

Running a Script

Python scripts can be written and edited in the Global Mapper Pro Script Editor or outside of the Global Mapper program. The Script Editor, like many other common tools and text editors used to write scripts in Python, includes syntax coloring to help users parse the content. Opening a script in this window, the functions and comments can be clearly seen. 

A screenshot of the script editor in Global Mapper
The Script Editor supports script creation in both Python and Global Mapper script and will display messages and errors in the Script Results portion when a script is run.

While this script does not need to be run in the Global Mapper Pro interface, doing so, and enabling the option to Run Script in the Context of Main View allows the loaded and created data to be seen in the Global Mapper interface. When the scripted process completes, users can quality check the generated data or continue analysis in the Global Mapper workspace. 

Example Script

This example script will load multiple data layers, change display options for layers and features, generate layers of contour lines, and export images with the generated data. 

Starting with setting up the export directory, keeping this variable definition at the top of the script allows it to more easily be changed by individual users. Layers from a network location are then loaded and if the layers do not load as expected a message is returned. Any printed messages will be shown in the script results area of the Script Editor. 

An example script text that can be run in Global Mapper.
Loading two layers from a network location and one layer from an online source, commands printing load confirmation methods are used to monitor the progress of the script.

While the file paths for the layers loaded in this script are written in the script, this workflow could be changed to prompt the user to select specific files to load to make this workflow more easily repeatable for multiple study areas. 

Using a while loop, two vector features from the States layer are identified and the vector feature display is changed for these two features. This is done by searching for the features and then using the SetAreaFeatureDrawStyle command to modify the style of the specific features. Since this process is creating a visual image output, setting the layer styles provides control over the look of data in the output image. 

Another example script text in Global Mapper.
With the areas for California and Nevada identified by an attribute, the draw style for these features is altered.

Next, another feature style edit is made, but this time for a Line Feature Type. A short loop changes the feature type for contour lines from their default brown style to a style that colors the lines based on the active elevation shader. This is a more permanent change to the Configuration of the Global Mapper installation. If these feature types are not reverted to their defaults later in the script, the changes will remain in Global Mapper and can be viewed in the Configuration > Styles section of the interface. 

The data creation completed by the script is the generation of contour lines. In this workflow, it was necessary to generate results three different times with different contour intervals. Starting this process the three desired contour intervals are listed. These contour interval values are then used in a for loop. A for loop goes through the same process for each value in the list of intervals. The loop uses the first value to create the contour line layer and export an image before starting the next interaction of the loop with the next listed value. 

Generating contours via script in Global Mapper
In the GenerateContours setup, the contour interval and created layer name are set, but all other parameters not mentioned use the default Global Mapper values.

Since three contour interval values are listed, three JPEG format images are exported from Global Mapper to the export directory defined at the start of the script. Each of these images has a unique file name with the contour interval noted. 

A comparison of different contour line outputs.
Three image outputs are generated from this script showing contours for the same area at different intervals.

Global Mapper Pro and the Python programming language are both incredibly versatile and useful tools for many GIS professionals. With options to automate processes over a series of values or files, Python integration with Global Mapper Pro greatly improves the efficiency of data processing and creation. 

If you would try working in Python with Global Mapper Pro, download a 14-day free trial today! If you have any questions, don’t hesitate to get in touch.

If you enjoyed this blog, you may also find these other resources useful:

Ask the Experts: Support for Python Scripting

Exploring a Python Script in Global Mapper Pro

Companies using Blue Marble’s geospatial technology