Dissolve Shapefiles using OGR
Quite often, I’ll run across a large or complex shapefile that causes issues (crashing, very poor performance, etc) in QGIS or other desktop GIS.
However, sometimes you have to find a way to deal with these files, and not spend a lot of time worrying about them. Often, the best way is to head back to the mysterious black box that is the command line, and use the ultra-stable suite of tools available there. My favourite for spatial work is GDAL/OGR
I was recently struggling to do a simple dissolve on a large and complex shapefile. I didn’t want to bring it into a database, but QGIS would crash trying to dissolve the shapefile. Of course, OGR had no problem with the file!
Here’s how to turn this (nine features with three different attributes):
Into this (three attributes, three multi-part features!):
Simply use this command (substituting your file names and field names as appropriate) into ogr, and you’re all set!
ogr2ogr outputfile.shp inputfile.shp -dialect sqlite -sql “SELECT dissolvefield,ST_Union(geometry) as geometry FROM inputfile GROUP BY dissolvefield”
Posted: January 23rd, 2015 under GIS.