Skip to main content
4 of 8
Improved formatting, removed sign of appreciation

Dissolve multiple shapefiles so that touching polygons become one: GDAL

Trying to polygonize large rasters* but since the process is taking forever/crashing in GDAL, I am resorting to doing so in ArcGIS. However there is a 2GB limit on shapefiles in Arc so I am taking a combination approach where I split the rasters into 4 parts with gdalwarp, polygonize them in ArcGIS (polygonizing the quarters in GDAL is also taking forever while the process was extremely fast in Arc), and then putting them back together in GDAL to get around the slowness of gdal_polygonize.py and also the 2GB ArcGIS limit.

I am basing my solution partially on the answer found here, but am running into a problem with the final Dissolve step.

*The pixels of the rasters I am polygonzing only contain 1's (i.e. the pixel value of the things I'm interested in as polygons) and NoData (do not wish to have polygons in these areas), so the attribute table after polygonization looks like this:

attribute table

Which is want I expect.

The next step is to merge these (4) polygons back together in GDAL like this:

ogr2ogr -f "ESRI Shapefile" merge.shp seg7_9__h06v01_1_2011to2015_min_a.arcpoly.shp -nln merge # create merge.shp
ogr2ogr -f "ESRI Shapefile" -update -append merge.shp seg7_9__h06v01_1_2011to2015_min_b.arcpoly.shp -nln merge # add b.shp to merge.shp
ogr2ogr -f "ESRI Shapefile" -update -append merge.shp seg7_9__h06v01_1_2011to2015_min_c.arcpoly.shp -nln merge
ogr2ogr -f "ESRI Shapefile" -update -append merge.shp seg7_9__h06v01_1_2011to2015_min_d.arcpoly.shp -nln merge

Which works fine and gives me a result that looks like this, where the highlighted polygon is from one of the input shapefile parts and shows the edge between the two:

merge shp

My last step/goal is to dissolve the merge.shp so that the polygons on the edge that are touching (such as the one highlighted in red and the portion above it) become one polygon. In the post linked above, they suggested using the following command (command1):

ogr2ogr "output.shp" "input.shp" -dialect sqlite -sql "SELECT ST_Union(geometry), field FROM input GROUP BY field"

Because I do not really understand SQL or SQLITE, I wanted to try this command (command2) at first just to see what would happen:

ogr2ogr out_dissolve1.shp merge.shp -dialect sqlite -sql "SELECT ST_Union(geometry), field FROM merge"

but got the following error:

ERROR 1: In ExecuteSQL(): sqlite3_prepare(SELECT ST_Union(geometry), field FROM merge2):
 wrong number of arguments to function ST_Union()

I looked online and tried looking at documentation of ST_Union but could not find anything useful.

In conclusion:

  1. Can anyone explain what the SQL statement of the function from command1 referenced above does and

  2. Does anyone know how I can adjust the ogr2ogr command to obtain the desired result, which is dissolving the touching/intersecting polygons into one?

user20408
  • 1.5k
  • 1
  • 22
  • 43