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)),...
Best Answer
If you want to Ignore the edge cases, and get only the First (or only) Polygon, from the MultiPolygon geometry, then you need to use the
ST_GeometryN()
function.A query like the following will give you the geometry as Polygons