2

I'm using QGIS 3.28

I want to find the neighboring polygons for each polygon in my layer, which is formed of polygons. Having searched for available solutions, I've come up with this:

https://spatialthoughts.com/2019/05/23/neighbor-polygons-aggregate-qgis/

I applied it accordingly, by adding a new field using the following:

aggregate(
     layer:= 'Regions',
     aggregate:='concatenate',
     expression:= to_string(region_id),
     concatenator:=', ',
     filter:=intersects($geometry, geometry(@parent))
 )

However, it didn't work. I've tried dozens of times, fine-tuning it every single attempt. Sometimes, out of the blue, the setting panel gives me preview results (see screenshot) and I get excited, only to get an error at the runtime, when I check the attributes table; I see the fields set to Null (or most of them) and when I click on "update all", I get the following error:

"an error occurred while evaluating the calculation string: No root node! parsing failed"

Having searched, I learned that this error is almost related to failure parsing some fields, due to their types not being String. I made sure to convert both input and output parameters into String, but to no avail.

  • I tried setting the parent to a copy of the layer, but this didn't change anything.

I'm sharing the shape file, the CSV file and other relevant files on my Google Drive: https://drive.google.com/drive/folders/1IpervV7mTMt8GJRpN5icgwh7oGLIZP-o?usp=sharing

Neighbors

Aggregate_Neighbors_1: Set the output to Text

Runtime error

1
  • You're right! Thanks! Fixed.
    – Iyad
    Dec 30, 2022 at 16:43

1 Answer 1

4

Use this expression to get a string-formated list of the $id of all neighboring polygons. If you want the content of an attribute field, replace $id with the name of this attribute, e.g. "region_id":

 array_to_string (overlay_touches( @layer, $id))

However, the polygon layer you provided is quite messy: many multipart polygons, overlaps, gaps etc. You should clean up the file first.

3
  • Awesome! This solved it perfectly! Here's the new expression:aggregate( layer:= 'Regions_1', aggregate:='concatenate', expression:= to_string(region_id), concatenator:=', ', filter:=intersects($geometry, geometry(@parent)) )
    – Iyad
    Dec 30, 2022 at 17:01
  • I just wonder what programming language is this? The assignment symbol ":=" being used remings me of Pascal/Delphi, while the use of $ for variables reminds of PHP, and the main user programming lang in QGIS is thought to be Python! This got me confused:)
    – Iyad
    Dec 30, 2022 at 17:04
  • 1
    QGIS expressions have their own syntax, but borrow heavily from other languages (also further ones like PostGIS), as you mentioned. So it is an own language, but with obvious similarities to other ones, see here: gis.stackexchange.com/a/247492/88814. See the full list of functions for details about syntax etc.: docs.qgis.org/latest/en/docs/user_manual/expressions/…
    – Babel
    Dec 30, 2022 at 17:17

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.