Skip to content

Instantly share code, notes, and snippets.

@ahmadpoorgholam
Created October 20, 2023 14:07
Show Gist options
  • Save ahmadpoorgholam/026fea985a3b814948bfa0b25247bbf2 to your computer and use it in GitHub Desktop.
Save ahmadpoorgholam/026fea985a3b814948bfa0b25247bbf2 to your computer and use it in GitHub Desktop.
convet_ploygon_to_multipolygon.py
from shapely.geometry import shape, MultiPolygon
# JSON representation of a single Polygon
polygon_json = {
"type": "Polygon",
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]
}
# Convert the JSON representation to a Shapely Polygon
polygon = shape(polygon_json)
# Convert the single Polygon to a MultiPolygon
multi_polygon = MultiPolygon([polygon])
# Get the JSON representation of the MultiPolygon
multi_polygon_json = multi_polygon.__geo_interface__
# Print the MultiPolygon JSON representation
print(multi_polygon_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment