Transforms various R data and object types so that they can be processed by QGIS' QgsCoordinateReferenceSystem class.

qgisCRS(crs, proj4 = FALSE)

Arguments

crs

a primitive or object that either specifies (or contains a reference to) a coordinate system

proj4

logical: if set to TRUE, stops this function from avoiding proj4 strings in favour of well-known text (WKT) specifications, which is the default behaviour

Value

A numeric or character string.

See also

iface.addVectorLayer()

Examples

# numerics are returned as they are qgisCRS(4326)
#> [1] 4326
# QGIS understands EPSG, ESRI and PostGIS ids, but this function does not run # any checks on whether the numerics passed are in fact valid. as a consequence, # QGIS will still fail to parse the CRS if you pass it nonsense such as this qgisCRS(12345.6)
#> [1] 12345.6
# numeric ids will automatically be extracted from instances of sp's CRS class (or any # of the Spatial* dataframes that include a CRS spec) spCRS <- sp::CRS('+init=epsg:4326') qgisCRS(spCRS)
#> [1] 4326
spCRS <- sp::CRS('+init=esri:54009') qgisCRS(spCRS)
#> [1] 54009
# when a CRS object's EPSG id can't be found, the function returns a well-known text (WKT) spec # by default mollweidestring <- '+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs' mollweide <- sp::CRS(mollweidestring) qgisCRS(mollweide)
#> [1] "PROJCS[\"Mollweide\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Mollweide\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]"
# automatic conversion from proj4 to WKT can be suppressed using the 'proj4' argument qgisCRS(mollweide, proj4 = TRUE)
#> [1] "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
# behaviour is identical when passing proj4 specs as character strings directly qgisCRS(mollweidestring)
#> [1] "PROJCS[\"Mollweide\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Mollweide\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]"
qgisCRS(mollweidestring, proj4 = TRUE)
#> [1] "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
# other string representations are left untouched for QGIS to deal with # it is the user's responsibility to ensure their format is understood by QGIS qgisCRS('EPSG:4326') # QGIS will understand this
#> [1] "EPSG:4326"
qgisCRS('FOO') # but not this
#> [1] "FOO"