Creating list of adjacent polygons in QGIS


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.

Best Answer

You can use SQL and join the layer to itself based on distance and where the ids are different.

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

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

enter image description here

Related Question