We have a very large shapefile with over 30000 features and want to merge it with a script on a server. Currently we try it with QGIS, but the processing takes way too much time.

Is there a way to do it with the GDAL/OGR Tools or Python?

  • 1
    By merge, do you mean to say, change the 30000 features to just one multipart feature? – R.K. Sep 15 '12 at 13:30
  • yes, thats what I want to do. – leak_my_brain Sep 15 '12 at 14:54
  • What are those data by the way? How would they be merged? Do they have common fields etc? – R.K. Sep 15 '12 at 15:14
  • We are working with the OSM-Coastline Polygons, which come from here: link – leak_my_brain Sep 15 '12 at 16:23
  • We want to render them in TileMill, but get artefacts at the cutlines, which we want to eliminate by merging the polygons. – leak_my_brain Sep 15 '12 at 16:25

In GIS context, the better is organize your data in a database (!).

Then, many kinds of operations will be simple. With PostGIS (using PostgreSQL as database) the command that solves your problem is

  SELECT ST_Collect(geom) FROM your30000features_table; 

And the commands are standard, see standard OGC functions ST_Collect and ST_Union.

Using as input "OSM-Coastline Polygons", you are using POLYGON OGC types in the database. The ST_Collect(geom) of a set of POLYGONs will return a GEOMETRYCOLLECTION, but you can convert it into a MULTIPOLYGON, or produce it directly by the ST_Union function.

  • This seems to be the most viable solution, as I wanted to try PostGIS anyway. I'll try it out right now. Thanks! – leak_my_brain Sep 18 '12 at 6:59
  • You're welcome; good decision, try it! PS: if you are new user in the Stackexchange, remember to click at the check icon in your accepted answer. – Peter Krauss Sep 18 '12 at 11:18
  • What would be the complete SQL-String for doing this with ST_Union? I just dont get it... – leak_my_brain Nov 6 '12 at 23:09
  • Hum... Sorry, I "dont get" your question, both are aggregate functions, use the same: 'SELECT ST_Union(geom) FROM table'. Perhaps you must check CPU time with 'EXPLAIN SELECT ST_Union(geom) FROM table', and compare with ST_collect... And read the guide to check what you need (check also ST_UnaryUnion) about "dissolve boundary" efects. – Peter Krauss Nov 7 '12 at 6:42
  • Sorry, I was a bit undefined. How do I write that into a new table. Yesterday I thought I worked it out, but the resulting table was empty after this: CREATE TABLE water_polygons_merged AS SELECT ST_Union(geometry) AS geometry FROM water_polygons; -- Update the geometry_columns table SELECT Populate_Geometry_Columns(); – leak_my_brain Nov 7 '12 at 9:43

Not quite Python or GDAL but maybe you could try GRASS' v.extract function. It should let you extract and dissolve the features. You can access it through QGIS' SEXTANTE Toolbox.

  • I think trough GRASS I add a lot of overhead to the whole thing. But other things don't work, i'll try it out. – leak_my_brain Sep 18 '12 at 6:57

i dont know whether arcmap Python scripting works for you or not but you can check out this topic about batch merging script here

Beside this in python there is a library with the name of pyshp about reading and writing shapefiles.

  • Thanks so far. I don' use ArcMap, but give pyshp a try. – leak_my_brain Sep 18 '12 at 6:56
  • Just a warning though, pyshp is a pure python library so it might be very slow considering the number of features it will have to process. – R.K. Sep 18 '12 at 7:05

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.