22

I have a large number of polygons representing the boundaries of US counties which I need to merge into as few polygons as possible.

Is it possible to do this with non-commercial software, or software of a reasonable price? I have been thinking of ogr2ogr or the General Polygon Clipper library from the University of Manchester.

PostGIS is not available, nor is Java, but C is.

Lee Goddard
  • 512
  • 1
  • 4
  • 14

2 Answers2

40

With ogr2ogr (GDAL >= 1.10 with SpatiaLite support):

ogr2ogr output.shp input.shp -dialect sqlite -sql "SELECT ST_Union(geometry), dissolve_field FROM input GROUP BY dissolve_field"
Antonio Falciano
  • 14,333
  • 2
  • 36
  • 66
  • 2
    Do any of the Gdal/Ogr builds, for Windows, contain Spatialite support, MS4W, OSGeo4w,gisinternals, geoinformatica? – klewis Feb 01 '14 at 14:46
  • AFAIK OSGeo4W and packages maintained by Tamas Szekeres (http://vbkto.dyndns.org/sdk/) for sure. – Antonio Falciano Feb 01 '14 at 22:41
  • @afalciano - thanks. I tried that with -f KML and was told ERROR 4: Failed to create KML file output.kml. KML driver failed to create output.kml — do you know what might cause that? – Lee Goddard Feb 02 '14 at 16:58
  • 1
    What version of GDAL are you using (gdalinfo --version)? As workaround, you could try to create a shapefile first and then convert it to KML, because KML driver has some limitations. – Antonio Falciano Feb 03 '14 at 09:07
  • GDAL 1.5.3, released 2008/09/09! Appreciate this may well be the problem, and will now get later sources, as perhaps my KML is 2.2. Thanks! – Lee Goddard Feb 03 '14 at 09:21
  • 1
    I think so. In fact, I have specified that GDAL version has to be at least 1.10. So you should update GDAL in order to make it fully working. – Antonio Falciano Feb 03 '14 at 09:25
  • 5
    I had a devil of a time determining what to use for "geometry", was getting the error no such column: geometry. All of the similar examples here on Stack and in the reference docs use also broken variants like geo or the_geom. Finally I discovered [tag:ogrinfo] -so summary only switch: ogrinfo -so somedata.gdb my_layer_name, and filter for "Geometry Column =" – matt wilkie Jan 12 '16 at 22:50
  • 7
    also, if you just want to dissolve all polygons in the shapefile into one large polygon you could do: ogr2ogr output_dissolved.shp input.shp -dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry FROM input" remember, if you build GDAL yourself, you need to have sqlite installed and include at least sqlite and spatialite in the configuration, i.e. flags --with-sqlite --with-spatialite=yes or similar. – cm1 Oct 27 '17 at 12:28
4

If you want a pure light weight C access, you can use a combiantion of shapelib from Frank Warmerdam and gpc from Alan Murta. I find the DBF handling in the shapelib is a little bit tricky, but reading somthing it is OK. For perl you can find them in the CPAN Repository under the Geo::Shapelib and Math::Geometry::GPC entries. In addition with Geo::Proj4 for coordinate transformation you can have a minimalistic access to GIS operations. I like that.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
huckfinn
  • 3,583
  • 17
  • 35
  • Thanks — I did try that, but it seemed very, very slow: over an hour to aggregate 2,000 counties on a MacBookPro 4gig. Perhaps it's my coding :) – Lee Goddard Feb 04 '14 at 14:41
  • Do yo use any kind of indexing, overlapping bboxes for example? – huckfinn Feb 04 '14 at 17:44
  • No — I'm stumbling around in the dark, finding it hard to find refs that don't assume I've been doing this for years, or am running PostGIS. – Lee Goddard Feb 04 '14 at 18:20
  • 1
    But you can use Postgresql/PostGIS as well with all the advanced stuff indexing, relation ...operations. The data import for shape is very easy with the tool shp2pg. And the SQL syntax is not so difficult to learn... – huckfinn Feb 04 '14 at 19:06
  • PostGIS took seconds to figure out — beautifully simple, especially with ogr2ogr. But I'd rather be using GPC: I've been accumulating polygons from files, each time making a UNION, and it took hours to do what PostGIS did in literally seconds, which is why I say it must be my misreading of the docs. – Lee Goddard Feb 05 '14 at 19:00