2

As you can see from the screen capture below, 2 waterbodies should be merged but were digitized as separate polygons. Using the Select Within Location set to 1m, these waterbodies and others similar, should be the only ones selected but that's not the result I'm getting, all of the waterbodies, no matter how far apart, are selected.

Why is the script not running as I expect it?

screen capture of 2 touching waterbody polygons

5
  • 1
    What CRS do you use for the layer? If its EPSG:4326, a distance of 10 means 10 degrees - that would be roughly the distance from London to Hamburg or from Barcelona to Rome. Make sure your layer is in a projected CRS. Reproject the layer, do not simply "set" (change) the layer's CRS! See gis.stackexchange.com/a/383437/88814 Commented yesterday
  • EPSG:26917 - NAD83 / UTM zone 17N, project is set to the same Commented yesterday
  • 1
    OK, so then you should provide more information, otherwise its difficult to guess what went wrong. Providing sample data could help, as well. Commented yesterday
  • I've created a small shapefile that includes waterbodies that should be merged because they touch each other and others that are clearly separate. I tried the Select Within Distance set to 1m (UTM17N) but the process continues to select all of them, not just those that are touching. The zip file containing the shapefile is: drive.google.com/file/d/1WzqIyuD2BxZCrhLPOuGXfJa7E0k0S0rP/… Commented 23 hours ago
  • 1
    All polygons are 0m from themselves so it selects all polygons. Select one polygon, run the tool with "Selected features only" checked and it will select any other polygons within 1m of that selected polygon. Perhaps just dissolve. Commented 21 hours ago

1 Answer 1

3

Use the following expression to create a unique ID for all polygons that are contiguous or have a distance of max 10 units (change that value on line 6). You could use this to create a new field with Field Calculator.

The expression creates a buffer of 10 meters around all polygons, then dissolves the result to create a multipart geometry, where every contiguous group of polygons is one part. It then checks within which of these dissolved parts the initial polygon is and assigns a unique id for this part.

You could also use the same expression, adding 2 = [expression] with Select by expression to select only contiguous polygons, here the ones with no. 2 (using the data you provided):

enter image description here

The expression to use:

array_max(
    with_variable(
        'mypolys',
        geometries_to_array (
            buffer (collect
                (buffer (@geometry,10)
            ),
            0
        )
    ),
    array_foreach(
        generate_series(0, array_length (@mypolys)-1),
        case
            when within (@geometry, @mypolys[@element])
            then @element
        end
    )
))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.