-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
259 lines (212 loc) · 8.76 KB
/
views.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
import json
from django.db import connection
from django.urls import reverse_lazy
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.views.generic import TemplateView, CreateView, UpdateView, ListView, DeleteView
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework.exceptions import ParseError
from mbm import forms
from mbm.models import MellowRoute, fetchall
class Home(TemplateView):
title = 'Home'
template_name = 'mbm/index.html'
class About(TemplateView):
title = 'About'
template_name = 'mbm/about.html'
class RouteList(APIView):
renderer_classes = [JSONRenderer]
def get(self, request):
return Response(MellowRoute.all())
class Route(APIView):
renderer_classes = [JSONRenderer]
def get(self, request):
source_coord = self.get_coord_from_request(request, 'source')
source_vertex_id = self.get_nearest_vertex_id(source_coord)
target_coord = self.get_coord_from_request(request, 'target')
target_vertex_id = self.get_nearest_vertex_id(target_coord)
return Response({
'source': source_coord,
'target': target_coord,
'source_vertex_id': source_vertex_id,
'target_vertex_id': target_vertex_id,
'route': self.get_route(source_vertex_id, target_vertex_id)
})
def get_coord_from_request(self, request, key):
try:
coord = request.GET[key]
except KeyError:
raise ParseError('Request is missing required key: %s' % key)
coord_parts = coord.split(',')
try:
assert len(coord_parts) == 2
float(coord_parts[0]), float(coord_parts[1])
except (AssertionError, TypeError):
raise ParseError(
"Request argument '%s' must be a coordinate of the form lng,lat" % key
)
return coord_parts
def get_nearest_vertex_id(self, coord):
with connection.cursor() as cursor:
cursor.execute("""
SELECT vert.id
FROM chicago_ways_vertices_pgr AS vert
ORDER BY vert.the_geom <-> ST_SetSRID(
ST_MakePoint(%s, %s),
4326
)
LIMIT 1
""", [coord[1], coord[0]]) # ST_MakePoint() expects lng,lat
rows = fetchall(cursor)
if rows:
return rows[0]['id']
else:
raise ParseError('No vertex found near point %s' % ','.join(coord))
def get_route(self, source_vertex_id, target_vertex_id):
with connection.cursor() as cursor:
cursor.execute("""
SELECT
way.name, ST_AsGeoJSON(way.the_geom) AS geometry, mellow.type
FROM pgr_dijkstra(
'WITH mellow AS (
SELECT DISTINCT(UNNEST(ways)) AS osm_id, type
FROM mbm_mellowroute
)
SELECT
way.gid AS id,
way.source,
way.target,
CASE
WHEN mellow.type = ''path'' THEN way.cost * 0.1
WHEN mellow.type = ''street'' THEN way.cost * 0.25
WHEN way.oneway = ''YES'' THEN way.cost * 0.5
WHEN mellow.type = ''route'' THEN way.cost * 0.75
ELSE way.cost
END AS cost,
CASE
WHEN mellow.type = ''path'' THEN way.reverse_cost * 0.1
WHEN mellow.type = ''street'' THEN way.reverse_cost * 0.25
WHEN way.oneway = ''YES'' THEN way.reverse_cost * 0.5
WHEN mellow.type = ''route'' THEN way.reverse_cost * 0.75
ELSE way.reverse_cost
END AS reverse_cost
FROM chicago_ways AS way
LEFT JOIN mellow
USING(osm_id)
',
%s,
%s
) AS path
JOIN chicago_ways AS way
ON path.edge = way.gid
LEFT JOIN (
SELECT UNNEST(ways) AS osm_id, type
FROM mbm_mellowroute
) as mellow
USING(osm_id)
""", [source_vertex_id, target_vertex_id])
rows = fetchall(cursor)
return {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': json.loads(row['geometry']),
'properties': {
'name': row['name'],
'type': row['type']
}
}
for row in rows
]
}
class MellowRouteList(LoginRequiredMixin, ListView):
title = 'Neighborhoods'
model = MellowRoute
queryset = MellowRoute.objects.values('name', 'slug').distinct('name', 'slug')
template_name = 'mbm/mellow_route_list.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['neighborhoods'] = json.dumps({
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': json.loads(way.bounding_box.json),
'properties': {
'name': way.name
}
}
for way in self.model.objects.all()
]
})
return context
class MellowRouteCreate(LoginRequiredMixin, CreateView):
title = 'Create Neighborhood'
template_name = 'mbm/mellow_route_create.html'
form_class = forms.MellowRouteCreateForm
model = MellowRoute
success_url = reverse_lazy('mellow-route-list')
def form_valid(self, form):
messages.success(self.request, 'Neighborhood created.')
return super().form_valid(form)
class MellowRouteEdit(LoginRequiredMixin, UpdateView):
title = 'Edit Neighborhood'
template_name = 'mbm/mellow_route_edit.html'
form_class = forms.MellowRouteEditForm
model = MellowRoute
success_url = reverse_lazy('mellow-route-list')
def get_object(self):
return self.model.objects.get(
slug=self.kwargs['slug'],
type=self.kwargs['type']
)
def form_valid(self, form):
messages.success(self.request, 'Neighborhood updated.')
return super().form_valid(form)
class MellowRouteNeighborhoodEdit(LoginRequiredMixin, UpdateView):
title = 'Edit Neighborhood'
template_name = 'mbm/mellow_route_edit.html'
form_class = forms.MellowRouteNeighborhoodEditForm
model = MellowRoute
success_url = reverse_lazy('mellow-route-list')
def get_object(self):
# Get first object, since we don't care about the type
return self.model.objects.filter(slug=self.kwargs['slug']).first()
def form_valid(self, form):
# Save the data for all MellowRoute types
self.model.objects.filter(slug=self.kwargs['slug']).update(
name=form.instance.name,
slug=form.instance.slug,
bounding_box=form.instance.bounding_box
)
return HttpResponseRedirect(self.success_url)
class MellowRouteDelete(LoginRequiredMixin, DeleteView):
title = 'Delete Neighborhood'
template_name = 'mbm/mellow_route_confirm_delete.html'
model = MellowRoute
success_url = reverse_lazy('mellow-route-list')
def get_object(self):
# We don't use the object type in the view, so just return the first
# match on the slug
return self.model.objects.filter(slug=self.kwargs['slug']).first()
def delete(self, request, *args, **kwargs):
# Delete all MellowRoutes with this slug, no matter the type
self.model.objects.filter(slug=self.kwargs['slug']).delete()
messages.success(self.request, 'Neighborhood deleted.')
return HttpResponseRedirect(self.success_url)
def page_not_found(request, exception, template_name='mbm/404.html'):
return render(request, template_name, status=404)
def server_error(request, template_name='mbm/500.html'):
return render(request, template_name, status=500)
def pong(request):
from django.http import HttpResponse
try:
from .deployment import DEPLOYMENT_ID
except ImportError as e:
return HttpResponse('Bad deployment: {}'.format(e), status=401)
return HttpResponse(DEPLOYMENT_ID)