4

I've been using ogr2ogr to do most of what I need with shapefiles (including dissolving them). However, I find that for big ones, it takes a REALLY long time.

Here's an example of what I'm doing:

ogr2ogr new.shp old.shp -dialect sqlite -sql "SELECT ST_Union(geometry) FROM old"

In certain instances, one might want to dissolve common neighboring shapes (which is what I think is going on here in the above command). However, in my case I simply want to flatten the entire file and every shape in it regardless of the values (I've already isolated the shapes I need).

  • Is there a faster way to do this when you don't need to care about the values and just want a shape that outlines the array of shapes in the file?
stewart715
  • 5,557
  • 11
  • 47
  • 80

2 Answers2

3

If you have isolated the shapes, and they don't have any shared boundaries, they can be easily collected into a single MULTIPOLYGON using ST_Collect. This should be really fast and simple to do:

ogr2ogr gcol.shp old.shp -dialect sqlite -sql "SELECT ST_Collect(geometry) FROM old"

If the geometries overlap and the boundaries need to be "dissolved", then ST_Union must be used. Faster spatial unions are done with a cascaded union technique, described here for PostGIS. It is supported by OGR, but it doesn't seem to be done elegantly.

Here is a two step SQL query. First make a MULTIPOLYGON of everything with ST_Collect (this is fast), then do a self-union which should trigger a UnionCascaded() call.

ogr2ogr new.shp old.shp -dialect sqlite -sql "SELECT ST_Union(gcol, gcol) FROM (SELECT ST_Collect(geometry) AS gcol FROM old) AS f"

Or to better view the actual SQL statement:

SELECT ST_Union(gcol, gcol)
FROM (
  SELECT ST_Collect(geometry) AS gcol
  FROM old
) AS f
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mike T
  • 41,085
  • 18
  • 152
  • 203
  • Thanks, Mike. This is a nice lead but seems to still take forever. Perhaps I'll look at PostGIS. Will leave this open a little longer and then accept. Could be at a dead end. – stewart715 Apr 21 '15 at 00:25
  • 1
    @stewart715 does ST_Collect (only) take forever, or the combined query? PostGIS is more mature than SpatiaLite, where your initial query (using just ST_Union) should work. – Mike T Apr 21 '15 at 00:34
-1

I've had better success (i.e. faster) by converting it to raster then back to vector. For example:

# convert the vector file old.shp to a raster file new.tif using a pixel size of XRES/YRES
gdal_rasterize -tr XRES YRES -burn 255 -ot Byte -co COMPRESS=DEFLATE old.shp new.tif

# convert the raster file new.tif to a vector file new.shp, using the same raster as a -mask speeds up the processing
gdal_polygonize.py -f 'ESRI Shapefile' -mask new.tif new.tif new.shp

# removes the DN attribute created by gdal_polygonize.py
ogrinfo new.shp -sql "ALTER TABLE new DROP COLUMN DN"
AndrewHarvey
  • 2,867
  • 11
  • 21