In my current project I'm visualizing data on a map representing some values for each administrative areas of a country. I'm given a shapefile of this country, which I'm converting to GeoJSON for use with PolyMaps. Now I've got a requirement to cluster multiple areas into bigger ones, since there is no data for some of the smaller parts, but aggregated data for big ones can be acquired. Those clusters are predefined (e.g. A+B+C=D), so I figured it would be easier to produce a clustered shapefile and convert that to GeoJSON afterwards.

But how do I create such a cluster, preferably automated and with open-source tools? Any other sensible way is of course welcome too.

2 Answers 2


You can always use an open-source tool like QGIS to open your shapefile and merge features using the built-in "merge selected features" tool (just select features, click merge, select attributes to inherit to the new merged feature, done).

This of course doesn't make much sense if we're talking about hundreds or thousands of merges. I myself was wondering the other day if it is possible to merge features "by attribute" in QGIS?

EDIT: Ok, I'm stupid, if you have a common attribute for all features you're going to "cluster" (like a province or county name) this would be a case for the dissolve tool. Open your shapefile, choose dissolve (in QGIS it's located at Vector > Geoprocessing Tools > Dissolve), select the column with the above mentioned common name, and start the process. This should hopefully (if I haven't misunderstood you) result in what you were looking for.

  • In general, good advice, and if it all comes down to manual work I'll find someone who'll create those shapefiles for me with QGIS like you described. However, I'd like to be able to define those clusters (e.g. via NUTS codes: merge FR413 and FR411 into something called _FRX1) and then run a script which would generate those clustered shapefiles. I want to be able to correct errors or accomodate fast-tracked changes without the overhead of a GUI tool. It's perfectly possible that those clusters will be defined by end-users someday, so I need a bit of flexibility. Aug 10, 2011 at 12:06
  • I never use it, but I think you can use the console to do everything in QGIS, so there is no need for the GUI. You will also always have to define those clusters in some sort of table, even if you completely rely on a scripted solution. So you could avoid messing with your shapefile by joining this table and doing dissolve then. I guess this would be scriptable, too?
    – SAnderka
    Aug 10, 2011 at 12:28
  • perfectly possible, I'm a completely newbie in all things GIS. I'll look into scripting QGIS, thanks. Aug 10, 2011 at 12:44

For less manual work, you could have a look at SpatiaLite. You can import shapefiles easily using e.g.

spatialite> .loadshp ~/maps/areas areas iso-8859-15
spatialite> UPDATE areas SET Geometry = SetSrid(Geometry,4326);
spatialite> SELECT RecoverGeometryColumn('areas','Geometry',4326,'POLYGON',2);

Then for the dissolving part:

I'd like to be able to define those clusters (e.g. via NUTS codes: merge FR413 and FR411 into something called _FRX1)

The function you need here is GUnion(). You might want to define your "clusters" in a second table. Then you can join cluster and area table and use GUnion() with GROUP BY to create the final union_table.

You can then export to shapefile using .dumpshp

spatialite> .dumpshp union_table Geometry new_shapefile iso-8859-15 POLYGON

If you need GeoJSON, you can convert the file now.

You can write a short script in e.g. Python that handles all these loads, joins and dumps.

  • Looks very promising, I'll experiment with it! Thanks! Could you explain what the UPDATE and SELECT lines do? Aug 10, 2011 at 15:32

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.