1

I'd like to know how to get nearest points of the layer "a" to another point layer "b", using fields calculator of QGIS. Layer "a" has points with 2 different id (1,2) and i want to get the closest points for each id. For example:
enter image description here

Any suggestions?

Mark
  • 965
  • 6
  • 12
  • Have you tried dissolving layer A based on the IDs and then running join attributes by nearest? – Erik Jun 14 '22 at 14:26

2 Answers2

1

On layer b, use this expression to get the closest point on layer a with the same id. Use Geometry Generator or Geometry by expression (see here for details):

with_variable (
    'id',
    id,
    eval (
    'overlay_nearest (
        ''a'', 
        $geometry,
        filter:= id = '  || 
        @id  || 
        ')[0]'
    )
)

Sollution using Geometry generator: layer b = red points; layer a = blue points; red triangle = closest point on layer b from points of layer b with same id: enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
0

Open the field calculator in the b layer and paste this expression: overlay_nearest('a_layer_id', $id)[0]. Replace a_layer_id with the id of the a layer.

Mayo
  • 3,902
  • 3
  • 22
  • This doesn't work: first argument in overlay_nearest must be a layer name. And: I'm not sure if that is what OP is looking for - if they look to create an id in the attribute table of if the geometry should be rendered. – Babel Jun 14 '22 at 14:57
  • You are right, reading the question again, the result of this expression is not what was requested. About the overlay_nearest function, the first argument can be a name or an ID. – Mayo Jun 14 '22 at 15:08