[Qgis-user] how to count overlays of objects

Ludwig Kniprath ludwig at kni-online.de
Sat Aug 19 04:59:22 PDT 2023


Hi Daniela,

possible solution:

1. run the "Union" algorithm that splits the input polygons

2. add a new integer-column to the "Union"-result-layer f. e. "num_overlays"

3. optionally an additional column with type "integer list" or "string", 
if You want to see afterwards, which features intersect, f. e. 
"overlay_ids"

4. run the script below, that performs a self-intersection for each 
feature to find the spatial identical overlaps (Note: Layer- and 
column-names hard coded, adjust if necessary)


union_layer = QgsProject.instance().mapLayersByName('Union')[0]

union_layer.startEditing()

for union_feature_1 in union_layer.getFeatures():
     num_overlays = 0
     overlay_ids = []
     for union_feature_2 in union_layer.getFeatures():
         if 
union_feature_1.geometry().equals(union_feature_2.geometry()) and 
union_feature_1.id() != union_feature_2.id():
             num_overlays += 1
             overlay_ids.append(union_feature_2.id())

     union_feature_1['num_overlays'] =  len(overlay_ids)
     # solution for column type "string" instead of "integer list":
     # union_feature_1['overlay_ids'] =  ','.join(str(id) for id in 
overlay_ids)
     union_feature_1['overlay_ids'] =  overlay_ids

     union_layer.updateFeature(union_feature_1)

union_layer.commitChanges()



hth

Ludwig





Am 18.08.23 um 16:15 schrieb Daniela Friebel via QGIS-User:
>
> Dear Community
>
> I have a vectorlayer with many objects (polygons) that overlap. Now I 
> want to create a new layer, where each overlap results in a separate 
> object, which has an attribute with the number of overlaps.
>
> I have already tried various things, but have not yet found a 
> solution. With the "union" tool I can generate the overlaps as 
> objects, but I cannot count the number of overlaps.
>
> I am grateful for any help and hints.
>
> (I would like to count the overlaps to eventually create a heatmap. If 
> there is a completely different way of doing this, I would also 
> appreciate a tip).
>
> Kind regards
>
> Daniela
>
>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info:https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230819/573d5ab9/attachment-0001.htm>


More information about the QGIS-User mailing list