39

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.

0

9 Answers 9

40

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.

0
36

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.

18

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".

2
  • 7
    Note 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. Commented Mar 31, 2012 at 14:40
  • 2
    FYI "Export/Add Geometry Columns" can be found in menu > Vector > Geometry Tools > Export / Add Geometry Columns
    – clhenrick
    Commented Feb 25, 2016 at 18:18
16

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.

enter image description here

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.

2
  • 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
    Commented Jan 25, 2020 at 16:50
  • 2
    In 3.10.3, I found it at: Processing > Toolbox > Vector table > Add X/Y fields to layer
    – Stu Smith
    Commented Mar 25, 2020 at 21:20
9

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.

6

Extract nodes. This makes it into a point layer. Then open attribute table, field calculate, Geometry-$y/$x

find polygon corner coordinates

6

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.

2
  • 1
    formulas from above links: longitude= x(transform($geometry, 'EPSG:FROM', 'EPSG:4326')) and latitude = y(transform($geometry, 'EPSG:FROM', 'EPSG:4326')) for WGS84 lat/long. Commented 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.
    – PolyGeo
    Commented Oct 7, 2020 at 1:05
3

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

0

For user like me, after 7+ years, there is a better and simple answer.

HERE > https://stackoverflow.com/a/51164270/9620153

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.