3

I've got a point shapefile which has a single attribute (elevation Z) and there are a lot of duplicate points (several points in the same location but each one has a different Z value). I would like to generate two shapefiles:

  • One in which I get rid of all the duplicates and keep only the point with the highest Z value at each location
  • One in which I get rid of all the duplicates and keep only the point with the lowest Z value at each location

Would there be a way to do this using the "Select by Expression" tool for instance?

enter image description here

2
  • Have you tried using the minimum() and maximum() expressions with $geometry as a filter?
    – Erik
    Nov 1, 2023 at 16:03
  • So in the minimum() or maximum() functions, if I use the $geometry as a filter, what should I use as a group_by value? I've added an ID column which has a single ID (integer increasing with an increment of 1) so I've got ID and Z as attributes.
    – Sken2107
    Nov 1, 2023 at 16:47

1 Answer 1

4

Use Select by expression with the following expression, then use Extract Selected Features.

Explanation: The expression uses overlay_nearest() with a very small distance (tolerance value) to get an array of the z values of all points clustered together, then gets the maximum value using array_max(). Check if the current point's z value equals this maximum.

Be aware: it seems you don't have an actual z-value, but an attribute named z. In this case, in the expression replace z($geometry) with "z" (3 times).

z($geometry)=  
array_max(   -- maximum z value (for minimum: replace with array_min)
    array_prepend( 
        overlay_nearest(  -- find all nearby points... 
            @layer,  -- ... from the same layer...
            z($geometry), -- ... get their z values...
            max_distance:=0.01,  -- ... not further distance from current point than this distance
            limit:=-1  -- ...get all points
        ), 
        z($geometry)  -- add current point to array
    )
)
5
  • Thank you for this. I tried this expression but it doesn't select any of the points and tells me "no matching feature(s) found" although I have a lot of overlaying points (duplicates). I also tried reducing or increasing the max_distance in you expression but it's still the same result so no features selected.
    – Sken2107
    Nov 2, 2023 at 9:04
  • Can you provide sample data?
    – Babel
    Nov 2, 2023 at 9:41
  • I've just added an image to my original post which shows typically how my data looks like.
    – Sken2107
    Nov 2, 2023 at 9:52
  • 1
    Your image shows an attribute table with a field named z. But an attribute is not the same as an actual z-value. That might be the problem. Replace z($geometry) in the expression with z three times. Then it should work.
    – Babel
    Nov 2, 2023 at 10:19
  • 1
    Yes, that was the issue, I replaced it by a Z and that now works fine. Thank you!
    – Sken2107
    Nov 2, 2023 at 10:31

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.