Is there an option in QGIS to fill holes in polygons automatically by setting parameters e.g. maximum area, distance between borderlines? I want to delete/fill holes that a very "thin"/small.
Using QGIS 2.10.1-Pisa with Processing version 2.10.2, there are a couple of tools available from the Processing Toolbox:
- Fill holes tool which you can specify the maximum area.
- Delete holes tool which would be applied to the whole input layer.
Hope this helps!
I faced the same problem as yours with this shapefile, having many gaps between polygons.
In order to solve the problem, You could use a slight modification of tshiffle's method:
- First, perform the difference between your file and a polygon covering its whole extent.
This action will create a new shapefile consisting in only one polygon, so you have to explode it into single parts. For that, you could use Vector/Geometry Tools/Multipart to singleparts tool.
After that, delete the external entity. Now you have, in a new shapefile, several polygons covering the gaps of your original coverage.
- Perform a Union between the initial data and the single parts. Use the SAGA tool, because default QGIS option creates duplicates into the database.
- All the gaps will now appear as empty records in the database (for the example 72 rows). Sort your table using an area field and take the value from the smallest polygon in the original file. For this case, 0.13645 hectares.
- Finally, use the "Eliminate sliver polygons" tool, with the settings shown in the image.
- The final result does not have any gaps.
There may be more elegant solutions out there, but this has always worked for me.
1) Create "hole-filling" polygons. To do this draw a giant polygon larger than the original and use the "difference" tool. If you drew a much larger polygon you'll have all the "hole-filling" polygons plus a single larger polygon that goes around the original shape. Delete that large polygon, keeping the "hole-filling" polygons.
2) Merge the "hole-filling" polygons with the original shapefile. If you only want to fill holes that fit a certain criteria (ie under X number of square kilometers) you can filter for that here.
3) Dissolve the newly merged polygon on a common field.
In python / shapely, this can fill in your polygon:
import shapely from shapely.geometry import Polygon filled_shape = Polygon(shape_with_holes.exterior)
I've seen some explanations following the same path as gtapko and tshiffle's, but none worked for me because the invalid geometries made the difference tool disfunctional. So, I came up with a solution of my own which is not 100% automated, but it's manageable. Probably someone already figured this, but I'll take my chances:
1) Create an ID on the original, damaged layer
Creating an ID attribute is simple, but so is my solution, so I'll go over it.
First, you have to open your attribute table and then go to the Field Calculator, which is a button pretty much like an abacus. There, you will be given the option to create a new field or update an existing one. On the picture above I'm creating a new one called "ID". The formula is right at the end of the picture. (Sorry for having my QGIS set up in a different language. Since the design and the formulas are exactly the same throughout the language versions, I don't think it will get in our way here.)
2) Convert the polygons to lines
This step is located on the Vector > Geometry section the the menu. Your converted shape should look like this:
Right after converting the polygons to line, you will have to do the exact opposite, which is...
3) Convert lines to polygons
The options are right next to each other. Again, let's not mind the language barrier here.
4) Rejoice The reason you did steps 2 and 3 is because now you're sure those lines will be considered as polygons, which means they now have their own line on the Attribute Table. If you check it out, it will look like this:
The first one is ALWAYS the entire polygon (trust me, I've deleted a couple hundred lines today...) and the rest are the loose lines in the middle. You can enable editing and get ready for action. If you decide to try this, two things that you should keep in mind are: organize your table ID-wise, this way you're sure you're not missing anything and use shift to select clusters of data, just like any other program.
I'm aware this is not the crème de la crème of QGIS problem-solving, but if you're facing a situation where the tools are not working because of the invalid geometry these line create (I even tried dissolving by the ID, but it didn't work), then this might be a way out.