40

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.

Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Mar 13, 2011 at 18:45
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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Mar 13, 2011 at 21:41
0
40

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.

answered Sep 25, 2020 at 13:27
1
  • This works also for Custom Projections! Use eg USER:100030 instead of EPSG:4326 Commented Jun 15 at 20:30
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".

answered Mar 30, 2012 at 11:49
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
  • 3
    FYI "Export/Add Geometry Columns" can be found in menu > Vector > Geometry Tools > Export / Add Geometry Columns Commented Feb 25, 2016 at 18:18
17

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.

Taras
35.8k5 gold badges77 silver badges151 bronze badges
answered Apr 11, 2019 at 11:23
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? 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 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.

answered Mar 30, 2012 at 11:09
6

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

find polygon corner coordinates

answered Apr 17, 2013 at 18:50
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.

answered Jul 4, 2018 at 0:25
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. 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

answered Feb 4, 2021 at 6:25
0

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

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

answered Jan 8, 2019 at 21:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.