QGIS – Graphical Model for Moving Random Points to Closest Line Point in QGIS

qgisqgis-expressionqgis-modelervector-layer

I want to place a regular random distribution points inside a polygon and move these points to the respective closest point on a line layer afterwards. I use regular points algorithm with random offset for the creation of the random points.

The goal is to create a graphical model for this process. I have the problem that I can't access the output of a previous part of the model inside an expression within the "Geometry by expression" tool, as described in this question. So for the moment I had to split it into two different models, which is not a large problem, but seems pretty "unelegant".

Here are images of the two parts of the model:

first part of model

second part of model

In the second part I use the approach described here to access an input layer inside an expression. (The productive case uses input layers without any ID attributes. This is why I add the "checkid" field needed inside the expression.)

With my Hamburg test case, the result after running the first part of the model looks like this:

result after running first part of model, background map: OpenStreetMap contributors

The second part looks like this (with hidden resulting layers from part 1):

result after running first part of model, background map: OpenStreetMap contributors

(background map: © OpenStreetMap contributors)

This is the current version of the expression inside "Geometry by expression" to move the random points to the next line (new_line_layer is the layer renamed using "Rename layer" algorithm):

make_point( 
  x(
    closest_point( 
      geometry(
        get_feature_by_id(
          'new_line_layer',
          array_to_string(
            overlay_nearest (
              'new_line_layer',
              "checkid"
            )
          )
        )
      ),
      $geometry
    )
  ),
  y(
    closest_point( 
      geometry(
        get_feature_by_id(
          'new_line_layer',
          array_to_string(
            overlay_nearest (
              'new_line_layer',
              "checkid"
            )
          )
        )
      ),
      $geometry
    )
  )
)

My question is now: Is there either a way to combine everything to one graphical model using my approach (or alternatively another approach leading to the same result)?

The model files and test data can be found here.

Best Answer

Solution

You don't even need to rename the clipped line layer to integrate both of your models in a single one. In the answer you linked to, the problem was with input layers. Here, we deal with layers as output of an algorithm. In the Model Builder, when you add an algorithm, QGIS automatically creates variables you can use in the expression to refer to the output of the algorithm.

So in your case, modify the expression of the Geometry by Expression algorithm: to reference the clipped line layer, don't use the text string 'new_line_layer', but rather use the variable @Add_check_ID_field_OUTPUT that QGIS provides - see first screenshot below.

Download model

I adapted the model accordingly and it works as you want, see screenshots. You can download the model here.

Screenshots

enter image description here

Output of the model: blue=test_points, red=network_test_points: enter image description here

Zoomed in: enter image description here

Related Question