3

Is there a simple way (without writing script or Python skills) to auto-calculate and auto-populate "lat" and "lon" attributes in QGIS?

When I manually add new points or move existing points in my layer, I'd like the "lat" and "lon" attributes to be captured and the attribute table populated/updated as I complete the task.

Right now, I create or modify the points and then update the attribute table with $y and $x in the Field Calculator.

1
  • 3
    use a virtual field or an auto-updating default field value
    – csk
    Commented Sep 11, 2019 at 2:37

3 Answers 3

2

You have a couple of options.

Option 1 - Virtual Field

The easiest method - which based on your question, seems likely to satisfy - is to simply add virtual fields "lat" and "lon" via the Field Calculator, with the following expressions:

  • x(@geometry) (you can use $x, but this is deprecated)
  • y(@geometry)

enter image description here

Option 2 - Default Values

The other option is to set default values, and have them auto-update. If this is the layer creation dialog:

enter image description here

Then under Layer Properties -> Attributes Form, click on "lat" and set the defaults to x(@geometry). Similar for "lon" - y(@geometry). Also set the Apply default value on update checkbox

enter image description here

Then when you create a point, the attributes will populate automatically:

enter image description here

And if you move the point, they will update:

enter image description here

0

Layer properties, Custom forms, select the field of the coordinate(s), Default value, $x, $y (or the way you intend them to be presented)

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 18 at 15:44
0

@Tom-Brennan describes exactly what you have to do, if your data is already in WGS84. But for a general solution, you must first transform the geometry object to WGS84:

lon = x(transform(@geometry, @layer_crs , 'EPSG:4326'))

lat = y(transform(@geometry, @layer_crs , 'EPSG:4326'))

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.