4

I have a QGIS project in which I work with a polygon layer ('POLIGONS_TEST') and a point layer ('PUNTS_TEST').

I use the following Geometry Generator expression on the 'POLIGONS_TEST' layer to draw a line that joins the centroid of each polygon with the nearest points layer 'PUNTS_TEST'.

make_line(
    centroid($geometry), 
    closest_point(
        overlay_nearest('PUNTS_TEST', $geometry,limit:=1)[0],
        $geometry
        )
    )

enter image description here

As you can see in the screenshot, the result is correct but there is but there is a visual inconsistency. The closest_point function returns the closest point from geometry1 to geometry2. It seems that it considers geometry1 as the polygon's boundary and not as the centroid.

How could I solve this so that the proximity is calculated concerning the centroid, without having to convert the polygon layer to a point layer?

Example: enter image description here

Unsatisfactory result after applying the expression proposed by the users @Erik and @Andrea: enter image description here

7
  • 1
    Not sure to understand... "the result is correct" or not? What is the meaning of the blu lines in the second screenshot? Commented Jul 2 at 8:39
  • 1
    The blue lines seem to be the expected results.
    – Erik
    Commented Jul 2 at 8:44
  • 1
    In the second screenshot there is also a grey line non flagged by a red X (which I suppose mark the unexpected results). What it means? Is it also an expected result? Commented Jul 2 at 8:45
  • I clarify this sentence. The result is correct with respect to the properties of the closest_point geometry function. What I am looking for is some solution to consider the centroid as the reference point and not the boundary. Regarding the screenshot, the gray lines are the result of the expression. The red lines is an indicator to say that it is not the expected result. And the blue lines are the expected result. Commented Jul 2 at 9:10
  • 1
    In the second screenshot there is also a grey line (maybe, two) not flagged by a red X. Is it an expected result or not? How do you check a point is closest to a centroid or not? Please provide the layers. Commented Jul 2 at 9:29

2 Answers 2

2

You are conducting your overlay check on the polygons, not their centroids. Try changing the expression to

make_line(centroid($geometry), 
    closest_point(overlay_nearest('PUNTS_TEST',
    centroid($geometry),limit:=1)[0],
    $geometry)
)
1
  • Erik, the result I get with the expression you propose is the same I get with my expression. I have to discard your solution. I am sorry. Commented Jul 2 at 9:14
2

The expression inside the overlay_nearest function is "an optional expression to evaluate on the features from the target layer". So the centroid function inside the other answers is evaluated on the point ('PUNTS_TEST') layer, not the polygon ('POLIGONS_TEST') layer, which is why it has no effect.

In this case you can avoid using the overlay_nearest function altogether by:

  • creating a variable (@centroid) to hold the centroid
  • using the aggregate function to create an array of points from the PUNTS_TEST layer
  • use the collect_geometries function to turn that into a multipoint geometry
  • find the closest point (closest_point) from the multipoint to the @centroid
  • finally making the line from that point to the @centroid using make_line
with_variable('centroid',centroid(@geometry),
    make_line(
        closest_point(
            collect_geometries(
                aggregate('PUNTS_TEST',
                aggregate:='array_agg',
                expression:=@geometry)
            )
        ,@centroid)
    ,@centroid)
)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.