Commit 314c65e2863dbd23a0a2e4bc1f6ae71be4bdecc3
1 parent
23ab5119
Exists in
master
Implement Scenario C : many origins, many destinations
Showing
1 changed file
with
38 additions
and
13 deletions
Show diff stats
flaskr/controllers/main_controller.py
1 | 1 | import importlib |
2 | +import geopy | |
3 | +import sqlalchemy | |
2 | 4 | |
3 | 5 | from flask import Blueprint, render_template, flash, request, redirect, \ |
4 | 6 | url_for, abort |
... | ... | @@ -14,8 +16,6 @@ from flaskr.content import content |
14 | 16 | # from io import StringIO |
15 | 17 | from yaml import safe_dump as yaml_dump |
16 | 18 | |
17 | -import sqlalchemy | |
18 | -import geopy | |
19 | 19 | |
20 | 20 | main = Blueprint('main', __name__) |
21 | 21 | |
... | ... | @@ -144,6 +144,8 @@ def compute(): # process the queue of estimation requests |
144 | 144 | _handle_failure(estimation, response) |
145 | 145 | return _respond(response) |
146 | 146 | |
147 | + print(repr(destination.raw)) | |
148 | + | |
147 | 149 | destinations.append(destination) |
148 | 150 | |
149 | 151 | response += u"Destination: %s == %s (%f, %f)\n" % ( |
... | ... | @@ -184,6 +186,16 @@ def compute(): # process the queue of estimation requests |
184 | 186 | |
185 | 187 | # UTILITY PRIVATE FUNCTION(S) ############################################# |
186 | 188 | |
189 | + def get_city_key(_location): | |
190 | + _city_key = _location.address | |
191 | + # if 'address100' in _location.raw['address']: | |
192 | + # _city_key = _location.raw['address']['address100'] | |
193 | + if 'city' in _location.raw['address']: | |
194 | + _city_key = _location.raw['address']['city'] | |
195 | + elif 'state' in _location.raw['address']: | |
196 | + _city_key = _location.raw['address']['state'] | |
197 | + return _city_key | |
198 | + | |
187 | 199 | def compute_one_to_many( |
188 | 200 | _origin, |
189 | 201 | _destinations, |
... | ... | @@ -203,15 +215,7 @@ def compute(): # process the queue of estimation requests |
203 | 215 | destination_longitude=_destination.longitude, |
204 | 216 | ) |
205 | 217 | |
206 | - # print(repr(_destination.raw)) | |
207 | - | |
208 | - city_key = _destination.address | |
209 | - if 'address100' in _destination.raw['address']: | |
210 | - city_key = _destination.raw['address']['address100'] | |
211 | - elif 'city' in _destination.raw['address']: | |
212 | - city_key = _destination.raw['address']['city'] | |
213 | - elif 'state' in _destination.raw['address']: | |
214 | - city_key = _destination.raw['address']['state'] | |
218 | + city_key = get_city_key(_destination) | |
215 | 219 | |
216 | 220 | if city_key not in cities: |
217 | 221 | cities[city_key] = 0.0 |
... | ... | @@ -226,14 +230,20 @@ def compute(): # process the queue of estimation requests |
226 | 230 | |
227 | 231 | _results['footprints'] = footprints |
228 | 232 | |
233 | + total = 0.0 | |
234 | + | |
229 | 235 | cities_mean = {} |
230 | 236 | for city in cities_sum.keys(): |
231 | - cities_mean[city] = 1.0 * cities_sum[city] / len(emission_models) | |
237 | + city_mean = 1.0 * cities_sum[city] / len(emission_models) | |
238 | + cities_mean[city] = city_mean | |
239 | + total += city_mean | |
232 | 240 | |
233 | 241 | _results['mean_footprint'] = { |
234 | 242 | 'cities': cities_mean |
235 | 243 | } |
236 | 244 | |
245 | + _results['total'] = total | |
246 | + | |
237 | 247 | return _results |
238 | 248 | |
239 | 249 | # SCENARIO A : One Origin, At Least One Destination ####################### |
... | ... | @@ -264,7 +274,21 @@ def compute(): # process the queue of estimation requests |
264 | 274 | # Run Scenario A for each Destination, and expose optimum Destination. |
265 | 275 | # |
266 | 276 | else: |
267 | - pass | |
277 | + results = { | |
278 | + 'cities': [], | |
279 | + } | |
280 | + for destination in destinations: | |
281 | + city_key = get_city_key(destination) | |
282 | + | |
283 | + city_results = compute_one_to_many( | |
284 | + _origin=destinations[0], | |
285 | + _destinations=origins, | |
286 | + use_train_below=0, | |
287 | + ) | |
288 | + city_results['city'] = city_key | |
289 | + results['cities'].append(city_results) | |
290 | + | |
291 | + # Todo: sort cities, and perhaps extract optimum | |
268 | 292 | |
269 | 293 | # WRITE RESULTS INTO THE DATABASE ######################################### |
270 | 294 | |
... | ... | @@ -296,6 +320,7 @@ def consult_estimation(public_id, format): |
296 | 320 | # abort(404) |
297 | 321 | |
298 | 322 | if 'html' == format: |
323 | + | |
299 | 324 | if estimation.status in [StatusEnum.pending]: |
300 | 325 | return render_template( |
301 | 326 | "estimation-queue-wait.html", | ... | ... |