C++-Style Arrays for GM Types in Python¶
Many functions in Global Mapper require array data structures for inputting data, rather than the typical Python list, to help keep the API for GM Python and the C++/C# SDK as similar as possible. To make use of arrays in Python, the Global Mapper module provides various array types that can be initialized to the desired size:
attribute_array = gm.GM_AttrValue_array(10)
Like in C++, these arrays are stored linearly in memory and have a fixed size, which is specified when they are created. For example, in the line above, a value of ‘10’ means that the array will be big enough to contain 10 GM_AttrValue_t objects.
After initializing the array, you can populate it in the same manner as with a standard python list, using the [] operator:
import globalmapper as gm
area_feature_list = [ ... ] # your list of GM_AreaFeature_t objects
num_features = len(area_feature_list)
area_feature_array = gm.GM_AreaFeature_array(num_features)
for i in range(num_features):
area_feature_array[i] = area_feature_list[i]
The above example demonstrates converting a Python list of area feature objects into a GM array, using the array type GM_AreaFeature_array. The size of the array is declared in the constructor; in this case, it uses the number of items in the list to allocate an array of the same size. Once the array is created, you can use the [ ] operator to index and modify the array in the same manner as a list.
Sometimes a function may return a pointer to an array, rather than an array itself. In those cases, you can use a special function in the form x_array_frompointer, substituting “x” for the appropriate object type; for example, the function GM_LayerHandle_array_from_pointer can be used on the pointer returned by GetLoadedLayerList to get an indexable array.
For more detail and examples about pointers in Global Mapper, see: Converting a Pointer to an Array or List.
Below are all the supported array types in Global Mapper Python scripting:
COLORREF_array
double_array
float_array
GM_AreaFeature_array
GM_AttrValue_array
GM_CustomShader_array
GM_ElevColor_array
GM_FoundFeature_array
GM_GroundControlPoint_array
GM_LayerHandle_array
GM_LidarAttrInfo_array
GM_LidarPoint_array
GM_LidarReturnInfo_array
GM_OnlineSourceInfo_array
GM_PaletteEntry_array
GM_Point_t_array
GM_ValName_array
uint8_array
uint32_array