[GIS] Python Library for converting GeoJSON Multi-polygon to polygon

geojsongeometry-conversionpolygonpython

I would like to know if there is a Python library that can be used to convert GeoJSON Multi-polygon to a Polygon.

Note: i want to convert .geojson file Multi-Polygon feature type to Polygon feature type.

Best Answer

from osgeo import ogr

str = '''
{ "type": "MultiPolygon",
    "coordinates": [
        [
            [[40, 40], [20, 45], [45, 30], [40, 40]]
        ],
        [
            [[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],
            [[30, 20], [20, 15], [20, 25], [30, 20]]
        ]
    ]
}
'''
g = ogr.CreateGeometryFromJson(str) 

print "Hi! I'm a %s with an Area  %s" % (g.GetGeometryName(), g.Area())
print "I have inside me %s feature(s)!\n" % g.GetGeometryCount()
for idx, f in enumerate(g):
    print "I'm feature n.%s and I am a %s.\t I have an Area of %s - You can get my json repr with f.ExportToJson()" % (idx, f.GetGeometryName(),f.Area())

>>> Hi! I'm a MULTIPOLYGON with an Area 712.5
>>> I have inside me 2 feature(s)!

>>> I'm feature n.0 and I am a POLYGON. I have an Area of 87.5 - You can get my json repr with f.ExportToJson()
>>> I'm feature n.1 and I am a POLYGON. I have an Area of 625.0 - You can get my json repr with f.ExportToJson()