This class represents a coordinate reference system (CRS).
Coordinate reference system object defines a specific map projection, as well as transformations between different coordinate reference systems. There are various ways how a CRS can be defined: using well-known text (WKT), PROJ string or combination of authority and code (e.g. EPSG:4326). QGIS comes with its internal database of coordinate reference systems (stored in SQLite) that allows lookups of CRS and seamless conversions between the various definitions.
Most commonly one comes across two types of coordinate systems:
- Geographic coordinate systems: based on a geodetic datum, normally with coordinates being latitude/longitude in degrees. The most common one is World Geodetic System 84 (WGS84).
- Projected coordinate systems: based on a geodetic datum with coordinates projected to a plane, typically using meters or feet as units. Common projected coordinate systems are Universal Transverse Mercator or Albers Equal Area.
Internally QGIS uses proj library for all the math behind coordinate transformations, so in case of any troubles with projections it is best to examine the PROJ representation within the object, as that is the representation that will be ultimately used.
Methods that allow inspection of CRS instances include isValid(), authid(), description(), toWkt(), toProj(), mapUnits() and others. Creation of CRS instances is further described in Object Construction and Copying section below. Transformations between coordinate reference systems are done using QgsCoordinateTransform class.
For example, the following code will create and inspect "British national grid" CRS:
if crs.isValid():
print("CRS Description: {}".format(crs.description()))
print("CRS PROJ text: {}".format(crs.toProj()))
else:
print("Invalid CRS!")
This class represents a coordinate reference system (CRS).
This will produce the following output:
CRS Description: OSGB 1936 / British National Grid
CRS PROJ text: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 [output trimmed]
CRS Definition Formats
This section gives an overview of various supported CRS definition formats:
Authority and Code: Also referred to as OGC WMS format within QGIS as they have been widely used in OGC standards. These are encoded as <auth>:<code>
, for example EPSG:4326
refers to WGS84 system. EPSG is the most commonly used authority that covers a wide range of coordinate systems around the world.
An extended variant of this format is OGC URN. Syntax of URN for CRS definition is urn:ogc:def:crs:<auth>:[<version>]:<code>
. This class can also parse URNs (versions are currently ignored). For example, WGS84 may be encoded as urn:ogc:def:crs:OGC:1.3:CRS84
.
QGIS adds support for "USER" authority that refers to IDs used internally in QGIS. This variant is best avoided or used with caution as the IDs are not permanent and they refer to different CRS on different machines or user profiles.
- See also
- authid()
-
createFromOgcWmsCrs()
PROJ string: This is a string consisting of a series of key/value pairs in the following format: +param1=value1 +param2=value2 [...]
. This is the format natively used by the underlying proj library. For example, the definition of WGS84 looks like this:
+proj=longlat +datum=WGS84 +no_defs
- See also
- toProj()
-
createFromProj()
Well-known text (WKT): Defined by Open Geospatial Consortium (OGC), this is another common format to define CRS. For WGS84 the OGC WKT definition is the following:
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]]
- See also
- toWkt()
-
createFromWkt()
CRS Database and Custom CRS
The database of CRS shipped with QGIS is stored in a SQLite database (see QgsApplication::srsDatabaseFilePath()) and it is based on the data files maintained by GDAL project (a variety of .csv and .wkt files).
Sometimes it happens that users need to use a CRS definition that is not well known or that has been only created with a specific purpose (and thus its definition is not available in our database of CRS). Whenever a new CRS definition is seen, it will be added to the local database (in user's home directory, see QgsApplication::qgisUserDatabaseFilePath()). QGIS also features a GUI for management of local custom CRS definitions.
There are therefore two databases: one for shipped CRS definitions and one for custom CRS definitions. Custom CRS have internal IDs (accessible with srsid()) greater or equal to USER_CRS_START_ID. The local CRS databases should never be accessed directly with SQLite functions, instead you should use QgsCoordinateReferenceSystem API for CRS lookups and for managements of custom CRS.
Validation
In some cases (most prominently when loading a map layer), QGIS will try to ensure that the given map layer CRS is valid using validate() call. If not, a custom validation function will be called - such function may for example show a GUI for manual CRS selection. The validation function is configured using setCustomCrsValidation(). If validation fails or no validation function is set, the default CRS is assigned (WGS84). QGIS application registers its validation function that will act according to user's settings (either show CRS selector dialog or use project/custom CRS).
Object Construction and Copying
The easiest way of creating CRS instances is to use QgsCoordinateReferenceSystem(const QString&) constructor that automatically recognizes definition format from the given string.
Creation of CRS object involves some queries in a local SQLite database, which may be potentially expensive. Consequently, CRS creation methods use an internal cache to avoid unnecessary database lookups. If the CRS database is modified, then it is necessary to call invalidateCache() to ensure that outdated records are not being returned from the cache.
Since QGIS 2.16 QgsCoordinateReferenceSystem objects are implicitly shared.
Caveats
There are two different flavors of WKT: one is defined by OGC, the other is the standard used by ESRI. They look very similar, but they are not the same. QGIS is able to consume both flavors.
- See also
- QgsCoordinateTransform
Definition at line 211 of file qgscoordinatereferencesystem.h.