Creating and Setting Projections¶
The functions SetProjection and SetProjectionEx use a GM_Projection_t object to set the projection that Global Mapper will use for drawing and exporting layers. A projection, in simple terms, is a mathematical conversion from a 3D globe to a 2D surface that allows us to make accurate maps of the Earth’s surface. Depending on your data, you may need to set a new projection and define its bounds.
Note
Changing the global projection in this way will not change the projection of individual layers; it will only affect operations which require a projection to interpret the data. For example, drawing or exporting the layer(s) may give distorted-looking results, and some elements of the metadata may be recalculated to fit the new projection. If you don’t use SetProjection, the global projection is set automatically based on the projection of the layers you load.
The GM_Projection_t object type that is used as a parameter for SetProjection(Ex) contains everything needed to define a projection. It consists of five variables, one of which is a list containing instances of the type GM_ProjAttrValue_t. Below is an example of how to set up a GM_Projection_t and set it as the current projection for GM.
EXAMPLE 1 - Set projection to UTM Zone 15:
import globalmapper as gm
utm_proj = gm.GM_Projection_t()
utm_proj.mProjSys
utm_proj.mProjSys = gm.GM_PRJ_UTM
utm_proj.mDatum = gm.GM_DATUM_WGS_84
utm_proj.mUnit = gm.GM_PRJ_UNIT_METERS
utm_proj.mAttrList[0].mAttr = gm.ZONE
utm_proj.mAttrList[0].mValue = 15
utm_proj.numAttrs = 1
gm.SetProjection(utm_proj)
Notice that most of the variables are set with Global Mapper constants (denoted by capital letters); many common datums, projection systems, and measurement units have been defined as constants in the Global Mapper module so that you can assign variables which depend on them more easily. For the complete list of constants available for setting up a projection, see DATUM, PROJATTR, PROJSYS, and UNIT in the “Types for Measurement Functions and Units” page of Constants.
EXAMPLE 2 - Set projection to Geographic (unprojected lat/lon):
import globalmapper as gm
geographic_cs = gm.GM_Projection_t()
geographic_cs.mProjSys = gm.GM_PRJ_GEO
geographic_cs.mDatum = gm.GM_DATUM_WGS_84
geographic_cs.mUnit = gm.GM_PRJ_UNIT_ARC_DEGREES
gm.SetProjection(geographic_cs)
For the example above, “projection” is a bit of a misnomer; representing data with a geographic coordinate system means it will be relative to the real-world 3D latitude/longitude of the globe, rather than with a projection to a flat surface. Any geographic coordinate system will be defined with angular units (degrees, radians) instead of the linear units (meters, miles, etc.) used by projected coordinate systems. Even though the function is called “SetProjection”, Global Mapper will still accept a geographic system.
EXAMPLE 3 - Set projection to UTM with Best Zone Automatically Selected from Bounding Box:
import globalmapper as gm
latlon_bounds_rect = gm.GM_Rectangle_t()
latlon_bounds_rect.mMinX = -90.0
latlon_bounds_rect.mMaxX = latlon_bounds_rect.mMinX + 0.1
latlon_bounds_rect.mMinY = 35.0
latlon_bounds_rect.mMaxY = latlon_bounds_rect.mMinY + 0.1
utm_proj = gm.GM_Projection_t()
utm_proj.mProjSys = gm.GM_PRJ_UTM
utm_proj.mDatum = gm.GM_DATUM_WGS_84
utm_proj.mUnit = gm.GM_PRJ_UNIT_METERS
utm_proj.numAttrs = 0
gm.SetProjectionEx(utm_proj, latlon_bounds_rect, 0)
Example 3 is similar to example 1 in setting up a UTM projection, except that no UTM zone is chosen. If a zoned projection system is used without a ZONE attribute or the ZONE is set to 0, Global Mapper will automatically use the zone that fits the area best based on the bounding rectangle provided or, if none is provided, the last drawn rectangle.
EXAMPLE 4 - Set projection to one loaded from a file:
import globalmapper as gm
err, utm_proj = gm.LoadProjectionFile("UTM 19N.prj")
gm.SetProjection(utm_proj)
The simplest way to set a projection is to use one that’s already created: if you have access to a projection file, you can use LoadProjectionFile to parse it into a GM_Projection_t with all its settings already filled out. Then it can be used to set the projection like normal.