3

I would like to ask about the possibility of capturing coordinates for more than 1 object in QGIS.

I used to use the coordinate capture plugin, but what is the problem there I have to collect coordinates one by one.

Is there any way to collect coordinates i.e. for all points from the given layer and export them to excel? Following their names or different features from data attribute table?

enter image description here

I will by more than happy if someone clarifies it for me.

UPDATE:

Following the answer below, I tried to run the $x expression, but it returns NULL.

enter image description here

What am I missing here?

2
  • 2
    Please define what you mean by "capturing" and what you're doing with the coordinates afterwards.
    – Erik
    Sep 4 '20 at 8:18
  • 1
    Capturing - implicitly - gathering the coordinates, as you do with coordinate capture plugin. I need them as a list in Excel. There are about 30-40 of them from each layer.
    – MKR
    Sep 4 '20 at 8:47
5

You may use the field calculator to add the coordinates of a feature to the attribute table. In cases like yours I usually create two fields, one for the X coordinate, and one for Y.

The expression is utterly simple:

$x

This writes the X coordinate in a column. The same goes for $y. Afterwards you can copy & paste the attribute table to excel.

You should choose the column type according to your CRS.

9
  • I suppose @Erik can refer to this thread Exporting x,y coordinates as latitude,longitude
    – Taras
    Sep 4 '20 at 9:01
  • Pay attention that when applying copy & paste the attribute table into Excel, it is not 100% sure whether Excel will maintain the type/format of the output data, I will suggest using one of solutions provided here Exporting attribute table to Excel from QGIS?.
    – Taras
    Sep 4 '20 at 9:05
  • if I type $x it returns NULL. What am I missing?
    – MKR
    Sep 10 '20 at 8:41
  • NULL can appear when you use $x or $y for polylines and polygons
    – Taras
    Sep 10 '20 at 9:16
  • 1
    @MKR since you're talking about "all points from the given layer" I assumed you had points. My solution doesn't work for lines/polygons.
    – Erik
    Sep 10 '20 at 13:22
1

You get NULL while using $x or $y because your geometry is probably either Polylines or Polygons.

So, I will suggest employing this expression to capture coordinates for a multitude of objects at once

map_to_json(
    map(
        'x', array_to_string(array_foreach(generate_series(1, num_points($geometry)), x(point_n($geometry, @element)))),
        'y', array_to_string(array_foreach(generate_series(1, num_points($geometry)), y(point_n($geometry, @element))))
     )
)

So, the result will look as following

{"x":"5395496.429117461,5395494.155999963,5395489.773418307","y":"5659901.713053187,5659897.333999608,5659899.601836967"}
{"x":"5396759.156386755,5396761.318999965,5396764.051999965,5396762.000375618,5396760.367999964,5396755.841312531","y":"5660769.610787445,5660770.32399961,5660769.08599961,5660764.5589490365,5660760.95699961,5660763.009167434"}
{"x":"5397230.945016465,5397230.575999966,5397225.642692687","y":"5660956.466597664,5660951.539999611,5660951.8752865605"}
...

There is also a possibility of using this expression with a different formatting

array_to_string(
               array_foreach(generate_series(1, num_points($geometry)),
                             'Point_'  || @element || ': (' || x(point_n($geometry, @element))|| ', ' || y(point_n($geometry, @element)) || ')'
                )
)

That will lead to this

Point_1: (5395496.429117461, 5659901.713053187),Point_2: (5395494.155999963, 5659897.333999608),Point_3: (5395489.773418307, 5659899.601836967)
Point_1: (5396759.156386755, 5660769.610787445),Point_2: (5396761.318999965, 5660770.32399961),Point_3: (5396764.051999965, 5660769.08599961),Point_4: (5396762.000375618, 5660764.5589490365),Point_5: (5396760.367999964, 5660760.95699961),Point_6: (5396755.841312531, 5660763.009167434)
Point_1: (5397230.945016465, 5660956.466597664),Point_2: (5397230.575999966, 5660951.539999611),Point_3: (5397225.642692687, 5660951.8752865605)
...

Both cases are approachable in the "Field Calculator" via creating a new text(string) field and using those expressions in Expression Dialog.

result

Note: it shall do the work for all geometry types

8
  • The expression works, but how to use it really. I plotted it into my data attribute table, where is the "e" - expression symbol and I saw your result at the very bottom like you wrote (points with coordinates) but I couldn't plot them into my data attribute table column. Is there any way to do this?
    – MKR
    Sep 18 '20 at 7:47
  • Simply create a new a new text(string) field and use those expressions there
    – Taras
    Sep 18 '20 at 7:58
  • Hi, Is that the way that I should use? imgur.com/gallery/fdp1atX It doesn't provide any result. Where should I roughly place this formula?
    – MKR
    Sep 18 '20 at 8:41
  • OK, now sorted. Could you tell me how to use your expression for the decimal type?
    – MKR
    Sep 18 '20 at 10:41
  • 1
    I think, that I already solved my problem, as I can run the centroid expression, which makes a perfect shortcut. gis.stackexchange.com/questions/374849/… Thank you very much for the help. I will definitely take into account your option when working with geoJSON or sth.
    – MKR
    Sep 24 '20 at 9:38

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.