Convert GeoJSON to/from WKT in Python. #python #geojson #geometry
import json | |
import geojson | |
from shapely.geometry import shape | |
o = { | |
"coordinates": [[[23.314208, 37.768469], [24.039306, 37.768469], [24.039306, 38.214372], [23.314208, 38.214372], [23.314208, 37.768469]]], | |
"type": "Polygon" | |
} | |
s = json.dumps(o) | |
# Convert to geojson.geometry.Polygon | |
g1 = geojson.loads(s) | |
# Feed to shape() to convert to shapely.geometry.polygon.Polygon | |
# This will invoke its __geo_interface__ (https://gist.github.com/sgillies/2217756) | |
g2 = shape(g1) | |
# Now it's very easy to get a WKT/WKB representation | |
g2.wkt | |
g2.wkb | |
import geojson | |
import shapely.wkt | |
s = '''POLYGON ((23.314208 37.768469, 24.039306 37.768469, 24.039306 38.214372, 23.314208 38.214372, 23.314208 37.768469))''' | |
# Convert to a shapely.geometry.polygon.Polygon object | |
g1 = shapely.wkt.loads(s) | |
g2 = geojson.Feature(geometry=g1, properties={}) | |
g2.geometry |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Actually, we just need shapely package to do everything. For import json
import shapely.wkt
import shapely.geometry
s = '''POLYGON ((23.314208 37.768469, 24.039306 37.768469, 24.039306 38.214372, 23.314208 38.214372, 23.314208 37.768469))'''
# Convert to a shapely.geometry.polygon.Polygon object
g1 = shapely.wkt.loads(s)
g2 = shapely.geometry.mapping(g1)
json.dumps(g2) |
This comment has been minimized.
This comment has been minimized.
Helpful, thank you. This can be further simplified to one line. Also note that you will have a geojson string, which can then be reinterpreted to a python dictionary
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
I believe
convert-geojson-to-wkt.py
can be simplified to: