Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Created May 12, 2014 22:13
Show Gist options
  • Star 65 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save drmalex07/5a54fc4f1db06a66679e to your computer and use it in GitHub Desktop.
Save drmalex07/5a54fc4f1db06a66679e to your computer and use it in GitHub Desktop.
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
@alukach
Copy link

alukach commented Aug 1, 2017

I believe convert-geojson-to-wkt.py can be simplified to:

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"
}
geom = shape(o)

# Now it's very easy to get a WKT/WKB representation
geom.wkt
geom.wkb

@panchicore
Copy link

👍

@tsantanaDH
Copy link

Actually, we just need shapely package to do everything. For convert-wkt-to-geojson.py:

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)

@anottrott
Copy link

anottrott commented Apr 17, 2019

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

from shapely.wkt import loads
from shapely.geometry import mapping
import geojson
import ast

wkt_string = '''POLYGON ((23.314208 37.768469, 24.039306 37.768469, 24.039306 38.214372, 23.314208 38.214372, 23.314208 37.768469))'''

geojson_string = geojson.dumps(mapping(loads(wkt_string)))

geojson_dict = ast.literal_eval(geojson_string)

@zabop
Copy link

zabop commented Nov 3, 2021

Is there a way to read in CURVEPOLYGON as well? Some of my strings start with CURVEPOLYGON(COMPOUNDCURVE(( instead of POLYGON ((. I get the error ParseException: Unknown type: 'CURVEPOLYGON' from wkt.loads. I see it can be done using GDAL, but installation of that package is a pain.

@moosetraveller
Copy link

moosetraveller commented Sep 4, 2022

@anottrott Instead of using ast.literal_eval(geojson_string), better use json.loads(geojson_string) when converting a JSON string to a Python dictionary.

import json

from shapely.wkt import loads
from shapely.geometry import mapping
import geojson

wkt_string = "POLYGON ((23.314208 37.768469, 24.039306 37.768469, 24.039306 38.214372, 23.314208 38.214372, 23.314208 37.768469))"

geojson_string = geojson.dumps(mapping(loads(wkt_string)))

geojson_dict = json.loads(geojson_string)

print(geojson_dict)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment