Convert GeoJSON with multiple Polygons and MultiPolygons into single MultiPolygon?

by knutole   Last Updated November 25, 2017 05:22 AM

Is this even possible?

I have a GeoJSON file with multiple polygons and multipolygons, and I need to set styling on them (in Leaflet.js). I'd like to create a single, large MultiPolygon from the individual MultiPolygons and Polygons.

How can I do this (preferably in Node.js or at the Linux command-line)?

Things get really tricky when there's a MultiPolygon consisting of Polygons AND other Multipolygons.

Can this be "cleaned up" without any loss of information?

Answers 1

You could do this using Javascript Topology Suite which will work with Node.js. Start with an empty MultiPolygon (or the first geometry in your collection) and union this with each (Multi)Polygon in your collection. You can only have one format for the whole collection, obviously, as properties are one to one with the geometry in the GeoJSON. Here are some jsts/node.js examples.

There is also a GeoJSON parser in jsts, which will return a jsts geometry. So putting it all together, you would do something along the lines of:

var jsts = require("jsts");

var reader = new;

//read your geometries
var geoms =;

//grab the first one
var multipolygon = geoms[0];

//union with all the others
for (var x=1; x < geoms.length ; x++){
    multipolygon = multipolygon.union(geoms[x]);

This is untested, but the general idea should work. You would have to recreate the GeoJSON with properties afterwards, but this should be easy with the built in JSON methods in Javascript.

John Powell aka Barça
John Powell aka Barça
October 16, 2014 15:46 PM

Related Questions

Is it possible to convert regular JSON to GeoJSON?

Updated April 03, 2018 18:22 PM

How can I clean/fix polygons in a Node.js application?

Updated October 17, 2016 09:09 AM