2

I need to add point coordinates into table ("xcoor" field and "ycoor" field). I knew this could be done using the Field Calculator.

First I created a new field "xcoor" and used expression $x to get x coordinate. I set field type as decimal number (real). I did the same for the "ycoor".

The table looked like this:

enter image description here

For existing point "xcoor" and "ycoor" field were updated.

enter image description here

But when I tried to insert new point feature I didn't get it's coordinates in the table. Both fields were empty for that feature.

Then I changed field type like this creating new field but virtual with type double:

enter image description here

enter image description here

And now, when I add new point it's coordinates are automatically inserted into table. But I can't understand why is this possible using virtual field and why double data type is offered just in this case? I hope someone can explain this to me.

1
  • If the field isn't populated on insert, it doesn't get populated, and if the column doesn't exist, then it's calculated every time.
    – Vince
    Jan 28, 2020 at 0:10

1 Answer 1

8

The field calculator is used for a one-time calculation. Whenever you add features, the new records will not be populated with the new coordinates.

The proper way to do that is in the layers' properties by configuring the "Attributes Form". For instance, in my example, I have created a shapefile with 3 fields; "ID", "XCoord" and "YCoord".

enter image description here

Double click on the layer's name to open the layer property dialog box then select "Attribute Form".

enter image description here

Make sure "Autogenerate" is selected, then click on the field you want to have the values generated ("XCoord" field in this case).

enter image description here

Click on the calculator button to have the "Expression Dialog" open. Then select "Geometry" ---> scroll down until you find $x and then double click on. Click OK.

enter image description here

Repeat the same operation for "Ycoord" and select $y.

enter image description here

When creating new geometries, the coordinates are generated and added in the respective fields automatically.

enter image description here

B

This is the method I would use; I hope this would workout for you as well.

1
  • Thas is exactly what I want. Thank you!
    – DrJacoby
    Jan 28, 2020 at 7:12

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.