4

Anyone knows if is there any way to export a polygon layer in geojson, as multipolygon?

I have a multipolygon shapefile. But there are single and multi polygons.

When I export it to geojson, I got both structures: Polygons with single feature, are exported as polygons (with 3 arrays)

 "type": "Polygon",
   "coordinates": [
       [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
       [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
   ]

And some polygons formed by several features, are exported as multipolygon, with four arrays:

 "type": "MultiPolygon",
   "coordinates": [
       [
           [ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0] ]
       ],
       [
           [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
           [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
       ]
   ]

What I need is to get a geojson that shows polygons and multipolygons with the second structure (four arrays)

Anyone could tell me if it is possible, or any clue on how to do it?

3

You can convert all your data into multipolygons with ogr2ogr by defining "new layer type" with -nlt MULTIPOLYGON. However, I am not sure if the result is exactly what you believe when multipolygon has only one part.

This is a triangle as polygon

{
"type": "FeatureCollection",

"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 95.0, 216.0 ], [ 241.0, 253.0 ], [ 175.0, 138.0 ], [ 95.0, 216.0 ] ] ] } }
]
}

This is the same triangle as multipolygon

{
"type": "FeatureCollection",

"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 95.0, 216.0 ], [ 241.0, 253.0 ], [ 175.0, 138.0 ], [ 95.0, 216.0 ] ] ] ] } }
]
}

EDIT

However, if the aim is to combine all the separate features, both polygons and multipolygons into one feature which is of type multipolygon, you must build an union. If your mixture of polygons and multipolygons is in file "mix.shp" the command to use is:

ogr2ogr -f geojson union.json mix.shp -dialect sqlite -sql "select st_union(geometry) from mix"

The result "union.json " contains one multipolygon as can be seen from the ogr2ogr report:

Layer name: OGRGeoJSON
Geometry: Multi Polygon
Feature Count: 1
...
OGRFeature(OGRGeoJSON):0
  MULTIPOLYGON (((125 229,185 289,252 234,208 169,125 229)),...
  • I've found this option: "PROMOTE_TO_MULTI can be used to automatically promote layers that mix polygon or multipolygons to multipolygons" But got an error with this command line: ogr2ogr ogr2ogr -f "GeoJSON" newfile.geojson oldfile.geojson -nlt PROMOTE_TO_MULTI Would be this ok, wouldn't? – Pablo Pardo Sep 16 '14 at 13:09
  • Dumb error, I was writing ogr2ogr twice :( It runs perfect – Pablo Pardo Sep 16 '14 at 13:17
2

If you are exporting your geojson from postgis, you can use the ST_MULTI function to ensure that the feature will be a MULTIPOLYGON if it's a polygon, a MULTILINESTRING if its a linestring or a MULTIPOINT if its a point

SELECT ST_AsGeoJSON(ST_Multi(geom))::json FROM table WHERE id = 0;

Edit: the technique to output a featureCollection in geoson from postgis is a bit "touchy":

SELECT row_to_json(fc)
 FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
 FROM (SELECT 'Feature' As type
    , ST_AsGeoJSON(ST_Multi(lg.geom))::json As geometry
    , row_to_json((field1, field2, fieldx)) As properties
   FROM table As lg   ) As f )  As fc;

see that we use ST_MULTI to ensure that each feature would be a MULTIPOLYGON. You have to adapt the table name and the fields names in the query.

  • That's a good point too. But my dount is: how could I group polygons to multipolygons? SELECT ST_Multi(geom), id from table group by geom, id does not work, as I obtain eparate polygons again. Should I add a st_union or other function to get polygons group by id? (id is not unique) – Pablo Pardo Sep 16 '14 at 13:35
  • you want a featureCollection made of multipolygons as output or a multipolygon that groups all features of the table? – Below the Radar Sep 16 '14 at 13:42
  • A feature collection made of multipilygons. But some multipoplygons are one polygon, and other needs to be grouped by its Id – Pablo Pardo Sep 16 '14 at 13:49
  • ok see my edit for that – Below the Radar Sep 16 '14 at 13:57
  • I guess now that you want to get only one multipolygon that holds all your original geometries. See my edited answer. If this is again a wrong guess, edit your question to be more clear. – user30184 Sep 16 '14 at 14:07

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.