With QGIS 3 you can use two simple expressions doing all the reprojection in one go without having to care about settings or projections of the layer. To get decimal degrees as float values use:
For x
to lon
:
x(transform($geometry, layer_property(@layer, 'crs'),'EPSG:4326'))
and for y
to lat
:
y(transform($geometry, layer_property(@layer, 'crs'),'EPSG:4326'))
You can use it for example in QGIS field calculator or anywhere else, where expressions are accepted. You can replace EPSG:4326
by the EPSG-Code of the CRS you wish to display coordinates in. EPSG:4326
is the code for WGS 84, so it will return latitude and longitude.
If you prefer the result to be a string value in degree, minutes and seconds instead of decimal degrees, you can add the to_dms()
function as follows:
to_dms(x(transform($geometry, layer_property(@layer, 'crs'),'EPSG:4326')),'x',4,'aligned')
and
to_dms(y(transform($geometry, layer_property(@layer, 'crs'),'EPSG:4326')),'y',4,'aligned')
Explanation:
transform()
function transforms every known CRS to the specified destination CRS (here EPSG:4326 WGS 84
). You can also type in every other known EPSG Code here instead. layer_property()
function automatically detects the source layers (@layer
) CRS using 'crs'
. (Since QGIS 3.18 you can also use @layer_crs
instead). x()
and y()
return the coordinates of the current $geometry
.
If the layer already is in the CRS you wish to display coordinates in, the accepted answer still is the shortest expression, by using $x
and $y
.