-2

How could I identify and select the rows with multiparts? How could I copy/save the polygons from a table with geom MULTIPOLYGON into a table with geom POLYGON?

Torsten
  • 396
  • 1
  • 8

1 Answers1

1

Read http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html

Find function ST_GeometryType. Use it like

SELECT * from my_table
WHERE ST_GeometryType(GEOMETRY)='MULTIPOLYGON';

How to create a new table with ElementaryGeometries has an answer already in Find and split Multipolygons in spatialite and the usage of the ElementaryGeometries function is

SELECT ElementaryGeometries('input_table','input_geometry','output_table','new_id_1', 'new_id_2');

The ElementaryGeometries is a special function and it requires that the input table is a real table (not a view) that has its spatial metadata inserted into SpatiaLite database.

Thus creating a new table from just multipolygons requires some interim steps.

CREATE TABLE interim as
SELECT * from input_table 
WHERE ST_GeometryType(GEOMETRY)='MULTIPOLYGON';

Then use RecoverGeometryColumn as documented in https://www.gaia-gis.it/stored-procs-win-test/spatialite-sql-latest.html.

And finally convert multipolygons from the interim table into polygons into a final table

SELECT ElementaryGeometries('interim','GEOMETRY','final_table','new_id_1', 'new_id_2');

It may feel a bit complicated to do this task with SpatiaLite but all the steps can be written into a script. But I wonder why you want to find just the multipolygons. It would be much easies to push the whole table with both polygons and multipolygons through ElementaryGeometries.

Myself I guess I would do your exact task of selecting and exploding multipolygons with ogr2ogr https://gdal.org/programs/ogr2ogr.html

user30184
  • 65,331
  • 4
  • 65
  • 118
  • I try to use ElementaryGeometries on a whole table which is in a spatiaLite database. The geometry column on final table is empty with select ElementaryGeometries('polyligne', "GEOMETRY", 'test', "pk","id") as GEOMETRY from polyligne. I am using the DBM of QGIS 3.16. Any idea ? – Leehan Jul 22 '21 at 08:31
  • 1
    The syntax of ElementaryGeometries is not like select .. from. The input and output tables are used as arguments. See the example and documentation (I corrected the link) http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html. – user30184 Jul 22 '21 at 09:17
  • Thanks. I had 2 mistakes : the FROM and the out_multi_id field whose name must be unique. – Leehan Jul 22 '21 at 09:54