The most simple expression nowadays is this one for x | lon
coordinate, if the layer is not already in the desired CRS:
x(transform($geometry, layer_property(@layer_name, 'crs'),'EPSG:4326'))
and
y(transform($geometry, layer_property(@layer_name, 'crs'),'EPSG:4326'))
for y | lat
coordinate. You can use it for example in QGIS field calculator or anywhere else, where expressions are accepted. Simply 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.
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 CRS by 'crs'
as well as the current layer name by @layer_name
. 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
.