Thursday, March 12, 2026

[016] Global Mapper - Python Integration: Saving Vectors as Images

Today, I am posting a piece I wrote a while ago and had been putting off, after some refinement. I ask for your understanding; it has been difficult to concentrate on writing lately.


The reason I created this script was because I saw a close junior of mine manually zooming in and out of hundreds of parcels in CAD, capturing each one for a report. I thought he might not survive long if he kept working like that. I made this to solve the problem for him the next time he faced a similar task.

Main map view showing a satellite image background with bright green parcel vector outlines


As shown in the image above, you can divide the cadastral map by parcel into separate layers and then perform the following operations. (Global Mapper users will likely find this basic.)

I will cover how to separate the layers in a future post.

I originally planned to introduce integration with external languages after the lectures had progressed a bit more, but for now, just take a look at it to see that this kind of functionality is possible.

How to Use

  1. The satellite image layer (raster layer) at the bottom must be at the very top of the layer list. Turn off all other raster layers.
  2. Each parcel must be separated into its own layer. The layer description should be in English.
  3. Turn off all vector layers related to the parcels.
  4. Go to the File menu -> Open Script Editor, paste the code below, and save it with an appropriate name.
  5. Project folders and paths must all be in English.
  6. You can change the image save path on line 35.
  7. The image aspect ratio can be adjusted on line 9. It is currently set to 16:9.
  8. The horizontal pixel width is set on line 70. The vertical pixels are automatically determined to maintain the 16:9 ratio.
  9. Click the Run Script button to execute.
  10. Once you click Run Script, JPG images will be created in the specified path.

#/usr/bin/env python 
# coding=utf8 
import globalmapper as gm 
import os 

zoom_out_factor = 1.5
aspect_ratio = 16/9 

layers_are_loaded = gm.GetLoadedLayerList() 

if not layers_are_loaded: 
    print("No layers are currently open, so nothing was exported.") 
else: 
    arr_ptr, arr_size = layers_are_loaded 
    all_layers = gm.GM_LayerHandle_array_frompointer(arr_ptr) 
    raster_layers = [] 
    vector_layers = [] 
    
    for layer in gm.carray_to_list(all_layers, arr_size): 
        layer_info = gm.GetLayerInfo(layer) 
        if layer_info.mHasVectorData:
            vector_layers.append(layer) 
        elif layer_info.mHasRasterData: 
            raster_layers.append(layer) 
            
    if len(vector_layers) == 0:
        print("No vector layers were found, so nothing was exported.") 
    elif len(raster_layers) == 0:
        print("No raster layers were found to use as background.") 
    else: 
        # Set your export path here
        output_directory = os.path.expanduser("F:\\2024-08-26_export\\result\\") 
        print("Exporting files to {}".format(output_directory)) 
        
        gm.SetLayerEnabled(raster_layers[0], True) 
        
        for layer in vector_layers: 
            layer_info = gm.GetLayerInfo(layer) 
            try: 
                # Character encoding handling
                if layer_info.mCodePage == 949: 
                    layer_name = layer_info.mDescription.encode('raw_unicode_escape').decode('cp949') 
                elif layer_info.mCodePage == 0: 
                    layer_name = layer_info.mDescription.encode("latin1").decode("cp1252") 
                else: 
                    layer_name = layer_info.mDescription 
            except UnicodeDecodeError: 
                layer_name = layer_info.mDescription 
                
            name_wo_ext = layer_name[:layer_name.index(".")] if "." in layer_name else layer_name 
            name_wo_ext = name_wo_ext.replace(" ", "_") 
            name_wo_ext = "".join(c for c in name_wo_ext if c.isalnum() or c in "_-") 
            
            # Calculating bounding box and aspect ratio
            width_meters = layer_info.mGlobalRect.mMaxX - layer_info.mGlobalRect.mMinX 
            height_meters = layer_info.mGlobalRect.mMaxY - layer_info.mGlobalRect.mMinY 
            dim_ratio = width_meters / height_meters 
            
            center_x = (layer_info.mGlobalRect.mMaxX + layer_info.mGlobalRect.mMinX) / 2 
            center_y = (layer_info.mGlobalRect.mMaxY + layer_info.mGlobalRect.mMinY) / 2 
            
            if dim_ratio > aspect_ratio:
                new_width = width_meters * zoom_out_factor
                new_height = new_width / aspect_ratio
            else:
                new_height = height_meters * zoom_out_factor
                new_width = new_height * aspect_ratio
                
            new_min_x = center_x - new_width / 2 
            new_max_x = center_x + new_width / 2 
            new_min_y = center_y - new_height / 2 
            new_max_y = center_y + new_height / 2 
            
            export_rect = gm.GM_Rectangle_t(new_min_x, new_min_y, new_max_x, new_max_y) 
            
            gm.SetLayerEnabled(layer, True) 
            output_filename = os.path.join(output_directory, "{}.jpg".format(name_wo_ext)) 
            
            HD_WIDTH = 800 
            HD_HEIGHT = int(HD_WIDTH / aspect_ratio) 
            
            error_code = gm.ExportRaster(output_filename, gm.GM_Export_JPG, 0x0, export_rect, HD_WIDTH, HD_HEIGHT, gm.GM_ExportFlags_AddAlpha) 
            
            if error_code == gm.GM_Error_None: 
                print("Exported {} to {}".format(layer_name, output_filename)) 
            else: 
                print("Error exporting {}: {}".format(layer_name, gm.strerror(error_code))) 
                
            gm.SetLayerEnabled(layer, False) 

        gm.SetLayerEnabled(raster_layers[0], False)


