I have a line map of buildings in a city. I was able to correctly polygonize the buildings in QGIS with the Polygonize processing algorithm. All the holes/islands became features as expected. Now I need to select and remove all features that are completely surrounded by other (one or more) features. Examples shown in red on the image: Inner yards to remove
These are not part of buildings, but inner yards.


an alternative is to use "vector > geoprocessing tools > dissolve" (--Dissolve all-- in the list of dissolve fields), then select the polygons that are "within" the dissolved polygons using the "spatial query" plugin.

| improve this answer | |
  • Thanks for this. My next issue here is how to dissolve a large dataset based on touching geometries. – csandor Sep 29 '15 at 20:17
  • @csandor You can't dissolve those geometries as well based on a common or shared attribute? – SaultDon Sep 29 '15 at 21:57
  • @csandor see my edit. As for the size, I've successfully used dissolve with hundred thousands of polygons – radouxju Sep 30 '15 at 10:39

You can use for example this python script to highlight or delete the features that are completely within another feature.

layer = iface.activeLayer()  #or the layer you want to use
features_a = layer.getFeatures()
features_b = [feat for feat in layer.getFeatures()]
within_list = list()
for feat_a in features_a:
    for feat_b in features_b:
        if feat_b.id() != feat_a.id():
            if feat_b.geometry().within(feat_a.geometry()):

To highlight the features you can use:


And to delete the highlighted features use:

caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.DeleteFeatures:
    res = layer.dataProvider().deleteFeatures(within_list)

As a test result I get for example: enter image description here

But before you execute this script please save your qgis file because if you want to process a large file this script probably crashes your qgis.

| improve this answer | |
  • Thanks for the idea. As the original picture of mine shows I have a lot of cases when more polygons surround a quasi island, so they are not completely within another polygon. – csandor Sep 29 '15 at 19:23

You can also duplicate layer and then make spatial query with "touches" between them. Works with prepared data.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.