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.
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.
The "Delete Holes" tool combines both of the above functionalities. You can optionally specify a maximum area of hole to delete. If left as 0.0, the tool will delete all holes of any size.
There are a couple of tools available from the Processing Toolbox:
Using QGIS 2.10.1-Pisa with Processing version 2.10.2.
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:
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.
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.
It is also possible to solve the same task using the "Geometry by expression" with a corresponding expression.
make_polygon(
array_first(
geometries_to_array(
order_parts(
boundary($geometry),
'area(make_polygon(boundary($geometry)))',
False
)
)
)
)
if(
num_interior_rings($geometry) = 0,
$geometry,
coalesce(
sym_difference(
collect_geometries(
array_foreach(
array_filter(
array_foreach(
generate_series(1, num_interior_rings($geometry), 1),
interior_ring_n($geometry, @element)
),
area(make_polygon(@element)) > 300 -- specify the maximum area
),
make_polygon(@element)
)
),
$geometry),
$geometry
)
)
Features with holes in which an area is more than 300 will be filled.