diff --git a/flaskr/controllers/main_controller.py b/flaskr/controllers/main_controller.py index e114426..7929a22 100644 --- a/flaskr/controllers/main_controller.py +++ b/flaskr/controllers/main_controller.py @@ -262,7 +262,8 @@ def compute(): # process the queue of estimation requests destinations_by_city_key = {} - cities_sum = {} + cities_sum_foot = {} + cities_sum_dist = {} for model in emission_models: cities_dict = {} for _destination in _destinations: @@ -282,11 +283,16 @@ def compute(): # process the queue of estimation requests 'city': city_key, 'address': _destination.address, 'footprint': 0.0, + 'distance': 0.0, } - cities_dict[city_key]['footprint'] += footprint - if city_key not in cities_sum: - cities_sum[city_key] = 0.0 - cities_sum[city_key] += footprint + cities_dict[city_key]['footprint'] += footprint['co2eq_kg'] + cities_dict[city_key]['distance'] += footprint['distance'] + if city_key not in cities_sum_foot: + cities_sum_foot[city_key] = 0.0 + cities_sum_foot[city_key] += footprint['co2eq_kg'] + if city_key not in cities_sum_dist: + cities_sum_dist[city_key] = 0.0 + cities_sum_dist[city_key] += footprint['distance'] cities = [cities_dict[k] for k in cities_dict.keys()] cities = sorted(cities, key=lambda c: c['footprint']) @@ -297,17 +303,21 @@ def compute(): # process the queue of estimation requests _results['footprints'] = footprints - total = 0.0 + total_foot = 0.0 + total_dist = 0.0 cities_mean_dict = {} - for city in cities_sum.keys(): - city_mean = 1.0 * cities_sum[city] / len(emission_models) + for city in cities_sum_foot.keys(): + city_mean_foot = 1.0 * cities_sum_foot[city] / len(emission_models) + city_mean_dist = 1.0 * cities_sum_dist[city] / len(emission_models) cities_mean_dict[city] = { 'address': destinations_by_city_key[city].address, 'city': city, - 'footprint': city_mean, + 'footprint': city_mean_foot, + 'distance': city_mean_dist, } - total += city_mean + total_foot += city_mean_foot + total_dist += city_mean_dist cities_mean = [cities_mean_dict[k] for k in cities_mean_dict.keys()] cities_mean = sorted(cities_mean, key=lambda c: c['footprint']) @@ -317,8 +327,10 @@ def compute(): # process the queue of estimation requests } _results['cities'] = cities_mean - _results['total'] = total # DEPRECATED - _results['footprint'] = total + _results['total'] = total_foot # DEPRECATED + _results['footprint'] = total_foot + + _results['distance'] = total_dist return _results @@ -433,25 +445,17 @@ def consult_estimation(public_id, extension): si = StringIO() cw = csv.writer(si, quoting=csv.QUOTE_ALL) - cw.writerow([u"city", u"address", u"co2 (g)"]) + cw.writerow([u"city", u"address", u"co2 (kg)", u"distance (km)"]) results = estimation.get_output_dict() - if 'mean_footprint' in results: - for city in results['mean_footprint']['cities']: - cw.writerow([ - city['city'].encode(OUT_ENCODING), - city['address'].encode(OUT_ENCODING), - city['footprint'] - ]) - elif 'cities' in results: - for city in results['cities']: - cw.writerow([ - city['city'].encode(OUT_ENCODING), - city['address'].encode(OUT_ENCODING), - city['total'] - ]) - - # HTTP headers? + for city in results['cities']: + cw.writerow([ + city['city'].encode(OUT_ENCODING), + city['address'].encode(OUT_ENCODING), + round(city['footprint'], 3), + round(city['distance'], 3), + ]) + # return si.getvalue().strip('\r\n') return Response( response=si.getvalue().strip('\r\n'), diff --git a/flaskr/laws/travel_emission_linear_fit.py b/flaskr/laws/travel_emission_linear_fit.py index 6f16b90..9ecfda6 100644 --- a/flaskr/laws/travel_emission_linear_fit.py +++ b/flaskr/laws/travel_emission_linear_fit.py @@ -2,6 +2,7 @@ import numpy as np from geopy.distance import great_circle +# @abc class BaseEmissionModel(): def __init__(self, config): # Constructor self.name = config.name @@ -29,9 +30,10 @@ class EmissionModel(BaseEmissionModel): prefer_train_under_distance=0, # meters ): footprint = 0.0 + distance = 0.0 ############################################# - # TODO: find closest airport(s) and pick one + # TODO: find closest airport(s) and pick one? # We're going to need caching here as well. from collections import namedtuple origin_airport = namedtuple('Position', [ @@ -55,34 +57,33 @@ class EmissionModel(BaseEmissionModel): # ... TODO # I.b Airplane travel footprint - footprint += self.compute_airplane_footprint( + great_circle_distance = self.get_distance_between( origin_latitude=origin_airport.latitude, origin_longitude=origin_airport.longitude, destination_latitude=destination_airport.latitude, destination_longitude=destination_airport.longitude, ) + footprint += self.compute_airplane_footprint( + distance=great_circle_distance + ) + distance += great_circle_distance - # II.a Double the footprint if it's a round-trip + # II.a Double it up since it's a round-trip footprint *= 2.0 + distance *= 2.0 - return footprint + return { + 'distance': distance, + 'co2eq_kg': footprint, + } + # return footprint def compute_airplane_footprint( self, - origin_latitude, - origin_longitude, - destination_latitude, - destination_longitude + distance ): config = self.config.plane_emission_linear_fit - great_circle_distance = self.get_distance_between( - origin_latitude, origin_longitude, - destination_latitude, destination_longitude - ) - - distance = great_circle_distance - distance = config.connecting_flights_scale * distance footprint = self.compute_airplane_distance_footprint(distance, config) @@ -111,7 +112,7 @@ class EmissionModel(BaseEmissionModel): """ :param distance: in km :param config: - :return: + :return: float """ footprint = distance for interval in config.intervals: diff --git a/flaskr/templates/estimation.html b/flaskr/templates/estimation.html index 70afef4..35aca83 100644 --- a/flaskr/templates/estimation.html +++ b/flaskr/templates/estimation.html @@ -193,7 +193,7 @@ jQuery(document).ready(function($){ var vizid = "#cities_footprints_d3viz"; var csvUrl = "/estimation/{{ estimation.public_id }}.csv"; var x_key = 'city'; - var y_key = 'co2 (g)'; + var y_key = 'co2 (kg)'; // Set the dimensions and margins of the graph var margin = {top: 10, right: 30, bottom: 150, left: 150}, @@ -214,7 +214,7 @@ jQuery(document).ready(function($){ d3.csv(csvUrl, function(data) { // Extrema - var data_y_max = d3.max(data, function(d) { return parseFloat(d['co2 (g)']); }); + var data_y_max = d3.max(data, function(d) { return parseFloat(d[y_key]); }); var axis_y_max = ceil_value_to_magnitude(data_y_max); // X axis @@ -266,7 +266,7 @@ jQuery(document).ready(function($){ var vizid = "#cities_footprints_d3viz_lollipop"; var csvUrl = "/estimation/{{ estimation.public_id }}.csv"; var y_key = 'city'; - var x_key = 'co2 (g)'; + var x_key = 'co2 (kg)'; var margin = {top: 10, right: 30, bottom: 150, left: 150}, height = Math.max(300, 100+16*plots_config['cities_count']) - margin.top - margin.bottom; -- libgit2 0.21.2