4

How do I apply using this same concept, but simply merging all features into one?

ogr2ogr output.shp input.shp -dialect sqlite -sql 
"SELECT ST_Union(geometry), dissolve_field FROM input GROUP BY dissolve_field"
Steven Lutz
  • 481
  • 4
  • 12

1 Answers1

9

In order to merge all features into one, you should do:

ogr2ogr output.shp input.shp -dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry FROM input"

where geometry is the special field used in order to represent the geometry of the features in SQLite SQL dialect and input in the SQL statement is the input layer name.

Antonio Falciano
  • 14,333
  • 2
  • 36
  • 66
  • 3
    Thank you for posting this. I believe your solution is the same as what ended up working for me:

    ogr2ogr -f "ESRI Shapefile" output.shp 32768.shp -dialect sqlite -sql "select ST_union(geometry) from '32768'"

    – Steven Lutz Jul 07 '15 at 20:18
  • I would like to apply this script on Python like this question. https://gis.stackexchange.com/questions/39080/using-ogr2ogr-to-convert-gml-to-shapefile-in-python. I am a bit confused with the arguments though. I run this one: ogr2ogr.main(["","-f", "ESRI Shapefile", "output.shp", "input.shp", '-dialect sqlite', '-sql "SELECT ST_Union(geometry) AS geometry FROM input"' but it did not work out. How many arguments are there? – Ilias Machairas Jul 04 '22 at 19:22
  • @IliasMachairas This does not work for GDAL 3.4.2 . I get an error about an incorrect number of argument supplied to ST_UNION. I downgraded to GDAL 3.2.2 and it worked fine. I have not tested it on the latest version of GDAL. – Dean Sherwin Sep 05 '22 at 13:45
  • @IliasMachairas Actually I do not think the version of GDAL is the issue. I think some system dependency is causing this (possible sqlite/spatialite). I suggest using the official GDAl ubuntu full docker images for this task. – Dean Sherwin Sep 05 '22 at 13:59