4

I'm trying to understand the closest_point function in qgis. For that, I created a polygon layer with two features, and through the Geometry Generator I intend to find the closest point between those two.

This is the expression I'm using, but it is not working:

closest_point(geometry (get_feature_by_id('closest', 1)), geometry(get_feature_by_id ('closest', 2)))

Can someone help me to find out why?

enter image description here

2
  • How is it not working? Please explain what you expect and what happens instead. Mar 20, 2021 at 16:35
  • Well, I'm really a beginner in this but I guess it was supposed to draw the point in geometry one closest to geometry two, isn't that right? And that doesn't happen.
    – Felippe M.
    Mar 20, 2021 at 16:50

1 Answer 1

4

Your expression should work well, but you have geometry errors in your layer: you have multipart geometries with just one part. That's what the Check Geometries tool found.

To solve the problem: run Menu Vector / Geometry tools / Multipart to singleparts - in the resulting layer, both your original as well as the following expression should work:

closest_point(
    geometry (get_feature_by_id (@layer, 1)), 
    geometry (get_feature_by_id (@layer, 2))
)

@layer refers to the current layer.

enter image description here

4
  • I'm really really sorry. I shared it wrongly. This is the right link: drive.google.com/drive/folders/…
    – Felippe M.
    Mar 20, 2021 at 17:26
  • How did you identify that it was a multipart? I can only say a feature is a multipart when you try to select it and and both features are selected.
    – Felippe M.
    Mar 20, 2021 at 17:44
  • 1
    I used the Check Geometries tool. It was the inverse: you had two separate features, but somehow QGIS consiedered them to be multipart, but they consisted both of just one part, thus it was inconsistent.
    – Babel
    Mar 20, 2021 at 17:46
  • Go figure! Well thank you very much, it worked perfectly!!
    – Felippe M.
    Mar 20, 2021 at 17:48

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.