The display options/drawing functions let you customize how data layers are visually rendered. They can modify the shading, background color, orientation, display options, etc. of your maps and Global Mapper workspace.
Crop, Merge, and Reproject USGS DRGs to GeoTIFF/JPEG
Note: Before running this sample, configure these variables:
import_dir: the directory containing sample dataexport_dir: the directory for resulting files to be exported to
This script loads digital raster graphics (DRGs), clips the collars off of the image layers, calculates the export dimensions to be used, and exports them new GeoTIFF and JPEG files. The first export creates a standard GeoTIFF, the second creates a grayscale GeoTIFF and generates a world file, the third exports a JPEG with both a world file and a projection file.
import os
import globalmapper as gm
import_dir = "Z:/GM-20069/crop_DRG_to_raster_sample_data/"
export_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "../misc/crop_DRG_to_raster_output/")
os.makedirs(export_dir, exist_ok=True)
# This script loads digital raster graphics (DRGs), clips the collars off of the image layers, calculates the export dimensions
# to be used, and exports them new GeoTIFF and JPEG files. The first export creates a standard GeoTIFF, the second creates a
# grayscale GeoTIFF and generates a world file, the third exports a JPEG with both a world file and a projection file.
# Close all layers that may be open
layers_are_open = gm.GetLoadedLayerList()
if layers_are_open:
arr_ptr, arr_size = layers_are_open
initial_layers = gm.GM_LayerHandle_array_frompointer(arr_ptr)
for i in range(arr_size):
gm.CloseLayer(initial_layers[i])
err_LoadLayerList1, array_pointer, array_size_param = gm.LoadLayerList(
os.path.join(import_dir, "O40079C3.tif"),
gm.GM_LoadFlags_UseDefaultLoadOpts,
)
err_LoadLayerList2, array_pointer, array_size_param = gm.LoadLayerList(
os.path.join(import_dir, "O40079C4.tif"),
gm.GM_LoadFlags_UseDefaultLoadOpts,
)
err_LoadLayerList3, array_pointer, array_size_param = gm.LoadLayerList(
os.path.join(import_dir, "O40079C5.tif"),
gm.GM_LoadFlags_UseDefaultLoadOpts,
)
# get handles for the loaded layers
arr_ptr, arr_size = gm.GetLoadedLayerList()
layers = gm.GM_LayerHandle_array_frompointer(arr_ptr)
### START SAMPLE
# Automatically clip the collar off the image layers
bounds = []
for i in range(arr_size):
err_GetRasterDisplayOptions, opts = gm.GetRasterDisplayOptions(layers[i])
opts.mAutoClipCollar = True
err_SetRasterDisplayOptions = gm.SetRasterDisplayOptions(layers[i], opts)
bounds.append(gm.GetLayerInfo(layers[i]).mGlobalRect)
# Set the projection manually
# The projection file is located within the specified import_dir
err_LoadProjectionFile, proj = gm.LoadProjectionFile(import_dir + "UTM-17N.prj")
err_SetProjection = gm.SetProjection(proj)
# determine the dimensions of the image
bounding_rect = bounds[0]
for rect in bounds[1:]:
bounding_rect.mMinX = (
rect.mMinX if (bounding_rect.mMinX > rect.mMinX) else bounding_rect.mMinX
)
bounding_rect.mMinY = (
rect.mMinY if (bounding_rect.mMinY > rect.mMinY) else bounding_rect.mMinY
)
bounding_rect.mMaxX = (
rect.mMaxX if (bounding_rect.mMaxX < rect.mMaxX) else bounding_rect.mMaxX
)
bounding_rect.mMaxY = (
rect.mMaxY if (bounding_rect.mMaxY < rect.mMaxY) else bounding_rect.mMaxY
)
ratio = abs(bounding_rect.mMaxX - bounding_rect.mMinX) / abs(
bounding_rect.mMaxY - bounding_rect.mMinY
)
# Make sure the exported layers are at least HD
width = 1920
height = width * (1 / ratio)
if height < 1080:
height = 1080
width = height * ratio
width = int(width)
height = int(height)
err_ExportRaster1 = gm.ExportRaster(
export_dir + "merged_drg_8bpp.tif",
gm.GM_Export_GeoTIFF,
0x0,
None,
width,
height,
0x0,
)
err_ExportRaster2 = gm.ExportRaster(
export_dir + "merged_drg_gray.tif",
gm.GM_Export_GeoTIFF,
0x0,
None,
width,
height,
gm.GM_ExportFlags_GenWorldFile + gm.GM_ExportFlags_Grayscale,
)
err_ExportRaster3 = gm.ExportRaster(
export_dir + "merged_drg.jpg",
gm.GM_Export_JPG,
0x0,
None,
width,
height,
gm.GM_ExportFlags_GenWorldFile + gm.GM_ExportFlags_GenPRJFile,
)
### END SAMPLE
assert (
gm.GM_Error_None == err_GetRasterDisplayOptions
), f"GetRasterDisplayOptions: Expected {gm.GM_Error_None}, got {err_GetRasterDisplayOptions}"
assert (
gm.GM_Error_None == err_LoadProjectionFile
), f"LoadProjectionFile: Expected {gm.GM_Error_None}, got {err_LoadProjectionFile}"
assert (
gm.GM_Error_None == err_LoadLayerList1
), f"LoadLayerList1: Expected {gm.GM_Error_None}, got {err_LoadLayerList1}"
assert (
gm.GM_Error_None == err_LoadLayerList2
), f"LoadLayerList2: Expected {gm.GM_Error_None}, got {err_LoadLayerList2}"
assert (
gm.GM_Error_None == err_LoadLayerList3
), f"LoadLayerList3: Expected {gm.GM_Error_None}, got {err_LoadLayerList3}"
assert (
gm.GM_Error_None == err_SetRasterDisplayOptions
), f"SetRasterDisplayOptions: Expected {gm.GM_Error_None}, got {err_SetRasterDisplayOptions}"
assert (
gm.GM_Error_None == err_SetProjection
), f"SetProjection: Expected {gm.GM_Error_None}, got {err_SetProjection}"
assert (
gm.GM_Error_None == err_ExportRaster1
), f"ExportRaster1: Expected {gm.GM_Error_None}, got {err_ExportRaster1}"
assert (
gm.GM_Error_None == err_ExportRaster2
), f"ExportRaster2: Expected {gm.GM_Error_None}, got {err_ExportRaster2}"
assert (
gm.GM_Error_None == err_ExportRaster3
), f"ExportRaster3: Expected {gm.GM_Error_None}, got {err_ExportRaster3}"
assert os.path.isdir(export_dir), f"Error creating the export directory: {export_dir}"
assert os.path.isdir(import_dir), f"Import directory doesn't exist: {import_dir}"
input_filenames = [
"O40079C3.tif",
"O40079C4.tif",
"O40079C5.tif",
"UTM-17N.prj",
]
# Make sure all expected files were imported
for name in input_filenames:
full_filepath = os.path.join(import_dir, name)
# Assert that each expected file exists
assert os.path.exists(
full_filepath
), f"Expected output file does not exist: {full_filepath}"
# Assert that each expected file is not empty
assert (
os.path.getsize(full_filepath) > 0
), f"Exported file is empty: {full_filepath}"
export_filenames = [
"merged_drg_8bpp.tif",
"merged_drg_gray.tfw",
"merged_drg_gray.tif",
"merged_drg.jgw",
"merged_drg.jpg",
"merged_drg.prj",
]
# Make sure all expected files were exported
for name in export_filenames:
full_filepath = os.path.join(export_dir, name)
# Assert that each expected file exists
assert os.path.exists(
full_filepath
), f"Expected output file does not exist: {full_filepath}"
# Assert that each expected file is not empty
assert (
os.path.getsize(full_filepath) > 0
), f"Exported file is empty: {full_filepath}"
# Check variable values
# These are the values they had when the sample was created
assert arr_size == 3, "Issue loading layers"
assert bounding_rect.mMaxX == 650134.36855, "Incorrect bounding_rect.mMaxX value"
assert bounding_rect.mMinX == 615616.344719, "Incorrect bounding_rect.mMinX value"
assert bounding_rect.mMaxY == 4471382.107305, "Incorrect bounding_rect.mMaxY value"
assert bounding_rect.mMinY == 4454461.369194999, "Incorrect bounding_rect.mMinY value"
assert ratio == 2.039983339178306, "Incorrect ratio value"
assert width == 2203, "Incorrect width value"
assert height == 1080, "Incorrect height value"
GMW Color Customization
Note: Before running this sample, configure these variables:
export_dir: the directory for resulting files to be exported to
This sample demonstrates how to change the background color and color palettes of a Global Mapper workspace.
import os
import globalmapper as gm
import_dir = "Z:/GM-20069/single_function_sample_data/"
export_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "../misc/customize_color_output/")
os.makedirs(export_dir, exist_ok=True)
# This sample demonstrates how to change the background color and
# color palettes of a Global Mapper workspace.
# Close all layers that may be open
layers_are_open = gm.GetLoadedLayerList()
if layers_are_open:
arr_ptr, arr_size = layers_are_open
initial_layers = gm.GM_LayerHandle_array_frompointer(arr_ptr)
for i in range(arr_size):
gm.CloseLayer(initial_layers[i])
err_LoadLayerList, array_pointer, array_size_param = gm.LoadLayerList(
os.path.join(import_dir, "O40079C4.TIF"),
gm.GM_LoadFlags_UseDefaultLoadOpts,
)
# Get handle for the loaded layer
arr_ptr, arr_size = gm.GetLoadedLayerList()
layers_array = gm.GM_LayerHandle_array_frompointer(arr_ptr)
### START SAMPLE
cur_back_color = gm.GetBackgroundColor()
print("The current background color is:", cur_back_color, "(COLORREF)")
# Sets the background color to light blue (Only visible in a Global Mapper workspace)
# Converts RGB to COLORREF (needed for color operations)
orig_background_color = gm.SetBackgroundColor(gm.toRGB(165, 211, 235))
# Palettes use COLORREF to assign color
new_palette_purple = gm.GM_PaletteEntry_t()
COLORREF_purple = gm.toRGB(125, 43, 243)
new_palette_purple.mColor = COLORREF_purple
new_palette_purple.mName = "Purple Palette"
new_palette_black = gm.GM_PaletteEntry_t()
COLORREF_black = gm.toRGB(0, 0, 0)
new_palette_black.mColor = COLORREF_black
new_palette_black.mName = "Black Palette"
# Changing the first 12 palette colors will show the most change in layer color
# To do this, add palette preferences to the palette list
# The following palette sets text to purple and the TIF background to black
err_SetLayerPalette1 = gm.SetLayerPalette(layers_array[0], [new_palette_purple, new_palette_black], 0)
# Get the right dimensions for the image
bounds = gm.GetLayerInfo(layers_array[0]).mGlobalRect
ratio = abs(bounds.mMaxX - bounds.mMinX) / abs(bounds.mMaxY - bounds.mMinY)
width = 1920
height = width * (1 / ratio)
if height < 1080: # make sure the exported layers are at least HD
height = 1080
width = height * ratio
width = int(width)
height = int(height)
err_ExportRaster = gm.ExportRaster(
export_dir + "custom_colors.jpg",
gm.GM_Export_JPG,
0x0,
None,
width,
height,
0x0,
)
# Resets all color changes made in this sample
# Resets background color
prev_background_color = gm.SetBackgroundColor(orig_background_color)
# Resets the layer palette
err_SetLayerPalette2 = gm.SetLayerPalette(layers_array[0], None, 0)
### END SAMPLE
assert (
gm.GM_Error_None == err_LoadLayerList
), f"LoadLayerList: Expected {gm.GM_Error_None}, got {err_LoadLayerList}"
assert (
gm.GM_Error_None == err_SetLayerPalette1
), f"SetLayerPalette1: Expected {gm.GM_Error_None}, got {err_SetLayerPalette1}"
assert (
gm.GM_Error_None == err_SetLayerPalette2
), f"SetLayerPalette2: Expected {gm.GM_Error_None}, got {err_SetLayerPalette2}"
assert (
gm.GM_Error_None == err_ExportRaster
), f"ExportRaster: Expected {gm.GM_Error_None}, got {err_ExportRaster}"
assert os.path.isdir(export_dir), f"Error creating the export directory: {export_dir}"
assert os.path.isdir(import_dir), f"Import directory doesn't exist: {import_dir}"
assert os.path.exists(
os.path.join(import_dir, "O40079C4.TIF")
), "Sample data doesn't exist"
# Expected path for file export
exported_filepath = os.path.join(export_dir, "custom_colors.jpg")
# Assert that the final expected output file exists and is not empty
assert os.path.exists(
exported_filepath
), f"Expected output file does not exist: {exported_filepath}"
assert (
os.path.getsize(exported_filepath) > 0
), f"Exported file is empty: {exported_filepath}"
# Check variable values
# These are the values they had when the sample was created
assert bounds.mMaxX == 639437.429491, "Incorrect bounds.mMaxX value"
assert bounds.mMinX == 626140.834291, "Incorrect bounds.mMinX value"
assert bounds.mMaxY == 4471207.497834, "Incorrect bounds.mMaxY value"
assert bounds.mMinY == 4454653.200234, "Incorrect bounds.mMinY value"
assert width == 1920, "Incorrect width value"
assert height == 2390, "Incorrect height value"
assert ratio == 0.803211076741779, "Incorrect ratio value"
Create Elevation Shader
This script generates a custom elevation shader for a loaded layer using a set of 10 RGB colors. Each color is mapped to a specific elevation range, which is calculated by dividing the difference between the layer’s minimum and maximum elevation values by the number of colors. This approach creates a smooth gradient effect across the elevation spectrum, enhanced by the chosen color palette. Once the shader is created, it is assigned a name and applied to the specified layer.
import os
import globalmapper as gm
import_dir = "Z:/GM-20069/single_function_sample_data/"
# This script generates a custom elevation shader for a loaded layer using a set of 10 RGB colors. Each color is
# mapped to a specific elevation range, which is calculated by dividing the difference between the layer's minimum
# and maximum elevation values by the number of colors. This approach creates a smooth gradient effect across the
# elevation spectrum, enhanced by the chosen color palette. Once the shader is created, it is assigned a name and
# applied to the specified layer.
# Close all layers that may be open
layers_are_open = gm.GetLoadedLayerList()
if layers_are_open:
arr_ptr, arr_size = layers_are_open
initial_layers = gm.GM_LayerHandle_array_frompointer(arr_ptr)
for i in range(arr_size):
gm.CloseLayer(initial_layers[i])
err_LoadLayerList, array_pointer, array_size_param = gm.LoadLayerList(
os.path.join(import_dir, "Mountain_DEM.dem"),
gm.GM_LoadFlags_UseDefaultLoadOpts,
)
# Get handle for the loaded layers
arr_ptr, arr_size = gm.GetLoadedLayerList()
layers_array = gm.GM_LayerHandle_array_frompointer(arr_ptr)
layers_list = gm.carray_to_list(layers_array, arr_size)
### START SAMPLE
custom_shader = gm.GM_CustomShader_t()
custom_shader.mShaderName = "Custom RGB Shader"
# For this sample, 10 rgb color values are used to form a gradient
num_colors = 10
# Get the min/max elevation values
# We will assign colors at elevation intervals (height_incr)
layer_info = gm.GetLayerInfo(layers_list[0])
height_incr = (layer_info.mMaxElevation - layer_info.mMinElevation) / num_colors
color_array = gm.GM_ElevColor_array(num_colors)
rgb_vals = [
[41, 47, 86],
[79, 59, 107],
[120, 70, 121],
[160, 81, 127],
[197, 96, 126],
[227, 117, 120],
[249, 144, 110],
[255, 177, 102],
[255, 213, 100],
[247, 250, 112],
]
# Assign rgb colors and elevation increments to the color array
elevation = layer_info.mMinElevation
for i in range(num_colors):
elev_color = gm.GM_ElevColor_t()
elevation += height_incr
elev_color.mElev = elevation
elev_color.mColor = gm.toRGB(rgb_vals[i][0], rgb_vals[i][1], rgb_vals[i][2])
color_array[i] = elev_color
# Finish creating the shader
custom_shader.mNumElevs = num_colors
custom_shader.mElevColorList = color_array
err_AddCustomShader = gm.AddCustomShader(custom_shader, 0)
# Grabs the original elevation overrides for a given layer (used to reset any changes)
err_GetElevationOverrides, orig_overrides = gm.GetElevationOverrides(layers_list[0])
overrides = gm.GM_ElevationOverrides_t()
overrides.mLayerShader = "Custom RGB Shader"
# Attach the custom shader to a given elevation layer
err_SetElevationOverrides1 = gm.SetElevationOverrides(layers_list[0], overrides)
# Resets the shader changes made in this sample
# To see shader changes, comment the following 2 lines
err_SetElevationOverrides2 = gm.SetElevationOverrides(layers_list[0], orig_overrides)
err_RemoveCustomShader = gm.RemoveCustomShader("Custom RGB Shader")
### END SAMPLE
assert (
gm.GM_Error_None == err_LoadLayerList
), f"LoadLayerList: Expected {gm.GM_Error_None}, got {err_LoadLayerList}"
assert (
gm.GM_Error_None == err_AddCustomShader
), f"AddCustomShader: Expected {gm.GM_Error_None}, got {err_AddCustomShader}"
assert (
gm.GM_Error_None == err_GetElevationOverrides
), f"GetElevationOverrides: Expected {gm.GM_Error_None}, got {err_GetElevationOverrides}"
assert (
gm.GM_Error_None == err_SetElevationOverrides1
), f"SetElevationOverrides1: Expected {gm.GM_Error_None}, got {err_SetElevationOverrides1}"
assert (
gm.GM_Error_None == err_SetElevationOverrides2
), f"SetElevationOverrides2: Expected {gm.GM_Error_None}, got {err_SetElevationOverrides2}"
assert (
gm.GM_Error_None == err_RemoveCustomShader
), f"RemoveCustomShader: Expected {gm.GM_Error_None}, got {err_RemoveCustomShader}"
assert os.path.isdir(import_dir), f"Import directory doesn't exist: {import_dir}"
assert os.path.exists(
os.path.join(import_dir, "Mountain_DEM.dem")
), "Sample data doesn't exist"