I have a polygon layer in QGIS 3.32.2 which I want to clean for later analysis. There should be no polygons that share any area at all, but they would touch quite frequently. However, the polygon layer I have to use has some errors in it so that sometimes there are equal polygons overlapping or they overlap by just a bit.
I want to annotate each feature with all types of overlap it has with other features/polygons from the layer. Disjoint and touches would be ok while everything else needs to be filtered out. Since I want/need to work non-destructively, I would annotate each feature and then filter the layer based on that. I chose to do this using the field calculator and got the following function:
CASE--touch and intersectWHEN array_to_string(overlay_touches('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'touches_and_intersects'--equalsWHEN array_to_string(overlay_equals('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'equals'--touchesWHEN array_to_string(overlay_touches('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'touches'--disjointWHEN array_to_string(overlay_disjoint('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'disjoint'--withinWHEN array_to_string(overlay_within('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'within'--containsWHEN array_to_string(overlay_contains('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'contains'--intersectWHEN array_to_string(overlay_intersects('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry)) <> '' THEN 'intersect'ELSE 'no_overlap'END
I ran the code on a smaller layer that I created to have all kinds of overlaps/disjoint. I got results, but they were just not right. When I then edited it so that e.g. a disjoint also cannot intersect or touch other features (using AND NOT) the result was still wrong. Then I simplified the code to get to the core of it:
array_to_string(overlay_touches('field_calc_test_6eea42a9_d4df_4074_9eac_bc1b465fed54', $geometry))
Not using the array_to_string function only gave me NULL while this code gave me another error:
There is no connection between the number of commas / NULL and the number of other features each feature touches. Where am I going wrong here? This thread seemed to give me an answer but this does not work for me. I also tried asking ChatGPT but to no result.