forked from codeforamerica/US-Census-Area-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
55 lines (41 loc) · 1.41 KB
/
app.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
from sys import stderr
import json
from flask import Flask
from flask import request
from flask import Response
from osgeo import ogr
from geo import get_intersecting_features
from util import json_encode, bool
filenames = [
('Bay Area Census Blockgroups (2010-2013)', 'bay-area-census/bay-area.shp', None),
]
app = Flask(__name__)
@app.route('/')
def hello():
with open('index.html') as index:
return index.read()
@app.route("/areas")
def areas():
lat = float(request.args['lat'])
lon = float(request.args['lon'])
include_geom = bool(request.args.get('include_geom', True))
pretty = bool(request.args.get('pretty', False))
json_callback = request.args.get('callback', None)
# This. Is. Python.
ogr.UseExceptions()
features = []
point = ogr.Geometry(wkt='POINT(%f %f)' % (lon, lat))
args = point, include_geom
#
# Look at four files in turn
#
for (dataname, shpname, zipname) in filenames:
features += get_intersecting_features(ogr.Open(shpname), dataname, *args)
geojson = dict(type='FeatureCollection', features=features)
body, mime = json_encode(geojson, pretty=pretty), 'application/json'
if json_callback:
body = '%s(%s);\n' % (json_callback, body)
mime = 'text/javascript'
return Response(body, headers={'Content-type': mime})
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)