If you have a simple Multipolygon such as the one below,
mp=
{
"type": "MultiPolygon",
"coordinates": [
[
[
[-99.028, 46.985], [-99.028, 50.979],
[-82.062, 50.979], [-82.062, 47.002],
[-99.028, 46.985]
]
],
[
[
[-109.028, 36.985], [-109.028, 40.979],
[-102.062, 40.979], [-102.062, 37.002],
[-109.028, 36.985]
]
]
]
}
then using Javascript/Nodejs you can access each constituent Polygon using forEach, and write out a new Polygon using JSON.stringify
mp.coordinates.forEach(function(coords){
var feat={'type':'Polygon','coordinates':coords};
console.log(JSON.stringify(feat));
}
);
You could also access them directly in a loop, if you prefer a less functional way, indexed on mp.coordinates.length eg,
for (var i=0;i<mp.coordinates.length;i++){
var feat={'type':'Polygon','coordinates':mp.coordinates[i]};
console.log(JSON.stringify(feat));
}
If you are dealing with a FeatureCollection, where you might have an array of feature, each containing a MultiPolygon, eg,
mp = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[[
[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0],
[100.0, 0.0]
]],
[[
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0],
[0.0, 0.0]
]]
]
},
"properties": {
"prop1": {
"this": "that"
},
"prop0": "value0"
}
}
]
}
Then, you can use forEach to get to each feature, and then access each Polygon within each Multipolygon simply by looping through the array, as the first dimension of the coordinates array, is the index into each Polygon. Note, you can also save the properties, and assign them to each new Polygon feature.
mp.features.forEach(function(feat){
var geom=feat.geometry;
var props=feat.properties;
if (geom.type === 'MultiPolygon'){
for (var i=0; i < geom.coordinates.length; i++){
var polygon = {
'type':'Polygon',
'coordinates':geom.coordinates[i],
'properties': props};
console.log(JSON.stringify(polygon));
}
}
});
If you want something more sophisticated, you could look into modifying the OpenLayers.Format.GeoJSON class.
That's easy to do with ogr2ogr http://www.gdal.org/ogr2ogr.html and GDAL SQLite dialect http://www.gdal.org/ogr_sql_sqlite.html.
An example using your sample data:
ogr2ogr -f "GeoJSON" -dialect sqlite -sql "select st_union(geometry) as geometry from OGRGeoJSON where string in ('1','2')" gj_union_test.json geojsontest.json
Check the result with ogrinfo:
ogrinfo gj_union_test.json -al -so
INFO: Open of `gj_union_test.json'
using driver `GeoJSON' successful.
Layer name: OGRGeoJSON
Geometry: Multi Polygon
Feature Count: 1
Extent: (50600.010000, 301849.995000) - (653900.010000, 576205.560000)
Layer SRS WKT:
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]]
As you can see there is now only one MultiPolygon feature. Another thing to notice is that if your GeoJSON don't use WGS84 coordinates you should add the CRS object http://geojson.org/geojson-spec.html#coordinate-reference-system-objects.
Best Answer
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:
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.