Global Mapper supports the export of many different file types. Exporting can involve saving spatial data, attribute tables, or visual maps in different formats. The choice of format depends on how the data will be used, such as for analysis, visualization, or web display which is possible due to Global Mapper’s extensive configuration options. If you are still unable to get the results you need, reference the RunScript function. It allows the SDK to run commands from the Global Mapper scripting language, which can include additional export configuration options that are not otherwise available to the SDK.
Convert DWG/DXF to a GeoTIFF Raster File
Note: Before running this sample, configure these variables:
export_dir: the directory for resulting files to be exported to
This example shows how to convert DWG and DXF vector data into a GeoTIFF raster file. It references any DWG and/or DXF files that are currently loaded and exports them as GeoTIFF files to the same directory. The width and height for the exported GeoTIFF are determined by the GlobalRect for the given DWG/DXF layer, ensuring a minimum resolution of 1920x1080.
import os
import globalmapper as gm
layer_data = "Z:/GM-20069/DWG_DXF_to_GeoTIFF_raster_sample_data/three_shapes.dwg"
export_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "../misc/DWG_DXF_to_GeoTIFF_raster_output/")
os.makedirs(export_dir, exist_ok=True)
# This example shows how to convert DWG and DXF vector data into a GeoTIFF raster file. It references any DWG and/or DXF
# files that are currently loaded and exports them as GeoTIFF files to the same directory. The width and height for the
# exported GeoTIFF are determined by the GlobalRect for the given DWG/DXF layer, ensuring a minimum resolution of 1920x1080.
err_LoadLayerList, array_pointer, array_size_param = gm.LoadLayerList(
layer_data, gm.GM_LoadFlags_HideAllPrompts
)
layer_pointer, layer_count = gm.GetLoadedLayerList()
layers = gm.GM_LayerHandle_array_frompointer(layer_pointer)
### START SAMPLE
vec_layers = []
# Find any dwg/dfx files that are currently open
for i in range(layer_count):
info = gm.GetLayerInfo(layers[i])
if info.mTypeName == "DWG" or info.mTypeName == "DXF":
vec_layers.append(layers[i])
if len(vec_layers) == 0:
print("None of the currently loaded are DWG or DXF files")
else:
print("Found", str(len(vec_layers)), "layers to export...")
for layer in vec_layers:
info = gm.GetLayerInfo(layer)
width_meters = info.mGlobalRect.mMaxX - info.mGlobalRect.mMinX
height_meters = info.mGlobalRect.mMaxY - info.mGlobalRect.mMinY
# Set the dimensions of the output file(s); minimum of 1920x1080
HD_WIDTH = 1920
HD_HEIGHT = 1080
dim_ratio = width_meters / height_meters
if width_meters > height_meters:
height_px = HD_HEIGHT
width_px = int(HD_HEIGHT * dim_ratio)
else:
width_px = HD_WIDTH
height_px = int(HD_WIDTH / dim_ratio)
# Converts the vector data to raster data and exports it
err_ExportRaster = gm.ExportRaster(
export_dir + "parcel.tif",
gm.GM_Export_GeoTIFF,
layer,
None,
width_px,
height_px,
gm.GM_ExportFlags_AddAlpha,
)
### END SAMPLE
assert (
gm.GM_Error_None == err_LoadLayerList
), f"LoadLayerList: Expected {gm.GM_Error_None}, got {err_LoadLayerList}"
if len(vec_layers) > 0: # Only assert ExportRaster if there was content to export
assert (
gm.GM_Error_None == err_ExportRaster
), f"ExportRaster: Expected {gm.GM_Error_None}, got {err_ExportRaster}"
if len(vec_layers) == 0:
assert False, "None of the currently loaded layers are DWG or DXF files"
assert os.path.isdir(export_dir), f"Error creating the export directory: {export_dir}"
# Expected path for file export
exported_filepath = os.path.join(export_dir, "parcel.tif")
# Assert that the final expected output file exists and is not empty
if len(vec_layers) > 0:
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}"
assert os.path.exists(layer_data), f"Sample data doesn't exist: {layer_data}"
Vector Layers to Multiple Shapefiles
Note: Before running this sample, configure these variables:
export_dir: the directory for resulting files to be exported to
This script exports any loaded vector layers into multiple shapefiles - one each for lines, points, and areas. This sample had a DWG data layer loaded to interact with and export shapefiles from.
import os
import globalmapper as gm
layer_data = "Z:/GM-20069/export_layers_to_mult_shapefiles_sample_data/three_shapes.dwg"
export_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "../misc/export_layers_to_mult_shapefiles_output/")
os.makedirs(export_dir, exist_ok=True)
# This script exports any loaded vector layers into multiple shapefiles - one each for lines, points, and areas. This sample
# had a DWG data layer loaded to interact with and export shapefiles from.
err_LoadLayerList, array_pointer, array_size_param = gm.LoadLayerList(
layer_data, gm.GM_LoadFlags_HideAllPrompts
)
arr_ptr, arr_size = gm.GetLoadedLayerList()
all_layers = gm.GM_LayerHandle_array_frompointer(arr_ptr)
### START SAMPLE
# Retrieve the currently loaded vector layers as an array
vector_layers = []
for layer in gm.carray_to_list(all_layers, arr_size):
if gm.GetLayerInfo(layer).mHasVectorData:
vector_layers.append(layer)
if len(vector_layers) == 0:
print("No vector layers were found, so nothing was exported.")
else:
# Export individually each of the layers' lines, points, and areas
print(f"Exporting files to {export_dir}")
for layer in vector_layers:
name = gm.GetLayerInfo(layer).mDescription
name_wo_ext = name[: name.index(".")] if "." in name else name
print("Exporting line features...")
err_ExportVector1 = gm.ExportVector(
export_dir + name_wo_ext + "_lines.shp",
gm.GM_Export_Shapefile,
layer,
None,
gm.GM_VectorExportFlags_HideProgress + gm.GM_VectorExportFlags_ExportLines,
0x0,
)
print("Exporting point features...")
err_ExportVector2 = gm.ExportVector(
export_dir + name_wo_ext + "_points.shp",
gm.GM_Export_Shapefile,
layer,
None,
gm.GM_VectorExportFlags_HideProgress + gm.GM_VectorExportFlags_ExportPoints,
0x0,
)
print("Exporting area features...")
err_ExportVector3 = gm.ExportVector(
export_dir + name_wo_ext + "_areas.shp",
gm.GM_Export_Shapefile,
layer,
None,
gm.GM_VectorExportFlags_HideProgress + gm.GM_VectorExportFlags_ExportAreas,
0x0,
)
### 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_ExportVector1
), f"ExportVector1: Expected {gm.GM_Error_None}, got {err_ExportVector1}"
assert (
gm.GM_Error_None == err_ExportVector2
), f"ExportVector2: Expected {gm.GM_Error_None}, got {err_ExportVector2}"
assert (
gm.GM_Error_None == err_ExportVector3
), f"ExportVector3: Expected {gm.GM_Error_None}, got {err_ExportVector3}"
assert os.path.isdir(export_dir), f"Error creating the export directory: {export_dir}"
if len(vector_layers) == 0:
assert False, "No vector layers were loaded"
assert os.path.exists(layer_data), f"Sample data doesn't exist: {layer_data}"
expected_filenames = [
"three_shapes_areas.shp",
"three_shapes_areas.shx",
"three_shapes_lines.shp",
"three_shapes_lines.shx",
"three_shapes_points.shp",
"three_shapes_points.shx",
]
# Make sure all expected files were exported
if len(vector_layers) > 0:
for name in expected_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}"
Vector Attributes to CSV
Note: Before running this sample, configure these variables and modules:
export_dir: the directory for resulting files to be exported toimport numpy as np
from sklearn.linear_model import LinearRegression
The following sample exports the attributes of a vector layer to a CSV file (a shapefile was used in this sample). Then, from the CSV file it uses the python libraries, Numpy and Scikitlearn to demonstrate a simple linear regression of the selected data.
import os
import globalmapper as gm
import numpy as np
from sklearn.linear_model import LinearRegression
layer_path = "Z:/GM-20069/vector_attributes_to_CSV_sample_data/CPA_Projects_and_Demographics_April_2023.shp"
export_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "../misc/vector_attributes_to_CSV_output/")
os.makedirs(export_dir, exist_ok=True)
# The following sample exports the attributes of a vector layer to a CSV file (a shapefile was used in this sample). Then,
# from the CSV file it uses the python libraries, Numpy and Scikitlearn to demonstrate a simple linear regression of the
# selected data.
# Closes 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, arr_ptr, arr_size = gm.LoadLayerList(
layer_path, gm.GM_LoadFlags_HideAllPrompts
)
layer_arr = gm.GM_LayerHandle_array_frompointer(arr_ptr)
### START SAMPLE
# Exports existing vector layer attribute data to a CSV filed
err_ExportVector = gm.ExportVector(
export_dir + "cap.csv",
gm.GM_Export_CSV,
gm.NULL,
None,
gm.GM_VectorExportFlags_ExportAttrs,
gm.NULL,
)
# Uses numpy to create arrays of selected columns
mean_award_array = np.genfromtxt(
export_dir + "cap.csv",
delimiter=",",
skip_header=True,
dtype=int,
usecols=(4),
)
population_array = np.genfromtxt(
export_dir + "cap.csv",
delimiter=",",
skip_header=True,
dtype=int,
usecols=(6),
)
# Assigns arrays to variables
x = population_array
y = mean_award_array
# Uses Scilearnkit for simple Linear Regression Model
model = LinearRegression()
# Fits the model, and reshapes a 1-D array to a 2-D array for Linear Regression
model.fit(x.reshape(-1, 1), y)
# Displays results to the user
r_sq = model.score(x.reshape(-1, 1), y)
print("Linear Regression Model")
print("Mean CPA Project Award with Population in Boston Neighborhoods\n")
print(f"Coefficient of Determination: {r_sq}")
print(f"Y-Intercept: {model.intercept_}")
print(f"Coefficients: {model.coef_}")
### 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_ExportVector
), f"ExportVector: Expected {gm.GM_Error_None}, got {err_ExportVector}"
assert os.path.isdir(export_dir), f"Error creating the export directory: {export_dir}"
assert os.path.exists(layer_path), f"Sample data doesn't exist: {layer_path}"
# Expected path for file export
exported_filepath = os.path.join(export_dir, "cap.csv")
# 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}"
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"