I have created a shapefile from raster. The shapefile was generated using contours detection. I am in the process of automating feature detection using Python and open source libraries such as shapely, fiona, ogr. The problem I am having is that the shapefile generated works well in detecting features small in size but when it tries to detect large features it also detects features inside the large feature. This results in feature inside a feature problem.

I have been trying to implement polygon inside a polygon implementation but have not been able to implement it successfully. I am thinking of finding a way in which this issue of feature inside a feature gets removed during the process of making the shapefile at the very beginning (raster to shapefile conversion). Need some suggestions on how to proceed on this issue.

The red highlighted area is the region I am interested in and everything inside it needs to be removed.

The red highlighted area is the region I am interested in and everything inside it to be removed. This removal I needs to do for every feature which has a feature inside it

  • Can you add a screen shot? Are your polygons overlapping?
    – klewis
    Mar 15, 2016 at 19:05
  • Hi @klewis, I added the screenshot, The red highlighted area is the region I am interested in and everything inside it need to be removed.
    – webapp
    Mar 15, 2016 at 20:24
  • 2
    Does this help? gis.stackexchange.com/questions/164631/…
    – klewis
    Mar 15, 2016 at 20:48
  • I envision a two part workflow: 1) select polygons > X area; 2) run a "difference" command.
    – Aaron
    Mar 16, 2016 at 0:06
  • Hi @Aaron, can you explain a bit more on what you mean by the difference command?
    – webapp
    Mar 16, 2016 at 1:12

1 Answer 1


It seems (but impossible to say as the details are missing) that you wish to remove ( in the polygon processing step) artefacts that are the results of small protrusions. The key to your answer is in the step earlier - the generation of the contours. Essentially, you can to generalize or smooth the raster first (e.g., based on a focal function to allow for smoothing), and possibly you can generalize with some low pass filter areas that are too small for you to care about ( that would be if you wanted to remove also tiny areas outside the polygons. Only after you have processed your model, generate the contours.

This may not solve all of your issues, but can solve many. Second, you need to select all geometries (polygons) that are non empty - contain other features (unless you have another criterion). Then, you union them.

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.