Skip to content

Instantly share code, notes, and snippets.

@andrewgleave
Last active August 29, 2015 14:15
Show Gist options
  • Save andrewgleave/e10faec6ef30aa880b5e to your computer and use it in GitHub Desktop.
Save andrewgleave/e10faec6ef30aa880b5e to your computer and use it in GitHub Desktop.
Snippet to clean up and push KMZ->JSON data to a CouchDB instance. Routes are converted to LineStrings.
import ijson
from couchdbkit import (Document, Server, StringProperty, DictProperty)
class Route(Document):
id = StringProperty()
geometry = DictProperty()
properties = DictProperty()
def main():
unnamed_count = 0
server = Server('https://xxxxxxxxx:[email protected]')
db = server.get_or_create_db('iom-walking-routes')
Route.set_db(db)
with open('routes.json') as file:
for feature_list in ijson.items(file, 'features'):
for route in feature_list:
name = route['properties'].get('Name')
if name in (None, 'NULL'):
name = 'Unnamed Route %d' % (unnamed_count,)
unnamed_count += 1
geometry = {
'type': 'LineString',
'coordinates': [[float(y) for y in x if y != 0.0] for x in route['geometry']['coordinates'][0]]
}
route = Route(id=name, geometry=geometry, properties=route['properties'])
route.save()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment