2

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?

5
  • Show a sample of such GeoJSON file and a sketch about how that "large Polygon" would look like. It feels as impossible task but perhaps I just don't understand. How about one GeometryCollection or perhaps one MultiPolygon? Is is so that your features do not have any meaningful attributes so you can happily combine everything into one single feature?
    – user30184
    Oct 16, 2014 at 12:51
  • 1
    The rules for MultiPolygons are that the constituent polygons do not intersect each other. So, if your MultiPolygon is valid, it is not possible to do what you are asking. Oct 16, 2014 at 12:58
  • @user30184 Yeah, one MultiPolygon is what I mean.
    – knutole
    Oct 16, 2014 at 12:59
  • Please edit the question, then. Oct 16, 2014 at 13:00
  • 2
    You could do this using Javascript Topology Suite which will work with Node.js. Start with an empty MultiPolgyon 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 examples to get you started. Oct 16, 2014 at 13:04

1 Answer 1

3

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 jsts.io.GeoJSONParser();

//read your geometries
var geoms = reader.read(geojson);

//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.

2
  • Ok, thanks, looks interesting. Also stumbled across mapshaper -dissolve
    – knutole
    Oct 17, 2014 at 0:06
  • 1
    Yes, there is also ST_Union in Postgres, which you could do on a whole collection at once, followed by Select as ST_GeoJSON. You could also do it with Shapely, etc, there are many ways to skin this cat. But you asked about node.js :D Oct 17, 2014 at 8:23

Your Answer

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

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