/
api.py
298 lines (221 loc) · 7.38 KB
/
api.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import json
import random
import os
import os.path
from flask_cors import CORS
import psycopg2
from werkzeug.utils import secure_filename
from flask import Flask
from flask import request
from flask import Response
from flask import jsonify, redirect
from flask_spyne import Spyne
from spyne.protocol.http import HttpRpc
from spyne.protocol.soap import Soap11
from spyne.protocol.json import JsonDocument
from spyne.model.primitive import Unicode, Integer
from spyne.model.complex import Iterable
from spyne.model.fault import Fault
from spyne import Application, rpc, ServiceBase, \
Integer, Unicode, Boolean
from spyne.model.complex import ComplexModel
from spyne import Iterable
from spyne.protocol.json import JsonDocument
from spyne.server.wsgi import WsgiApplication
import parsexml
app = Flask(__name__)
CORS(app)
connection = psycopg2.connect(database="main", user="main", password="foobar")
cursor = connection.cursor()
def get_file(filename): # pragma: no cover
try:
src = os.path.join("./static/", filename)
# Figure out how flask returns static files
# Tried:
# - render_template
# - send_file
# This should not be so non-obvious
return open(src).read()
except IOError as exc:
return str(exc)
@app.route('/', methods=['GET'])
def index(): # pragma: no cover
content = get_file('index.html')
return Response(content, mimetype="text/html")
@app.route('/googleTest.js', methods=['GET'])
def jsfile(): # pragma: no cover
content = get_file('googleTest.js')
return Response(content, mimetype="application/javascript")
@app.route('/trash.png', methods=['GET'])
def trashfile(): # pragma: no cover
content = get_file('trash.png')
return Response(content, mimetype="image/png")
upload_folder = "./uploads/"
ALLOWED_EXTENSIONS = ['gpx']
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/path', methods=['POST'])
def upload_file():
# check if the post request has the file part
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(upload_folder, filename)
file.save(filepath)
res = parsexml.parse(open(filepath))
print "Got %d points" % (len(res),)
parsexml.insertpath(res, connection, cursor)
return redirect("/")
return 'failed'
@app.route('/litter', methods=['PUT'])
def putLitter():
lat = request.args.get('lat')
lon = request.args.get('lon')
sql = "insert into litter values (now(), ST_MakePoint(%s, %s))"
cursor.execute(sql, (lon, lat))
# find polygon for point
sql = "select gid from bins, litter where ST_contains(bins.the_geom, ST_SetSRID(ST_MakePoint(%s, %s), 4326))"
cursor.execute(sql, (lon, lat))
if cursor.description != None:
x = cursor.fetchall()
print "Found bins: %s" % (x,)
gid = x[0][0]
sql = "select count(*) from bins, litter where gid = %s and ST_Contains(bins.the_geom, litter.location::geometry)"
cursor.execute(sql, (gid,))
x = cursor.fetchall()
count = x[0][0]
sql = "update bins set dirtiness = %s where gid = %s"
cursor.execute(sql, (count, gid))
else:
# no bin found
pass
connection.commit()
result = {'result': 'ok'}
resp = jsonify(result)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/litter', methods=['OPTIONS'])
def optionsLitter():
resp = Response()
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/litter', methods=['GET'])
def getLitter():
latmin = request.args.get('latmin', None)
latmax = request.args.get('latmax', None)
lonmin = request.args.get('lonmin', None)
lonmax = request.args.get('lonmax', None)
if latmin == None or latmax == None or lonmin == None or lonmax == None:
return jsonify({'result': 'missing parameters'})
sql = "select extract(epoch from(reportdate)), st_x(st_asewkt(location)), st_y(st_asewkt(location)) from litter where location && ST_MakeEnvelope(%s, %s, %s, %s, 4326)"
cursor.execute(sql, (latmin, latmax, lonmin, lonmax))
if cursor.description != None:
x = cursor.fetchall()
else:
x = []
geo = makeLitterGeoJSON(x)
connection.commit()
resp = jsonify(geo)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/bins', methods=['GET'])
def getBins():
latmin = request.args.get('latmin', None)
latmax = request.args.get('latmax', None)
lonmin = request.args.get('lonmin', None)
lonmax = request.args.get('lonmax', None)
#if latmin == None or latmax == None or lonmin == None or lonmax == None:
# return jsonify({'result': 'missing parameters'})
#sql = "select ST_AsGeoJSON(ST_Scale(ST_Translate(the_geom, 0.0, 20), 1.0, 0.7)) from hex_grid"
#sql = "select ST_AsGeoJSON(ST_translate(ST_Scale(the_geom, 1.0, 0.4), 0.0, 38.46)) from hex_grid"
sql = "select ST_AsGeoJSON(the_geom), dirtiness, walks from bins"
cursor.execute(sql)
x = cursor.fetchall()
geo = makeBinsGeoJSON(x)
connection.commit()
resp = jsonify(geo)
return resp
def makeLitterGeoJSON(points):
res = []
for i in points:
data = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [i[1], i[2]]
},
"properties": {
"name": "Litter"
}
}
res.append(data)
return {
"type": "FeatureCollection",
"features": res
}
bin_dirty_palette = {
1: '#ffb3b3',
# 2: '#ff9999',
2: '#ff8080',
# 4: '#ff6666',
3: '#ff4d4d',
# 6: '#ff3333',
4: '#ff1a1a',
5: '#ff0000',
}
bin_walks_palette = {
1: '#b3ff99',
2: '#9fff80',
3: '#8cff66',
4: '#79ff4d',
5: '#66ff33',
}
def makeBinsGeoJSON(thelist):
res = []
for i in thelist:
igeo = json.loads(i[0])
dirtiness = i[1]
walks = i[2]
if dirtiness == 0 and walks == 0:
continue
#red = 0#min(dirtiness*30, 255) #random.randint(0, 255)
#green = 0#255-red # random.randint(0, 255)
#blue = 0#random.randint(0, 255)
opacity = 0.0
if dirtiness > 0:
color = bin_dirty_palette.get(dirtiness, "#ff0000") #min(dirtiness*50, 255)
opacity = 0.6
else:
#green = min(walks*50, 255)
color = bin_walks_palette.get(walks, '#66ff33')
opacity = 0.8
data = {
'type': 'Feature',
'geometry': igeo,
"properties": {
'fill': color, #"#%02X%02X%02X" % (red, green, blue),
'opacity': opacity,
}
}
res.append(data)
return {
"type": "FeatureCollection",
"features": res
}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, threaded=False)
"""
spyne = Spyne(app)
@spyne.srpc(Unicode, _returns=Unicode)
def newLitter(username):
if x == None:
raise Fault(faultcode='Client', faultstring='Account not found', detail={'uid': username})
return "ok"
"""