Creating polygon from hole in QGIS?
I recommend Digitizing Tools plugin.
Once installed, you will find a new toolbar. The eighth from the left of this toolbar, there is an icon called Fill ring with a new feature (interactive)
.
For you to fill all rings at once;
- Switch to Fill all rings in selected polygons with new feature mode by clicking on a small triangle (right-hand-side).
- Start editing mode and select polygons you want to work on (you can select all polygons).
- Click on the tool icon.
- A small dialog box will appear and asks you to give a new attribute value. This value will be given all fillings, so that it helps you to identify these new features in the attribute table. Hit
[OK]
to close.
In QGIS I can suggest using a "Virtual Layer"
through Layer > Add Layer > Add/Edit Virtual Layer...
.
Let's assume there is a layer "layer_with_holes"
with its corresponding attribute table, see image below.
There are possible two cases.
Case I. When holes that are located on the polygon edges should not be included.
With the following query, it is possible to create a new polygon layer from the empty gaps inside the polygons.
-- generate series
WITH RECURSIVE generate_series(cat) AS (
SELECT conf.start
FROM conf
UNION ALL
SELECT cat + conf.step
FROM generate_series, conf
WHERE cat + conf.step <= conf.stop
),
-- config
conf AS (
SELECT
1 AS start,
1 AS step,
SUM(st_numgeometries(st_difference(make_polygon(st_exteriorring(l.geometry)), l.geometry))) AS stop
FROM "layer_with_holes" AS l
)
-- query
SELECT cat, st_geometryn(st_union(st_difference(make_polygon(st_exteriorring(l.geometry)), l.geometry)), cat) AS geom
FROM generate_series, "layer_with_holes" AS l
GROUP BY cat
The output Virtual Layer with its Attribute table will look as follows.
Case II. When holes that are located on the polygon edges should be included.
With the following query, it is possible to create a new polygon layer from the empty gaps within the polygons.
-- generate series
WITH RECURSIVE generate_series(cat) AS (
SELECT conf.start
FROM conf
UNION ALL
SELECT cat + conf.step
FROM generate_series, conf
WHERE cat + conf.step <= conf.stop
),
-- config
conf AS (
SELECT
1 AS start,
1 AS step,
SUM(st_numgeometries(st_difference(make_polygon(st_exteriorring(l.geometry)), l.geometry))) + st_numgeometries(st_difference(make_polygon(st_exteriorring(st_union(make_polygon(st_exteriorring(l.geometry))))), st_union(make_polygon(st_exteriorring(l.geometry))))) AS stop
FROM "layer_with_holes" AS l
)
-- query
SELECT cat, st_geometryn((st_union(st_union(st_difference(make_polygon(st_exteriorring(l.geometry)), l.geometry)), st_difference(make_polygon(st_exteriorring(st_union(make_polygon(st_exteriorring(l.geometry))))), st_union(make_polygon(st_exteriorring(l.geometry))))), cat) AS geom
FROM generate_series, "layer_with_holes" AS l
GROUP BY cat
The output Virtual Layer with its Attribute table will look as follows.