Created
February 4, 2019 08:01
-
-
Save ryanbateman/951ed9b9ea26421385406ebd4bce27f4 to your computer and use it in GitHub Desktop.
A short script which shows how to calculate carbon cost of a flight, given data from Monzo's API using Monzor (the R library I wrote)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(monzor) | |
library(geosphere) | |
library(ggrepel) | |
library(ggmap) | |
# For the purposes of this demo, I picked the relevant transcation by hand. Using Monzo's callbacks, this would be automated in live code | |
getFlight <- function() { | |
getTransaction(transactionId = "tx_0000AAAAA5i3BqkXSPAAAA") | |
} | |
# Given an start and end, calculate the offset required, using the lat/lon of each airport | |
calculateOffset <- function(startAirport = NULL, endAirport = NULL) { | |
airports <- read.csv("data/airports.csv", sep = ",") | |
airports <- airports[which(airports$iata_code %in% c(startAirport, endAirport)), ] | |
startAirport <- airports[which(airports$iata_code == startAirport), ] | |
endAirport <- airports[which(airports$iata_code == endAirport), ] | |
distance <- distHaversine( | |
c(startAirport$latitude_deg, startAirport$longitude_deg), | |
c(endAirport$latitude_deg, endAirport$longitude_deg) | |
) | |
journey <- as.data.frame(cbind(startAirport$latitude_deg, startAirport$longitude_deg, endAirport$latitude_deg, endAirport$longitude_deg)) | |
names(journey) <- c("startLat", "startLon", "endLat", "endLon") | |
worldmap <- borders("world", colour="#efede1", fill="#efede1") | |
ggplot() + worldmap + | |
geom_curve(data = journey, aes(x = startLon, | |
y = startLat, | |
xend = endLon, | |
yend = endLat), | |
col = "#b29e7d", size = 1, curvature = .2) + | |
geom_point(data = airports, aes(x = longitude_deg, y = latitude_deg), col = "#970027") + | |
geom_text_repel(data = airports, aes(x = longitude_deg, y = latitude_deg, label = name), col = "black", size = 5, segment.color = NA) + | |
xlim(-20, 59) + | |
ylim(35, 71) + | |
theme(panel.background = element_rect(fill="white"), | |
axis.line = element_blank(), | |
axis.text.x = element_blank(), | |
axis.text.y = element_blank(), | |
axis.ticks = element_blank(), | |
axis.title.x = element_blank(), | |
axis.title.y = element_blank() | |
) | |
} | |
# Calculate the cost itself | |
calculate <- function(distance = 0) { | |
costPerKilogram = 0.008 | |
AverageSeatNumber = 158.44 | |
PassengerLoadFactor = 0.77 | |
DetourConstant = 50 | |
InverseCargoFactor = 0.951 | |
EconomyClassWeight = 0.960 | |
EmissionFactor = 3.150 | |
PreProduction = 0.51 | |
Multiplier = 2 | |
a = 0.0000387871 | |
b = 2.9866 | |
c = 1263.42 | |
emissions <- ((a*distance^2 + b*distance + c) / (AverageSeatNumber * PassengerLoadFactor)) * InverseCargoFactor * EconomyClassWeight * (EmissionFactor * Multiplier + PreProduction) | |
costPerKilogram * emissions | |
} | |
# Do it all | |
go <- function() { | |
flight <- getFlight() | |
origin <- flight$metadata$city_of_origin_airport_code | |
dest <- flight$metadata$city_of_destination_airport_code | |
offsetCost <- calculateOffset(origin, dest) | |
response <- addItemToFeed(title = cat("Offset your flight to ", dest$name), | |
itembody = cat("Offsetting the flight would cost ", offsetCost), | |
image_url = "https://www.dropbox.com/s/57fx4lk0a11g1d3/co2.png?raw=1", | |
background_colour = "#ff00ff") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment