Opening and Closing a Layer

This code shows a simple way to load a layer, get information about it, and then close it using Python scripting for Global Mapper. The variable “file_path” is a string providing a path to a file which should contain data for one or more layers (eg. a .shp or .gmw file). The three values returned by LoadLayerList are the error code of the operation (which isn’t used here but can be helpful to verify a successful function call), a pointer to the start of an array of the layers, and the size of that array. The layers will be represented as Python ints (pointers), which will be interpreted as a GM_LayerHandle_t32 by Global Mapper functions. For example, GetLayerInfo and CloseLayer both use these ints as inputs to work with the loaded layers.

import globalmapper as gm

err, array_ptr, array_size = gm.LoadLayerList(file_path, gm.GM_LoadFlags_UseDefaultLoadOpts)
loaded_array = gm.GM_LayerHandle_array_frompointer(array_ptr)
print("Layers opened: %d" % array_size)

for i in range(array_size):
    info = gm.GetLayerInfo(loaded_array[i])
    print("Layer %d, 0x%x:" % (i, loaded_array[i]))
    print("    Description: %s" % info.mDescription)
    print("    Type: %s" % info.mTypeName)
    # there are many other properties of 'info' that can also be retrieved this way - see documentation for GM_LayerInfo_t.

for i in range(array_size):
    err = gm.CloseLayer(loaded_array[i])
    if err == 0:
        print("Closed layer %d successfully" % i)

When working with the type GM_LayerHandle_t32, you may notice that that you can’t actually create an instance of it yourself, or even call ‘help’ on it. This is because GM_LayerHandle_t32 and all other _t32 types are functionally just ints. Refer to the page about :doc:`Global Mapper integer types <globalmapper.DemoInts>` for more information about how to interpret types like GM_LayerHandle_t32. Note, however, that the variables *array_ptr and loaded_array in the above example are pointers to arrays, and are their own types, rather than just integers.

If you are working with layers that are already opened, you can retrieve the layer handles using GetLoadedLayerList. Unlike many other functions, it does not return an error code, and instead just returns the array pointer and size. The next code example shows a simple way to confirm that GetLoadedLayerList has successfully found layers before trying to operate on the array it returns.

import globalmapper as gm

result = gm.GetLoadedLayerList()
if not result:
    # no layers are open...
else:
    list_ptr, list_size = result
    loaded_array = gm.GM_LayerHandle_array_frompointer(list_ptr)
    # do something with the layers...