23

I'm trying to figure out how to calculate the lat/long of points using the QGIS field calculator.

Is there an easy way to do this?

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.

28

Sure can. 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

NOTE: This was added in 1.7.

| improve this answer | |
16

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

| improve this answer | |
  • 4
    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. – Maxim Dubinin Mar 31 '12 at 14:40
  • FYI "Export/Add Geometry Columns" can be found in menu > Vector > Geometry Tools > Export / Add Geometry Columns – clhenrick Feb 25 '16 at 18:18
8

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.

| improve this answer | |
6

In QGIS 3.8 (June 2019), there is now an algorithm to add the X and Y values. 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.

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

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

find polygon corner coordinates

| improve this answer | |
5

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.

| improve this answer | |
  • 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. – Brian Fisher Nov 15 '19 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 Oct 7 at 1:05
4

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.

| improve this answer | |
0

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

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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