Display Options/Drawing Samples¶
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.
# 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,
)
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.
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)
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.
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")