5

The answer to Using join in Mapviewer describes an effective way to count the number of adjacent polygons. However, I need to create a list of the adjacent polys.

I envision a process that generates an output table containing the source and adjacent unique IDs, something like this for the image below:

Source poly Adjacent poly


1                  2
1                  3
1                  4
1                  5
2                  1
2                  3
3                  1
3                  2
3                  4
4                  1
4                  3
5                  1

enter image description here

The beauty of the aforementioned post is that the adjacent polygons do not have to touch; see polygon #2 in my example. Indeed, the "gap" distance can be set by the user.

I've searched the internet, as well as the Plugin repository, without success. There is an old SE-GIS answer involving Spatialite, but I fear it is out of date.

How do I create a list of adjacent polygons?

After @Spacedman's comments, in my example, I assumed that a user-defined threshold had been applied that was >= the gap around polygon #2, which would include it. Had the threshold been < the gap distance, polygon #2 would be excluded.

0

3 Answers 3

8

You can use SQL and join the layer to itself based on a maximum distance and where the ids are different (to prevent joining polygons to themselves).

Change mylayer to the name of your layer, and 30 to your distance limit:

select row_number() over() as newid,
        a.id as aid, b.id as bid, 
        a.geometry
from mylayer a
left join mylayer b
on st_distance(a.geometry, b.geometry)<30 and a.id<>b.id
order by a.id, b.id

enter image description here

0
6

To complete the answers given so far: you can also use the field calculator to get a list of neighbours, with the expression:

array_to_string(
    overlay_nearest(
        layer:='polygons',
        expression:="id",
        limit:=10,
        max_distance:=30
        )
    )

enter image description here

enter image description here

1
  • Although this works, the output is separated by commas, where my OP required a separate record for each combination.
    – Stu Smith
    Commented May 19 at 1:02
1

If you use the Spatial Join Tool and the layer on itself with the option of "Touches" you can get a list of all Polygons around that is similar to your request.

Here the steps:

  • Open the "Join attributes by location" Tool: Go to the menu bar and select Vector > Data Management Tools > Join attributes by location.
  • Set up the Join Operation: In the "Join attributes by location" dialog box, select your target polygon layer under the "Target vector layer" dropdown menu. Choose the polygon layer again under the "Join vector layer" dropdown menu. You should have the polygon in both input and output.
  • Define the Spatial Join Method: Select the spatial relationship between the polygons you want to consider. You can choose "Touches". You can also choose which fields you want to keep in the join. It would be a good idea to reduce the number of fields to just the bare minimum to check your results later.
  • Run the Spatial Join: Once you've configured the settings, click "Run".
  • After the spatial join is completed, you'll have a new layer containing the polygons from your target layer joined with the attributes of the join layer based on the spatial relationship you specified.

To take distance into account, you could create a buffered polygon first and use that as one of the layers in the spatial join.

I am sure there are other ways to do this programmatically or with Plugins. This method uses the built in QGIS Spatial tools.

Here is the resulting table from my test. enter image description here

Polygon 21 - Far Western has 5 adjacent polygons - 13, 15, 20, 14 and 19. This can be exported as a csv.

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.