I tried to do it using the Graphical modeler and QGIS algorithms only. It works well except for the attributes join. I can't figure out why it fails to join because when I create joins in the layer property, it works fine. Note my points are a regular 5m grid, but it would work with irregularly spaced points too as long as they are roughly in a grid pattern. Here's what the model, test points, and resulting table look like (point IDs labeled):
Here's the settings for each algorithm in order:
Distance matrix:
- The input and target layers are your points layer.
- Output type:
Linear
- Use only nearest:
9
target points (it should capture enough neighbors but if your samples are very irregular, you might have to increase this)
Field calculator (Input/Target line calc.):
You need two calculators, adding two fields which contain the line IDs for inputs and targets in the distance matrix
- Result field name:
input_line
& target_line
- field type:
int
- Formula:
attribute( get_feature( 'your_orig_points', 'fid', attribute( $currentfeature, 'InputID') ), 'line_id_field')
- Just replace
InputID
by TargetID
for the second formula
Extract by expression (next/previous line extract.):
Here, the idea was to isolate targets in the distance matrix that were on the previous and next lines. To keep expressions simpler, I did it separately.
- Expressions:
"target_line" = "input_line" + 1
& "target_line" = "input_line" - 1
Extract by expression (minimum distance calc.):
This step is where the closest point on the previous and next lines are isolated. The expression is the same for both instances.
- Expression:
"Distance" = minimum( "Distance",group_by:="InputID")
Join attributes by field value:
From this point, all the attributes are written somewhere so it's just a matter of joining them back to the original layer. I tried doing it within the model and export a Final
layer, but it wouldn't work, neither directly from the Processing toolbox. The table screenshotted above is after a join in the layer properties from the outputs of last two Extract by expression
.