Commit b935618e6f8c308b041b1470506194ec66d73de5

Authored by Antoine Goutenoir
1 parent 13c292f1
Exists in master

Count the number of trips by plane and train.

flaskr/controllers/main_controller.py
  1 +from copy import deepcopy
  2 +
1 3 import geopy
2 4 import sqlalchemy
3 5  
... ... @@ -236,7 +238,9 @@ def compute(): # process the queue of estimation requests
236 238 emission_models = estimation.get_models()
237 239 # print(emission_models)
238 240  
239   - extra_config = {}
  241 + extra_config = {
  242 + 'use_train_below_distance': 300,
  243 + }
240 244  
241 245 # PREPARE RESULT DICTIONARY THAT WILL BE STORED ###########################
242 246  
... ... @@ -269,6 +273,7 @@ def compute(): # process the queue of estimation requests
269 273  
270 274 cities_sum_foot = {}
271 275 cities_sum_dist = {}
  276 + cities_dict_first_model = None
272 277 for model in emission_models:
273 278 cities_dict = {}
274 279 for _destination in _destinations:
... ... @@ -280,50 +285,64 @@ def compute(): # process the queue of estimation requests
280 285 extra_config=_extra_config,
281 286 )
282 287  
283   - city_key = get_city_key(_destination)
  288 + _key = get_city_key(_destination)
284 289  
285   - destinations_by_city_key[city_key] = _destination
  290 + destinations_by_city_key[_key] = _destination
