I wanted to merge two polygon geometries in a
SpatiaLite database (an example is the land feature layer shown in the screenshot below) into a single geometry using
QGIS.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhuBE-4ai3aLrehOeJxIkLhLZrSffmW25MlQUvHqX5lnlLZu4rpyuKGIHeitAajF95VYr0NRkX3pLDsU5ZTJf4jdrZ83FUOoj_xsA9wSdK5NRgm3iGXXZFicJVbqqGBLKl42D8KZteQw/s1600/gunion01.png) |
Two SpatiaLite polygons labelled with the primary key id number. |
In
QGIS, the usual command to perform this task is the
Union command, but unfortunately it creates only Shapefiles. The alternative is to use the
QSpatiaLite plug-in and run some SQL commands to do the job. The following steps show how to merge two polygons into one polygon using the SpatiaLite
GUnion operator.
- In QGIS, select Database | SpatiaLite | QSpatiaLite.
The QSpatiaLite dialog box appears.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw44SVtbDOq3zMxytKXvZS04BJxu49OCmoC4Y86YchD9wcK5HcfsTbiL2GpDVwhxs0EBsAQceSZq-AG-Y5bp3d-_Eon7ni4jIiD4jfOtreNO04ln-tkgjCt_1UhxWIqj78FPAV4V77tw/s1600/gunion02.png)
- Note down the primary key numbers or any suitable identifier of the polygons to be merged, e.g. pkuid 10 and 20.
- In the SQL tab field, type in the SQL command.
SELECT GUNION
(
(SELECT geometry FROM land WHERE pkuid = 10)
,
(SELECT geometry FROM land WHERE pkuid = 20)
)
AS geometry
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWsZOJ5CVQGQIPVuiF_8XlRZQWi7pUbi7M40TId0FncWS2YEMYRC6ccZSEiZFAFJz4tgbIQKwNQh3QP6zfBwFiKU9pHdRm5YBvzwnX3LZO0KxRjAaPRv6fjXDkJyokmdXD-PM5W-Dl8A/s1600/gunion03.png)
- Click Run SQL.
A temporary new geometry object is created.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxuWPweDcYItDH4GyPN9rUSQFBb7MZ36c_aoeLE_dRZbW7FuodbgM9UoQKBGQm_u3KpXP8RS620BwZS_X-XRs0xmNq7smvdUTg09T3SlTnukpqw_YBXJhislJcfyZ9JR25sYmwdzJqeg/s1600/gunion04.png)
Note: if the above SQL command execution is successful, then it is fine to proceed to create a new merged polygon feature and delete the original two polygons.
- In the SQL tab, type in the following SQL command to create and insert a merged polygon feature record.
INSERT INTO land (geometry)
SELECT GUNION
(
(SELECT geometry FROM land WHERE pkuid = 10)
,
(SELECT geometry FROM land WHERE pkuid = 20)
)
AS geometry
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUpkYx0cmdnP9s4JuvMejy9QqZENaIZOEoeEn7ExE8dV7DB2zTsIyyILtRdMwkCZLgeI2BqnSux6LBgMgRRudpBhdwQeg0o2zNKldMU4_snp2D6rr_ccAJrcbHV8xuHL9wqv1Z9EYhXg/s1600/gunion05.png)
- Click Run SQL.
A new row is inserted into the land table.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIyHnDdOTKiNHasQ-QOkNAaUHgUHhMEsBkMV22sWQON8K_Z-97B_XW4fedFmcQqpBfWRUUp_wXd5Nqs9vb81aQVaoDqqewMo3T2TUk58DuYwbycDU84Q7RzESaA8BnqCHfJ_KSQYqQIQ/s1600/gunion06.png)
- In the SQL tab, type in the SQL command to delete the original two polygon features.
DELETE FROM land WHERE pkuid = 10 OR pkuid = 20
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2XcPdXvPnwiqtKl76tFjqXgJxdTNiquMt7gE7LjowfHyeip69lJpR45yC3fk2d9Z5PvijYKGasqbQwBsVigj_j2EShswxcXv02N-gpC9tQ3sEMTW2x_Qx26u5MvAjafuAgx9DbK2ZiA/s1600/gunion07.png)
- Click Run SQL.
The original polygon features are deleted.
- Refresh the map view by toggling the land feature display off and on.
The merged polygon is displayed.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC-Bcb3sWfxkrNrIbJyScE1SgWXxUYIfbATJ8xn98nQ2Xe-02kpC5noP7LY28BFHJwgv7DxaFch93cAMYBgNIjBBymAAUnOCNc5ukCSPu2fjGFy1l_FuTRC3aG4LvtmKwgeZv_L7GFDA/s1600/gunion08.png)
No comments:
Post a Comment