2

I'm using QGIS 3.16.

I have to get coordinates from a line-layer (layer_1) and write them to a point-layer (layer_2). I cannot create the coordinates in layer_1 because i am not allowed to create any fields there.

I tried this:

attribute (get_feature ('layer_1', 'layer1_field_1', 'layer_2_field2' ), $x_at(0))

but I get 'NULL' as result, while querying the fields of layer_1 works just fine.

Am I using the attribute expression wrong? Does $x_at() not work in this context? Or should I use another expression?

Edit: I have to find a solution for several users who only have basic knowledge of GIS, this is why I am looking for a automated solution. I need x, y and z coordinates of the start- and end-vertices.

Edit 2: Usually, I would add fields to the line-layer and get the coordinates by using $x_at(0), $y_at(0), etc. and then transfer the coordinates using a variant of the code I mentioned above. That works well. In my special case, I cannot create additional fields in the line-layer and have to query the coordinates from the point-layer.

Edit3: Still trying to make my problem understandable:

This is the code I use (with adaptations to the end-position of the line-feature) to label the lines with their start- and end-coordinates without creating new fields in the respective line-layer:

code for coordinates

lines with labels

Now when I try to query the same information (e.g. $x_at(0)) from a different layer or by simply using the field-calculator with the expression attribute (get_feature ('layer_1', 'layer1_field_1', 'layer_2_field2' ), $x_at(0)) mentioned above, I get NULL as a result. Is it possible to get the $x_at() expression to work this way? Or do I have to use another way altogether?

The example with the point-layer above was a (likely irritating) attempt to explain, but the solution should preferably work with the field-calculator, so it should be usable with different geometries.

docr
  • 21
  • 4

1 Answers1

1

Lets say you have a point layer and want to get the coordinates of the nearest point on the line layer.

Use this expression for x-coordinate and replace x by y for y-coordinate:

x (
    closest_point (
        overlay_nearest (
            'line',
            $geometry
        )[0],
        $geometry
    )
)

Point layer with blue points and closest point on line layer (red), generated based on the expression. Blue points are labeled with the coordinates of the red points:

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • Thank you! This works as a workaroundand I will try it with my colleagues. But I would still like to know if this can be solved using an expression, as it would make the workflow less prone to operator errors. – docr Jul 05 '22 at 06:15
  • I don't understand: my solution does use an expression... What do you mean by "using an expression"? Please edit your question and add more details, like this, it's impossible to guess what you're really looking for. Best would be adding a screenshot + more details. – Babel Jul 05 '22 at 06:32
  • I'm sorry, that was a bad use of words from me. I was trying to refer to my question regarding the use of the attribute function. I will edit my question and add screenshots. – docr Jul 05 '22 at 06:38