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?

Geographos
  • 4,087
  • 2
  • 27
  • 88
  • 2
    Please define what you mean by "capturing" and what you're doing with the coordinates afterwards. – Erik Sep 04 '20 at 08: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. – Geographos Sep 04 '20 at 08:47

2 Answers2

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.

Erik
  • 16,269
  • 1
  • 24
  • 43
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

Taras
  • 32,823
  • 4
  • 66
  • 137
  • 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? – Geographos Sep 18 '20 at 07:47
  • Simply create a new a new text(string) field and use those expressions there – Taras Sep 18 '20 at 07:58
  • Hi,

    Is that the way that I should use?

    https://imgur.com/gallery/fdp1atX

    It doesn't provide any result. Where should I roughly place this formula?

    – Geographos Sep 18 '20 at 08:41
  • OK, now sorted. Could you tell me how to use your expression for the decimal type? – Geographos Sep 18 '20 at 10:41
  • What do you mean by "the decimal type"? – Taras Sep 18 '20 at 14:38
  • instead of "text string" – Geographos Sep 18 '20 at 15:21
  • Can you please explain to me how would you like to have that decimal type, how will it look like? – Taras Sep 19 '20 at 15:43
  • 1
    I think, that I already solved my problem, as I can run the centroid expression, which makes a perfect shortcut.

    https://gis.stackexchange.com/questions/374849/merging-attributes-of-selected-features-not-working-in-qgis-3

    Thank you very much for the help. I will definitely take into account your option when working with geoJSON or sth.

    – Geographos Sep 24 '20 at 09:38