I tinkered with lines 47–54 while trying to handle character conversion, but eventually left it as is. It works fine for English, so you can use it without issues. I was going to fix it but felt a bit too lazy.

I tested it with 500 parcels, and the export finished in less than 20 seconds. It’s incredibly satisfying.

Windows Explorer view showing a grid of exported parcel images with satellite backgrounds

As a side note, when using Python scripts in Global Mapper, you should handle everything in English. The path where the workspace is saved, the workspace name itself, and even the comments should ideally be in English for best compatibility.

That's all for today...

Wednesday, March 11, 2026

012] Global Mapper - Terrain Information Lines: Calculate Elevation/Slope Stats

Today, we will learn how to extract terrain information using Line features.

To follow along, please open the data file we used in the previous session.

Global Mapper screen showing the terrain, boundary, and road line from the previous session


1. Extracting Terrain Information for a Line

The process is identical to the one we used for polygons. We will use the same tool to analyze the terrain along our "Road" feature.

  1. Select the Road line using the Digitizer Tool.

  2. Right-click and select Analysis/Measurement -> Calculate Elevation/Slope Stats for Selected Feature(s)....

Right-click menu on the line feature selecting Calculate Elevation/Slope Stats


  1. A dialog will appear asking if you'd like to create new point features at the minimum and maximum elevations. Choose your preference (Yes to add points, No to just calculate).

Point creation confirmation dialog for min/max elevation points

If you select "Yes," points will be generated on your map showing the exact locations of the highest and lowest elevations along that line.

Main screen showing the road with blue and yellow triangle markers at min/max spots


2. Understanding Line-Specific Statistics

The Feature Information window will now display the results. While similar to the polygon results, there are several key items specific to line features.

Feature Information window showing comprehensive statistics for the road line


Key Statistics Explained for Lines:

  1. MAX_UP_SLOPE_DEG/PCT: The steepest incline (uphill) encountered along the path.

  2. MAX_DOWN_SLOPE_DEG/PCT: The steepest decline (downhill) encountered along the path.

  3. ELEV_GAIN_M/FT: The total cumulative elevation gain (sum of all uphill segments).

  4. ELEV_LOSS_M/FT: The total cumulative elevation loss (sum of all downhill segments).

  5. LENGTH_3D: The actual 3D distance of the line, taking the terrain's rises and falls into account.

Note on Direction:

In Global Mapper, lines have a direction. The Start point is marked in Green, and the End point is marked in Red when selected. "Up" and "Down" are relative to moving from the green point to the red point.

In this example, the ELEV_GAIN is 26.91m and the ELEV_LOSS is 16.94m. This means that while traveling from the start to the end, you climbed a total of 26.91m and descended 16.94m. Consequently, the end point is approximately 10m higher than the start.


3. Review: Using Path Profile

Let's visualize this data using the Path Profile tool we covered previously.

  1. With the line selected, right-click and select Analysis/Measurement -> PATH PROFILE - Generate Path Profile Along Line(s)....

Right-click menu selecting Path Profile


A cross-section of the terrain along the line will appear. The yellow dots at the bottom represent the vertices (corners) of the line.

Initial Path Profile window showing the terrain cross-section


The default view might show a lot of empty space below the terrain. Let's adjust the settings to focus on the relevant elevation.

  1. Go to Path Setup -> Path Profile Settings....

Path Profile menu selecting Settings


  1. In the General Settings tab:

    • Elevation Range: Check Clamp Bottom Displayed Elevation to and enter 75 (meters).

    • Guidelines: Check Draw Reference Line at and enter 90 (meters).

    • Click OK.

Path Profile Settings dialog with custom elevation and reference line values



Now, the unnecessary bottom space is removed, and a clear reference line is added at 90m.

Final Path Profile view with optimized elevation range and reference line


This analysis is incredibly useful for evaluating hiking trails, bicycle paths, or planned road routes. If you have multiple potential paths from point A to point B, comparing the total elevation gain and loss will tell you which route is the easiest or most challenging.

That’s all for today!