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.

4
  • 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, 2014 at 1:14
  • 1
    download qgis from www.qgis.org Feb 1, 2014 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, 2014 at 16:49
  • I presume the gpc referenced in title is GPC – General Polygon Clipper library from University of Manchester Jan 12, 2016 at 19:19

2 Answers 2

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"
8
  • 2
    Do any of the Gdal/Ogr builds, for Windows, contain Spatialite support, MS4W, OSGeo4w,gisinternals, geoinformatica?
    – klewis
    Feb 1, 2014 at 14:46
  • 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, 2014 at 9:07
  • 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. Feb 3, 2014 at 9: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 ogrinfo -so summary only switch: ogrinfo -so somedata.gdb my_layer_name, and filter for "Geometry Column =" Jan 12, 2016 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, 2017 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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