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?

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

  • OpenSourceSoftware can do alot of things, what do you need... a tool, a library and have a C compiler too work with in which environment/operating system?
    – huckfinn
    Feb 1 '14 at 1:14
  • download qgis from www.qgis.org Feb 1 '14 at 3:19
  • @huckfinn: I'm on CentOS, and a command line tool or Perl-XS or C library would be ideal, thanks for asking. My source data is in ESRI Shapefiles, though I have copies of the polygons into MySQL. I do have ogr2ogr but not the MySQL dialect. Feb 2 '14 at 16:49
  • I presume the gpc referenced in title is GPC – General Polygon Clipper library from University of Manchester Jan 12 '16 at 19:19

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"
  • 1
    Do any of the Gdal/Ogr builds, for Windows, contain Spatialite support, MS4W, OSGeo4w,gisinternals, geoinformatica?
    – klewis
    Feb 1 '14 at 14:46
  • AFAIK OSGeo4W and packages maintained by Tamas Szekeres (vbkto.dyndns.org/sdk) for sure. Feb 1 '14 at 22:41
  • 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. Feb 3 '14 at 9:07
  • 4
    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 ogrinfo -so summary only switch: ogrinfo -so somedata.gdb my_layer_name, and filter for "Geometry Column =" Jan 12 '16 at 22:50
  • 5
    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

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.

  • 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 :) Feb 4 '14 at 14:41
  • Do yo use any kind of indexing, overlapping bboxes for example?
    – huckfinn
    Feb 4 '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. Feb 4 '14 at 18:20
  • 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 4 '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. Feb 5 '14 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.