286 291  
287   - if city_key not in cities_dict:
288   - cities_dict[city_key] = {
289   - 'city': city_key,
  292 + if _key not in cities_dict:
  293 + cities_dict[_key] = {
  294 + 'city': _key,
290 295 'address': _destination.address,
291 296 'footprint': 0.0,
292 297 'distance': 0.0,
  298 + 'train_trips': 0,
  299 + 'plane_trips': 0,
293 300 }
294   - cities_dict[city_key]['footprint'] += footprint['co2eq_kg']
295   - cities_dict[city_key]['distance'] += footprint['distance']
296   - if city_key not in cities_sum_foot:
297   - cities_sum_foot[city_key] = 0.0
298   - cities_sum_foot[city_key] += footprint['co2eq_kg']
299   - if city_key not in cities_sum_dist:
300   - cities_sum_dist[city_key] = 0.0
301   - cities_sum_dist[city_key] += footprint['distance']
302   -
303   - cities = [cities_dict[k] for k in cities_dict.keys()]
304   - cities = sorted(cities, key=lambda c: c['footprint'])
  301 + cities_dict[_key]['footprint'] += footprint['co2eq_kg']
  302 + cities_dict[_key]['distance'] += footprint['distance_km']
  303 + cities_dict[_key]['train_trips'] += footprint['train_trips']
  304 + cities_dict[_key]['plane_trips'] += footprint['plane_trips']
  305 + if _key not in cities_sum_foot:
  306 + cities_sum_foot[_key] = 0.0
  307 + cities_sum_foot[_key] += footprint['co2eq_kg']
  308 + if _key not in cities_sum_dist:
  309 + cities_sum_dist[_key] = 0.0
  310 + cities_sum_dist[_key] += footprint['distance_km']
  311 +
  312 + cities = sorted(cities_dict.values(), key=lambda c: c['footprint'])
305 313  
306 314 footprints[model.slug] = {
307 315 'cities': cities,
308 316 }
309 317  
  318 + if cities_dict_first_model is None:
  319 + cities_dict_first_model = deepcopy(cities_dict)
  320 +
310 321 _results['footprints'] = footprints
311 322  
312 323 total_foot = 0.0
313 324 total_dist = 0.0
  325 + total_train_trips = 0
  326 + total_plane_trips = 0
314 327  
315 328 cities_mean_dict = {}
316 329 for city in cities_sum_foot.keys():
317 330 city_mean_foot = 1.0 * cities_sum_foot[city] / len(emission_models)
318 331 city_mean_dist = 1.0 * cities_sum_dist[city] / len(emission_models)
  332 + city_train_trips = cities_dict_first_model[city]['train_trips']
  333 + city_plane_trips = cities_dict_first_model[city]['plane_trips']
319 334 cities_mean_dict[city] = {
320 335 'address': destinations_by_city_key[city].address,
321 336 'city': city,
322 337 'footprint': city_mean_foot,
323 338 'distance': city_mean_dist,
  339 + 'train_trips': city_train_trips,
  340 + 'plane_trips': city_plane_trips,
324 341 }
325 342 total_foot += city_mean_foot
326 343 total_dist += city_mean_dist
  344 + total_train_trips += city_train_trips
  345 + total_plane_trips += city_plane_trips
327 346  
328 347 cities_mean = [cities_mean_dict[k] for k in cities_mean_dict.keys()]
329 348 cities_mean = sorted(cities_mean, key=lambda c: c['footprint'])
... ... @@ -337,6 +356,8 @@ def compute(): # process the queue of estimation requests
337 356 _results['footprint'] = total_foot
338 357  
339 358 _results['distance'] = total_dist
  359 + _results['train_trips'] = total_train_trips
  360 + _results['plane_trips'] = total_plane_trips
340 361  
341 362 return _results
342 363  
... ... @@ -387,7 +408,7 @@ def compute(): # process the queue of estimation requests
387 408 city_results['address'] = destination.address
388 409 result_cities.append(city_results)
389 410  
390   - result_cities = sorted(result_cities, key=lambda c: int(c['total']))
  411 + result_cities = sorted(result_cities, key=lambda c: int(c['footprint']))
391 412 results = {
392 413 'cities': result_cities,
393 414 }
... ... @@ -451,7 +472,11 @@ def consult_estimation(public_id, extension):
451 472  
452 473 si = StringIO()
453 474 cw = csv.writer(si, quoting=csv.QUOTE_ALL)
454   - cw.writerow([u"city", u"address", u"co2 (kg)", u"distance (km)"])
  475 + cw.writerow([
  476 + u"city", u"address",
  477 + u"co2 (kg)", u"distance (km)",
  478 + u"plane trips", u'train trips',
  479 + ])
455 480  
456 481 results = estimation.get_output_dict()
457 482 for city in results['cities']:
... ... @@ -460,6 +485,8 @@ def consult_estimation(public_id, extension):
460 485 city['address'].encode(OUT_ENCODING),
461 486 round(city['footprint'], 3),
462 487 round(city['distance'], 3),
  488 + city['plane_trips'],
  489 + city['train_trips'],
463 490 ])
464 491  
465 492 # return si.getvalue().strip('\r\n')
... ...
flaskr/laws/travel_emission_linear_fit.py
... ... @@ -45,6 +45,14 @@ class EmissionModel(BaseEmissionModel):
45 45 destination_latitude=destination_airport.latitude,
46 46 destination_longitude=destination_airport.longitude,
47 47 )
  48 + distance += great_circle_distance
  49 +
  50 + use_train = False
  51 + use_plane = False
  52 + if distance < extra_config['use_train_below_distance']:
  53 + use_train = True
  54 + else:
  55 + use_plane = True
48 56  
49 57 # I.a Train travel footprint
50 58 # ... TODO
... ... @@ -53,15 +61,16 @@ class EmissionModel(BaseEmissionModel):
53 61 footprint += self.compute_airplane_footprint(
54 62 distance=great_circle_distance
55 63 )
56   - distance += great_circle_distance
57 64  
58 65 # II.a Double it up since it's a round-trip
59 66 footprint *= 2.0
60 67 distance *= 2.0
61 68  
62 69 return {
63   - 'distance': distance,
  70 + 'distance_km': distance,
64 71 'co2eq_kg': footprint,
  72 + 'train_trips': 2 if use_train else 0,
  73 + 'plane_trips': 2 if use_plane else 0,
65 74 }
66 75  
67 76 def compute_airplane_footprint(
... ...