Skip to content

Instantly share code, notes, and snippets.

@zouzias
Created August 11, 2016 17:27
Show Gist options
  • Save zouzias/38469093729c91f1034fc2ed010bf39b to your computer and use it in GitHub Desktop.
Save zouzias/38469093729c91f1034fc2ed010bf39b to your computer and use it in GitHub Desktop.
Convert GeoJSON data to WKT
import json
import geojson
from shapely.geometry import shape
# Get cities from https://github.com/mahemoff/geodata/blob/master/cities.geojson
with open('cities.geojson') as json_data:
d = json.load(json_data)
for country in d['features']:
name = country['properties']['city']
id = country['id']
geo = country['geometry']
s = shape(geojson.loads(json.dumps(geo)))
wkt = s.wkt
result = '{"id": "' + id + '", "name" : "' + name + '", "shape":"' + wkt + '"}'
# print(id + ' : ' + name + ' : ' + wkt)
print(result)
import json
import geojson
from shapely.geometry import shape
# Get countries file from https://github.com/zouzias/world.geo.json/blob/master/countries.geo.json
with open('countries.geo.json') as json_data:
d = json.load(json_data)
for country in d['features']:
name = country['properties']['name']
id = country['id']
geo = country['geometry']
s = shape(geojson.loads(json.dumps(geo)))
wkt = s.wkt
result = '{"id": "' + id + '", "name" : "' + name + '", "shape":"' + wkt + '"}'
# print(id + ' : ' + name + ' : ' + wkt)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment