I took for granted that some polygons will overlap separately with several polygons. This makes it so one ID cannot describe all overlaps. A list of overlaps in your original layer's table can work for queries if you have another layer with overlap IDs. I use QGIS 3.4 so if you're using 2.x, it might be different.
- First, create a field in the attribute table of your polygon layer called for example
orig_id
and with the field calculator, fill it using the expression $id
- Then, use the vector geoprocessing tool Union to create a new layer where all overlapping parts are split. The result will contain duplicate polygons for every original polygon that overlaps (we'll deal with them later on).
- Then use the Join attributes by location (summary) from the processing toolbox with these parameters:
- Input layer: step 2's resulting layer
- Join layer: step 2's resulting layer
- Geometric predicate: equals
- Field to summarize:
orig_id
- Summaries to calculate: count
- Again with Join attributes by location(summary), calculate the number of overlapping polygons with these settings:
- Input layer: your original polygons layer
- Join layer: step 3's resulting layer
- Geometric predicate: overlaps
- Field to summarize:
orig_id_count
(provided you called the field orig_id earlier)
- Summaries to calculate: max
The number of overlaps with other polygons is now settled in a new layer that you could rename (to avoid getting lost in the temporary names) but let's keep going:
- Using the Delete duplicate geometries tool from the processing toolbox, get rid of identical copies in step 3's result layer.
- With the Extract by attribute tool, get rid of the parts that don't overlap using these parameters:
- Input layer: step 5's resulting layer
- Selection attribute:
orig_id_count
provided you named your fields like I did
- Operator: ≠
- Value: 1
- In step 6's resulting layer, create a new field called for example
over_id
and, with the field calculator, copy the feature ID into it using the expression $id
This layer is now your index from which you can look for unique overlap IDs. Keep it!
- Then, using the Join attributes by location (not the same as earlier), match the overlapping polygon IDs with the polygon layer, using these settings:
- Input layer: step 4's resulting layer
- Join layer: step 6's resulting layer (with the field added at step 7)
- Geometric predicate: overlaps
- Fields to add: over_id
- Join type: one-to-many
The result is one copy of the original polygons for each unique overlap combination.
It is now possible to simply fill a new field (for example called over_con
) in that layer for each feature using an expression that will concatenate overlap IDs for each original polygon ID. The expression looks like this:
concatenate( to_string("over_id"),group_by:="orig_id",concatenator:=',')
Finally, use the Delete duplicate geometries tool on the final layer to get back to the original number of polygons. You can delete the over_id
field as it isn't needed anymore.
The result of the whole operation for your original layer is two additional fields:
- one that contains a comma separated list of overlap IDs that can be queried to return polygons that do overlap each other.
- one that contains the number of overlaps that a particular polygon shares with other polygons.
Combined with the overlap layer, these can help select polygons that overlap at specific locations.