If I use the 'Add coordinates to point' it adds the coordinates to in the file, however splits the data into the amount of objects in the original file. If I try to add separate columns in the attribute file calling them 'Coordinate X' or 'Y', then using the $x
or $y
expression, the output is just NULL
. I need to coordinates but all in one file.
-
4Please state which CRS your using, which feature type you have and which field type your coordinate fields have when you try to create them.– ErikCommented Apr 29, 2020 at 10:54
-
2If you were using field calculator or created a new field in qgis make sure the length of the field is greater than 0– she_weedsCommented Apr 30, 2020 at 22:57
-
Please, do not forget about "What should I do when someone answers my question?"– Taras ♦Commented Mar 21, 2022 at 15:23
3 Answers
Use a built-in tool "Add X/Y fields to layer"
There you can choose a coordinate system in which you want your coordinates and add an optional field prefix.
Under assumption that you have some kind of a vector layer (such as GeoJSON or something similar) I provide an explanation on the example of a GeoJSON file that contains point features. You didn't provide information how did you calculate fields X and Y. You should use Field calculator. See hovered icon in the first figure.
First, right click on the vector layer and "Open attribute table" and it will open like this.
After you click on Field calculator icon another window will appear. I've put all parameters that you would need to create a new field with X coordinate (Note I named my field E -> easting instead of X). You can also edit existing attribute by ticking "Create new field" checkbox. This will enable the listbox on the right side of the form "Update existing field".
After you repeat the procedure for Y, you get attribute table with updated set of attributes as you can see in an image below.
Don't forget to save your edits. These edits will remain permanent in your file.
A possible solution using PyQGIS.
Let's assume there is a point layer called 'points' with its attribute table, see image below.
Proceed with Plugins > Python Console > Show Editor
(Ctrl+Alt+P) and paste the script below
# imports
from qgis.core import QgsProject, QgsField
from PyQt5.QtCore import QVariant
# getting point layer by its name
layer = QgsProject.instance().mapLayersByName('test')[0]
# accessing point layer's provider
layer_provider = layer.dataProvider()
# adding new fields
for attr in ["X_Coord", "Y_Coord"]:
layer_provider.addAttributes([QgsField(attr, QVariant.Double)])
layer.updateFields()
layer.startEditing()
# looping over each feature
for feature in layer.getFeatures():
# accessing layer fields
fields = layer.fields()
# accessing geometry as a point
geom_pt = feature.geometry().asPoint()
attrs = {
fields.indexFromName("X_Coord"): geom_pt[0],
fields.indexFromName("Y_Coord"): geom_pt[1]
}
# changing attribute records
layer_provider.changeAttributeValues({feature.id(): attrs})
layer.commitChanges()
Press Run script
and get the output that will look like
References: