How do I easily calculate the lat/long of points using the QGIS Field Calculator?
I saw a previous related question (Using QGIS API and Python, to return latitude and longitude of point?), but it deals with a more advanced approach.
In ArcGIS Desktop this is similar to the 'Calculate Geometry' function; I just can't find where do this in QGIS.
9 Answers 9
First, save the layer as WGS84 and import it again. Then in the field calculator you can use $x or $y as a variable to get the x and y.
x and y
This was added in 1.7.
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
.
-
This works also for Custom Projections! Use eg USER:100030 instead of EPSG:4326Hans Erren– Hans Erren2025年06月15日 20:30:20 +00:00Commented Jun 15 at 20:30
You have to reproject the point layer to EPSG:4326 first. Right-click on the layer name in layer list and select "Save as ...". There, you can specify the target CRS to be EPSG:4326.
Load the new reprojected point layer and then run "Export/Add Geometry Columns".
-
7Note that with newer version of QGIS (accessible via OSGeo4W installer) you don't even need to Export a layer to a new CRS. You only need to change CRS of the map canvas to whatever is you choice for output units and CRS and check "Calculate using Project CRS" when doing "Export/Add Geometry Columns" and output to the same shapefile.Maxim Dubinin– Maxim Dubinin2012年03月31日 14:40:54 +00:00Commented Mar 31, 2012 at 14:40
-
3FYI "Export/Add Geometry Columns" can be found in
menu > Vector > Geometry Tools > Export / Add Geometry Columns
clhenrick– clhenrick2016年02月25日 18:18:26 +00:00Commented Feb 25, 2016 at 18:18
Since QGIS 3.8 (June 2019), there is now an algorithm to add the X and Y values called "Add X/Y fields to layer".
It can reproject coordinates on the fly.
If you still need a Degree Minute or Degree Minute Second format, you need to use first the to_dm()
or to_dms()
expression to add these fields into the attribute table.
-
I'm sorry, etrimaille, I'm so lame I can't find how to find that screen ("Add X/Y Fields to Layer"). Can you please give me some guidance?TVZ– TVZ2020年01月25日 16:50:15 +00:00Commented Jan 25, 2020 at 16:50
-
2In 3.10.3, I found it at: Processing > Toolbox > Vector table > Add X/Y fields to layerStu Smith– Stu Smith2020年03月25日 21:20:13 +00:00Commented Mar 25, 2020 at 21:20
One option could be specifying the layer CRS as WGS 84(no projection). May be you will have to change the project CRS too. Now use "Vector> Geometry Tools> Export/Add Geometry Columns". This should give you Lat/long coordinates.
Extract nodes. This makes it into a point layer. Then open attribute table, field calculate, Geometry-$y/$x
The previous best answer can be considered obsolete.
Using the field calculator, you can get x, y coordinates from points layer in any projection and make the conversion to longitude latitude.
You can find the recipe, documented with formulas and an illustrated GIF.
-
1formulas from above links: longitude= x(transform($geometry, 'EPSG:FROM', 'EPSG:4326')) and latitude = y(transform($geometry, 'EPSG:FROM', 'EPSG:4326')) for WGS84 lat/long.Brian Fisher– Brian Fisher2019年11月15日 22:27:11 +00:00Commented Nov 15, 2019 at 22:27
-
Can you provide a link to "The previous best answer", please? If it’s obsolete it should probably be receiving downvotes so as to inform the community that it should no longer be seen as the best.2020年10月07日 01:05:39 +00:00Commented Oct 7, 2020 at 1:05
In the most recent version of QGIS, they added a feature called "Add X/Y Field to Layer" which (In this case you're wanting the lats/longs of a point layer) will add the latitude & longitude to your specified projection as x (longitude) & y (latitude) columns
For user like me, after 7+ years, there is a better and simple answer.
Explore related questions
See similar questions with these tags.