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:
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:
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 merge): 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.
Can anyone explain what the SQL statement of the function from command1 referenced above does and
Does anyone know how I can adjust the ogr2ogr command to obtain the desired result, which is dissolving the touching/intersecting polygons into one?
UPDATE: A couple of other variations I've tried and the errors:
I thought using the GRIDCODE field would do the same thing I want because every polygon has a GRIDCODE=1
ogr2ogr dissolve.shp merge.shp -dialect sqlite -sql "SELECT ST_Union(geometry), GRIDCODE FROM merge GROUP BY GRIDCODE" ERROR 1: In ExecuteSQL(): sqlite3_prepare(SELECT ST_Union(geometry), GRIDCODE FROM merge GROUP BY GRIDCODE): wrong number of arguments to function ST_Union()
ogr2ogr dissolve.shp merge.shp -dialect sqlite -sql "SELECT ST_Union(geometry) FROM merge" ERROR 1: In ExecuteSQL(): sqlite3_prepare(SELECT ST_Union(geometry) FROM merge): wrong number of arguments to function ST_Union()
Not sure why I'm getting this error or how to fix it.
UPDATE2: I think I found the right command to dissolve based solely on geometry, but I'm getting an error with the ST_Union command:
ogr2ogr dissolve.shp merge.shp -dialect sqlite3 -sql "SELECT ST_Union(geometry) as geometry FROM merge" ERROR 1: Undefined function 'ST_Union' used.
I tried using GUnion and got the same error.