Python Scripting in Global Mapper Pro
Global Mapper Pro, the latest development from Blue Marble Geographics, is a complete geospatial software program containing functionality from data creation to advanced point cloud editing and analysis. Built as an expansion to the base Global Mapper standard program, the Pro edition makes a suite of cutting-edge geospatial analysis tools accessible to novice and expert users. Included in Global Mapper Pro is the ability to use the Python programming language to automate workflows and combine the power of Global Mapper Pro with tools from third-party libraries.
To learn more about how Global Mapper Pro can help you, check out the Blue Marble blog and past webinars posted on our YouTube channel. To test out Global Mapper Pro, download a 14-day free trial today!
One of the most popular programming languages, Python is an interpreted language that is easy to learn, read, and write. Due to Python’s popularity as an open-source language, it has been the basis for much workflow automation throughout GIS and other industries.
With the base installation of Python containing a comprehensive standard library, users can hit the ground running and begin scripting using the built-in keywords, conditional statements, loops, and operators. Adding to the basic installation of Python, packages and libraries (collections of packages) can be installed and used in scripts. With many libraries available, they can be used in combination in a single Python script to accomplish a larger task or workflow.
Global Mapper Pro contains a library of Python functions that can be called and executed through the Python programming language. To use the Global Mapper functions through Python, Global Mapper Pro needs to be installed and licensed, and Python 3.9 needs to be installed. A basic installation of Python 3.9 can be added to a machine through the Extra Downloads option in the Global Mapper program installer, or an existing installation of Python can be used. To use an existing version of Python, add the path to the installed Global Mapper Pro program to the PythonPath environment variable.
Like with any Python package or library, Global Mapper must be imported before any of the program-specific functionality can be used. Use the line import globalmapper to import the Global Mapper Pro library of Python functions. Often users will shorten this name to gm (import globalmapper as gm) to make the name more concise when calling functions later in the script.
The Python programming language can be written in any text editor, an integrated development environment (IDE), or Global Mapper Pro’s user interface Script Editor. Wherever you choose to create your Python scripts, they can be saved with a *.py extension, marking them as an executable Python script, and opened again in any editor for future modification.
When creating a script in many text editors, IDEs, and Global Mapper Pro’s Script Editor, syntax highlighting will help to differentiate parts of the script, making it easier to read and understand. Even as an easily readable language, adding comments to any script helps to clarify the workflow and individual sections and lines. Comments are particularly important when sharing a script with other users.
The Global Mapper Python library is object-oriented, meaning that defined objects containing specific parameters are passed to the function to complete a task. This allows sections of script to be re-used throughout a longer workflow. With GIS workflows being mostly data-driven, scripts using the Python Global Mapper library often work with imported data files to process, modify, or extract new data. By accessing the Global Mapper Pro functionality through Python, users can include tools from third-party Python libraries to more efficiently execute a process that would be tedious, time-consuming, or near impossible through installed program interfaces.
A created Python script can be run from an IDE, the command line, or through Global Mapper Pro’s Script Editor. Any of these methods will use the imported Global Mapper library of commands, provided Global Mapper Pro is installed and licensed on the machine.
A more typical way to run a Python script, through the command line or an IDE, does not require the Global Mapper program to be open. Running the script by selecting to run it from an interface menu or by simply pointing to a *.py file in the command line will utilize the Global Mapper functionality, and the tools from any other third-party Python libraries, in the background. Any errors or logged messages will print to the designated file or window as the script runs.
A Python script run through the Global Mapper Pro Script Editor can utilize currently open layers of data in the workspace and, conversely, load imported and created data as the script runs. While not required, the Run Script in the Context of the Main View checkbox in the Script Editor window will display data in the program interface as the script runs and completes. If left unchecked, the Script Editor acts as an environment for executing the script and reporting any errors or logged messages in the Script Results window.
As an example, this script checks for open point cloud layers in the active Global Mapper interface prompts the user to load data if none is found, and then processes the raw lidar data into a gridded digital terrain model.
To use the Global Mapper tools through this Python script, the Global Mapper library is imported with the shortened name gm. Another module, tkinter, is also imported in the script and will be used to create interactive Windows dialogs that allow the user to select a location to save generated data.
The first action in this script checks the active Global Mapper workspace to determine if any layers are already loaded and, if so, whether they are lidar point cloud layers. Using IF statements, one of two control flows is followed based on the presence of data. If no lidar layers are found, the user is prompted to select a lidar file to load.
Now that the script has identified lidar data to use, the process begins by selecting points and calling the LidarClassifyNoise method. This method cleans up any noise that exists in the raw data file, such as birds, reflections, or sensor anomalies. Removing noise from the point cloud allows for better classification as the workflow progresses.
With noise points removed, ground is then identified in the point cloud using the LidarClassifyGround method from the Global Mapper Pro Python library. For this example, the default processing parameters are used, and any pre-classified ground points are cleared using the GM_ClassifyGroundSetup_t() class which is passed into the ClassifyGround method.
The classified and cleaned-up data is then filtered by applying the GetLidarClassFilter method. This filter can be used to identify and separate any point cloud data based on its identified classification. Since the LidarClassifyGround method has already been called, the process can be initialized using the GM_LidarClass_Ground type, then applied to the data using the SetLidarClassFilter call.
The next step of the script identifies the geographic bounds that will be used in creating and exporting a digital elevation model. These values are returned in the coordinate system of the lidar layer using a call to the GetLayerInfo method. In this case, the GetLayerInfo method is used to explicitly look at the mGlobalRect parameter.
The elevation grid is then generated from the filtered lidar dataset using the GenerateElevationGrid call. This method is initialized using a GM_GridGenSetup_t class, which is defined with the required resolution, grid generation algorithm, and gap-filling parameters. A successful call to the GenerateElevationGrid method will return a new variable called grid_layer representing the newly generated data layer.
Using the previously identified bounds, CalcDistance converts from the coordinate system units to meters and uses these values to calculate the scale for the export resolution. In this example, the data is exported at a one-meter resolution, but that can be modified using the pixel_resolution variable at the top of the file.
Finally, the user is prompted for an output file path using stock Python tk interface dialogs. The grid_layer variable generated above is then used as the base for exporting a DEM file. The details are passed to the ExportElevation method, and a file is created and written to disk.
More information on Python scripting with Global Mapper Pro can be found in the following blogs:
These short videos help to demonstrate Python use in Global Mapper Pro:
The Global Mapper library of Python commands can be found here: www.bluemarblegeo.com/knowledgebase/global-mapper-python-23-1/index.html
Additional Python script examples are available here: www.bluemarblegeo.com/knowledgebase/global-mapper-python-23-1/sampleCode/DemoMisc.html
To use Global Mapper Pro functionality in your Python scripts, download a 14-day trial today or purchase a full version of the program.