Selecting a File to Open in GlobalMapper¶
If you wish to automate Global Mapper workflow using a Python script, you will most likely want to open a different file or files each time the script is run. There are multiple ways to do this using the the GM SDK; the simplest is to have the user type the filepath(s) as a parameter for the script, just as you might for any other Python script. However, if you expect to run the script within the Global Mapper application’s script editor tool, you will not be able to add any arguments when running the script as you could through the command line. In that case, you may want to use the SelectFile function, which opens a system file browser dialogue to let the user choose a file to load.
import globalmapper as gm
err_code, filepath = gm.SelectFile("", "", 0)
if not filepath:
# throw an exception
err_code, array_ptr, array_size = gm.LoadLayerList(filepath, gm.GM_LoadFlags_UseDefaultLoadOpts)
# do something with the new layer...
When calling SelectFile, be sure to account for users potentially closing the window without selecting a file. In this example, this is handled by checking that filepath has been initialized to something; you could also check for err_code == 18 (GM_Error_OperationCanceled). The file dialogue only allows users to pick one file at a time, so filepath will only ever contain one string.
The parameters of SelectFile function allow for some modification of the file browser dialogue. The first parameter accepts a Windows-style filter string to limit which file types will be shown in the window. If it is left blank, as in the code shown above, no filter will be used and it will show all files. As an example, to filter for only Lidar files, you could call the function like this:
gm.SelectFile("LiDAR files (*.laz, *.las)|*.laz;*.las", "", 0)
The second parameter sets the directory that the file dialogue will open to. If it is an empty string or None, then it will default to the current working directory. The last parameter is a handle to a window to use as the file selection dialogue, a HWND pointer. To open a new file browser window, pass 0 for this parameter.