Methods of Exporting

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.

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,
        )

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.

# 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,
        )

Vector Attributes to CSV

Note

Before running this sample, configure these variables and modules:

  • export_dir: the directory for resulting files to be exported to

  • import 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.

# 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_}")

Crop, Merge, and Reproject USGS DRGs to GeoTIFF/JPEG

Note

Before running this sample, configure these variables:

  • import_dir: the directory containing sample data

  • export_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,
)