-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
122 lines (82 loc) · 2.97 KB
/
wsgi.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from __future__ import print_function
import os
import csv
from flask import Flask, request
from flask_restful import Resource, Api
from pymongo import MongoClient, GEO2D
DB_HOST = os.environ.get('DB_HOST', 'mongodb')
DB_NAME = os.environ.get('DB_NAME', 'mongodb')
DB_USERNAME = os.environ.get('DB_USERNAME', 'mongodb')
DB_PASSWORD = os.environ.get('DB_PASSWORD', 'mongodb')
DB_URI = 'mongodb://%s:%s@%s:27017/%s' % (DB_USERNAME, DB_PASSWORD,
DB_HOST, DB_NAME)
DATASET_FILE = 'ToiletmapExport_161101_090000.csv'
application = Flask(__name__)
api = Api(application)
class HealthCheck(Resource):
def get(self):
return 'OK'
api.add_resource(HealthCheck, '/ws/healthz/')
class Info(Resource):
description = {
"id": "aussiedunnies",
"displayName": "Aussie Dunnies",
"type": "cluster",
#"scope": "within",
"center": {"latitude": "-33.87310577115773", "longitude": "151.19792461395264"},
"zoom": 15
}
def get(self):
return self.description
api.add_resource(Info, '/ws/info/')
class DataLoad(Resource):
def get(self):
client = MongoClient(DB_URI)
database = client[DB_NAME]
collection = database.aussiedunnies
collection.remove({})
collection.create_index([('Location', GEO2D)])
with open(DATASET_FILE, 'rb') as csvfile:
reader = csv.reader(csvfile)
headers = reader.next()
entries = []
for row in reader:
entry = dict(zip(headers, row))
loc = [float(entry['Longitude']), float(entry['Latitude'])]
entry['Location'] = loc
entries.append(entry)
if len(entries) >= 1000:
collection.insert_many(entries)
entries = []
if entries:
collection.insert_many(entries)
return 'Inserted %s items.' % collection.count()
api.add_resource(DataLoad, '/ws/data/load')
def format_result(entries):
result = []
for entry in entries:
data = {}
data['id'] = entry['ToiletID']
data['latitude'] = entry['Latitude']
data['longitude'] = entry['Longitude']
data['name'] = entry['Name']
result.append(data)
return result
class DataAll(Resource):
def get(self):
client = MongoClient(DB_URI)
database = client[DB_NAME]
collection = database.aussiedunnies
return format_result(collection.find())
api.add_resource(DataAll, '/ws/data/all')
class DataWithin(Resource):
def get(self):
args = request.args
box = [[float(args['lon1']), float(args['lat1'])],
[float(args['lon2']), float(args['lat2'])]]
query = {"Location": {"$within": {"$box": box}}}
client = MongoClient(DB_URI)
database = client[DB_NAME]
collection = database.aussiedunnies
return format_result(collection.find(query))
api.add_resource(DataWithin, '/ws/data/within')