This repository has been archived by the owner on Mar 24, 2019. It is now read-only.
/
manage.py
61 lines (53 loc) · 2.2 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from flask.ext.script import Shell, Server, Manager
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.sqlalchemy import SQLAlchemy
import geojson
from zipfile import ZipFile
from urllib.request import urlopen
from io import BytesIO, TextIOWrapper
from geoalchemy2.shape import from_shape
from shapely.geometry import asShape
from geoindex.factory import create_app
app = create_app()
app.debug = True
port = os.environ.get('PORT', 8000)
manager = Manager(app)
manager.add_command('server', Server(host="0.0.0.0", port=port))
from geoindex.extensions import db
from geoindex.frontend.models import *
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.command
def load_geojson():
result = urlopen('https://github.com/openregister/boundaries/archive/master.zip').read()
stream = BytesIO(result)
zipfile = ZipFile(stream, 'r')
file_names = [name for name in zipfile.namelist()
if name.endswith('.geojson')]
for name in file_names:
with zipfile.open(name, 'r') as f:
if name.endswith('.geojson'):
file_contents = TextIOWrapper(f, encoding='utf-8',
newline='')
data = geojson.loads(file_contents.read())
try:
name = data['properties']['REGD14NM']
code = data['properties']['REGD14CD']
geometry = data['geometry']
# hackery store everthing as multipolygon
if geometry['type'] == 'Polygon':
coordinates = []
coordinates.append(geometry['coordinates'])
geometry['coordinates'] = coordinates
geometry['type'] = 'MultiPolygon'
polygon = from_shape(asShape(geometry), srid=4326)
boundary = Boundary(name=name, code=code, polygon=polygon)
db.session.add(boundary)
db.session.commit()
except KeyError as e:
print("not something we were expecting really")
if __name__ == '__main__':
manager.run()