-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 21.5 KB
/
.Rhistory
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2])
st_read('shapefiles/Berths.shp') %>%
select(berth_name) %>%
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2]) %>%
st_drop_geometry()
berths_v1 <- st_read('shapefiles/Berths.shp') %>%
select(berth_name) %>%
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2]) %>%
st_drop_geometry()
berths_v1
class(berths_v1)
st_read('shapefiles/Berths.shp') %>%
select(berth_name) %>%
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2]) %>%
st_drop_geometry() %>%
tibble()
berths_v2
names(berths_v2)
st_read('shapefiles/Berths.shp') %>%
select(berth_name) %>%
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2]) %>%
st_drop_geometry()
read.csv('berths/POLARIS_Wharves.csv') %>%
rename('ï..Name' = 'berth_name' )
read.csv('berths/POLARIS_Wharves.csv') %>%
rename('berth_name' = 'ï..Name')
read.csv('berths/POLARIS_Wharves.csv') %>%
rename('berth_name' = 'ï..Name',
'x' = 'Longitude',
'y' = 'Latitude') %>% print(n=10)
read.csv('berths/POLARIS_Wharves.csv') %>%
rename('berth_name' = 'ï..Name',
'x' = 'Longitude',
'y' = 'Latitude') %>% print(10)
berths_v1 <- st_read('shapefiles/Berths.shp') %>%
select(berth_name) %>%
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2]) %>%
st_drop_geometry()
berths_v2 <- read.csv('berths/POLARIS_Wharves.csv') %>%
rename('berth_name' = 'ï..Name',
'x' = 'Longitude',
'y' = 'Latitude')
head(berths_v1)
head(berths_v2)
library(sf)
library(tidyverse)
berths_v1 <- st_read('shapefiles/Berths.shp') %>%
select(berth_name) %>%
st_transform(4326) %>%
mutate(x = st_coordinates(geometry)[,1],
y = st_coordinates(geometry)[,2]) %>%
st_drop_geometry()
berths_v2 <- read.csv('berths/POLARIS_Wharves.csv') %>%
rename('berth_name' = 'ï..Name',
'x' = 'Longitude',
'y' = 'Latitude')
write_csv(berths_v1, 'berths_v1.csv')
write_csv(berths_v2, 'berths_v2.csv')
rm(list=ls(all=TRUE))
library(sf)
library(tidyverse)
library(scales)
library(snowfall)
library(fasterize)
library(raster)
library(RColorBrewer)
berths <- read_csv('berths/berths_v1.csv') %>%
st_as_sf(coords = c('x', 'y'))
berths
berths <- read_csv('berths/berths_v1.csv') %>%
st_as_sf(coords = c('x', 'y')) %>%
st_set_crs(4326)
plot(berths)
berths <- read_csv('berths/berths_v1.csv') %>%
st_as_sf(coords = c('x', 'y')) %>%
st_set_crs(4326) %>%
filter(berth_name != 'Coldharbour Jetty')
plot(berths)
rm(list=ls(all=TRUE))
library(sf)
library(tidyverse)
library(scales)
library(snowfall)
library(fasterize)
library(raster)
library(RColorBrewer)
latlong = "+init=epsg:4326"
ukgrid = "+init=epsg:27700"
google = "+init=epsg:3857"
the_thames <- st_read('https://raw.githubusercontent.com/KCL-ERG/useful_geography/master/thames.geojson')
plot(the_thames)
plot(st_geometry(the_thames))
vessel_class <- read_csv('docs/vessel_classifications.csv')
vessel_class$code <- as.character(vessel_class$code)
head(vessel_class)
# Get emissions by exact cut, substance and vessel type
emissions <- read_csv('emissions/inventory_export_2016.csv', col_types = cols())
emissions <- emissions[emissions$LAEIPLAExt == 'LAEI',]
emissions <- emissions[,c('VesselType', 'Substance', 'CellID', 'Sailing_kg', 'AtBerth_kg')]
emissions$CellID <- as.numeric(emissions$CellID)
head(emissions)
names(emissions) <- c('ship_type', 'pollutant', 'cellid', 'sailing', 'berth')
head(emissions)
pollutants_we_want <- c('PM', 'PM2.5', 'NOx')
emissions <- emissions[emissions$pollutant %in% pollutants_we_want,]
rm(pollutants_we_want)
head(emissions)
head(vessel_class)
# Tidy up some of the vessel classifications in the emissions file to match the GPS ecssel types
emissions[emissions$ship_type == 'RoRo Cargo / Vehicle','ship_type'] <-'RoRo Cargo/Vehicle'
emissions[emissions$ship_type == 'Cruise ship','ship_type'] <-'Passenger (cruise)'
emissions[emissions$ship_type == 'Passenger', 'ship_type'] <-'Passenger (ferry)'
emissions <- left_join(emissions, unique(vessel_class[,c('aggregated_class', 'group')]),
by = c('ship_type' = 'aggregated_class'))
head(emissions)
# Now get the grid by exact cut
grid <- st_read('grids/LAEIGridExtensionV2.gpkg', quiet = T)
## Make a map showing the exaxt cut issue
temp <- left_join(grid, emissions, by = c('CellID' = 'cellid')) %>%
rename(large_grid_id = GRID_ID0, x = X_COORD, y = Y_COORD)
focus_area <- st_as_sf(as(raster::extent(529735, 533671, 180049, 181052), 'SpatialPolygons')) %>%
st_set_crs(27700)
temp <- st_crop(temp, focus_area) %>%
filter(pollutant == 'NOx' & ship_type == 'Passenger (ferry)') %>%
arrange(sailing)
temp$sailing <- factor(temp$sailing)
colourCount = length(unique(temp$sailing))
getPalette = colorRampPalette(c('#ffffd4','#fed98e','#fe9929','#d95f0e','#993404'))
plot <- ggplot() +
geom_sf(data = temp, aes(fill = sailing)) +
scale_fill_manual(values = getPalette(colourCount), name = "Passenger ferry NOx \n emissions (kg/year)") +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=5),
legend.title = element_text(size=6)) +
guides(fill=guide_legend(ncol=2),
color = guide_legend(override.aes = list(shape = 0.5))) +
geom_sf(data = st_crop(st_transform(the_thames,27700),temp), fill = NA, colour='blue')
plot
ggsave('large_grid_nox_passenger_sailing_before_merge.png', plot = plot, path = 'maps/', height = 5, width = 15, units='cm')
rm(plot, colourCount, getPalette, temp, focus_area)
temp <- left_join(emissions, grid, by = c('cellid' = 'CellID')) %>%
rename(large_grid_id = GRID_ID0, x = X_COORD, y = Y_COORD) %>%
dplyr::select(cellid, ship_type,large_grid_id, x, y, pollutant, group, sailing, berth) %>%
st_as_sf(coords = c("x", "y"), crs=27700) %>%
st_buffer(dist = 500, endCapStyle= "SQUARE")
nox_roro_sailing <- filter(temp, pollutant == 'NOx' & ship_type == 'RoRo Cargo/Vehicle' & !is.na(sailing) & sailing > 0) %>%
dplyr::select(sailing)
labels <- list()
for (i in 1:5) {
labels[[i]] <- paste(round(quantile(nox_roro_sailing$sailing,seq(0,1,0.2))[i],0),
'-',
round(quantile(nox_roro_sailing$sailing,seq(0,1,0.2))[(i+1)],0))
}
labels <- unlist(labels)
nox_roro_sailing$emissions <- cut(nox_roro_sailing$sailing,
breaks=c(0,quantile(nox_roro_sailing$sailing,seq(0,1,0.2))[2:10]),
labels = labels)
colours <- c('#ffffd4','#fed98e','#fe9929','#d95f0e','#993404')
plot <- ggplot() +
geom_sf(data=temp) +
geom_sf(data = nox_roro_sailing, colour = 'black', aes(fill = emissions)) +
coord_sf() +
scale_fill_manual(values = colours, name = "NOx emissions (kg/year)") +
#scale_fill_discrete(name = expression(paste("NOx emissions (", mu, m^2, "/", m^3, ")", sep=""))) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank())
plot
rm(plot, nox_roro_sailing, colours, i, labels)
nrow(emissions)
head(emissions)
# Now aggregte
emissions <- emissions %>%
dplyr::select(-ship_type) %>%
group_by(pollutant, cellid, group) %>%
summarise(sailing = sum(sailing, na.rm=T),
berth = sum(berth, na.rm=T)) %>%
ungroup()
nrow(emissions)
head(emissions)
grid_emissions <- left_join(emissions, grid, by = c('cellid' = 'CellID')) %>%
rename(large_grid_id = GRID_ID0, x = X_COORD, y = Y_COORD) %>%
dplyr::select(cellid, large_grid_id, x, y, pollutant, group, sailing, berth) %>%
as_tibble() %>%
group_by(pollutant, group, large_grid_id, x, y) %>%
summarise(sailing = sum(sailing), berth = sum(berth)) %>%
st_as_sf(coords = c("x", "y"), crs=27700) %>%
st_buffer(dist = 500, endCapStyle= "SQUARE")
head(grid_emissions)
rm(emissions, grid)
nox_group_one_sailing <- filter(grid_emissions, pollutant == 'NOx' & group == 1 & !is.na(sailing) & sailing > 0) %>%
dplyr::select(sailing)
labels <- list()
for (i in 1:5) {
labels[[i]] <- paste(round(quantile(nox_group_one_sailing$sailing,seq(0,1,0.2))[i],0),
'-',
round(quantile(nox_group_one_sailing$sailing,seq(0,1,0.2))[(i+1)],0))
}
labels <- unlist(labels)
nox_group_one_sailing$emissions <- cut(nox_group_one_sailing$sailing,
breaks=c(0,quantile(nox_group_one_sailing$sailing,seq(0,1,0.2))[2:10]),
labels = labels)
colours <- c('#ffffd4','#fed98e','#fe9929','#d95f0e','#993404')
plot <- ggplot() +
geom_sf(data = temp) +
geom_sf(data = nox_group_one_sailing, colour = 'black', aes(fill = emissions)) +
coord_sf() +
scale_fill_manual(values = colours, name = "NOx emissions (kg/year)") +
#scale_fill_discrete(name = expression(paste("NOx emissions (", mu, m^2, "/", m^3, ")", sep=""))) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank())
ggsave('large_grid_sailing_group_one_nox.png', plot = plot, path = 'maps/', height = 5, width = 15, units='cm')
rm(plot, nox_group_one_sailing, colours, i, labels)
nox_group_one_berth <- filter(grid_emissions, pollutant == 'NOx' & group == 1 & !is.na(berth) & berth > 0) %>%
dplyr::select(berth)
labels <- list()
for (i in 1:5) {
labels[[i]] <- paste(round(quantile(nox_group_one_berth$berth,seq(0,1,0.2))[i],3),
'-',
round(quantile(nox_group_one_berth$berth,seq(0,1,0.2))[(i+1)],3))
}
labels <- unlist(labels)
nox_group_one_berth$emissions <- cut(nox_group_one_berth$berth,
breaks=c(0,quantile(nox_group_one_berth$berth,seq(0,1,0.2))[2:10]),
labels = labels)
colours <- c('#ffffd4','#fed98e','#fe9929','#d95f0e','#993404')
plot <- ggplot() +
geom_sf(data = temp) +
geom_sf(data = nox_group_one_berth,colour = 'black', aes(fill = emissions)) +
coord_sf() +
scale_fill_manual(values = colours, name = "NOx emissions (kg/year)") +
#scale_fill_discrete(name = expression(paste("NOx emissions (", mu, m^2, "/", m^3, ")", sep=""))) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank())
ggsave('large_grid_berth_group_one_nox.png', plot = plot, path = 'maps/', height = 5, width = 15, units='cm')
rm(plot, nox_group_one_berth, colours, i, labels)
head(grid_emissions)
filter(large_grid_emissions, large_grid_id == 9559)
filter(grid_emissions, large_grid_id == 9559)
small_grid <- unique(grid_emissions[,c('large_grid_id','geometry')]) %>%
st_make_grid(cellsize = 20, what = 'polygons') %>%
st_sf() %>%
st_join(unique(grid_emissions[,c('large_grid_id','geometry')]), join = st_within) %>%
filter(!is.na(large_grid_id)) %>%
mutate(small_grid_id = row_number())
head(small_grid)
small_grid[1:100,]
ggplot(data = small_grid[1:100,]) + geom_sf()
ggplot(data = small_grid[1:10,]) + geom_sf()
small_grid_result <- rbind(small_grid %>% mutate(group = 1),
small_grid %>% mutate(group = 2),
small_grid %>% mutate(group = 3),
small_grid %>% mutate(group = 4))
plot <- ggplot() +
geom_sf(data=filter(small_grid_result, large_grid_id %in% c(10399,10400,10401) & group == 1), aes(colour = as.factor(large_grid_id)), size=0.5) +
geom_sf(data=st_crop(st_transform(the_thames,27700),
filter(small_grid_result, large_grid_id %in% c(10399,10400,10401) & group == 1)), fill=NA, colour = 'blue') +
theme(legend.position = 'none', axis.text = element_blank(), axis.ticks = element_blank()) +
ggtitle('Small grid example')
ggsave('small_grid_example.png', plot = plot, path = 'maps/', height = 5, width = 15, units='cm')
rm(plot)
list.files('gps/', full.names=T, pattern = 'Rdata')
## Get GPS data
## These files are produced by Andrew. He processed the AIS data in Python.
## THe scripts to do this, are here: https://github.com/JimShady/laei_river_data/tree/master/ais_processing
list_of_gps_data <- list.files('gps/', full.names=T, pattern = 'Rdata')
list_of_gps_data <- data.frame(filename = list_of_gps_data,
actual_date = NA,
stringsAsFactors = F)
list_of_gps_data
head(list_of_gps_data)
list_of_gps_data$actual_date <- substr(x = list_of_gps_data$filename,
start = 24,
stop = nchar(list_of_gps_data$filename)-6)
list_of_gps_data$actual_date <- as.Date(list_of_gps_data$actual_date, format = '%d_%b_%Y')
list_of_gps_data <- list_of_gps_data[order(list_of_gps_data$actual_date),]
list_of_gps_data <- as.list(list_of_gps_data$filename)
head(list_of_gps_data)
list_of_gps_data <- list.files('gps/', full.names=T, pattern = 'Rdata')
list_of_gps_data <- data.frame(filename = list_of_gps_data,
actual_date = NA,
stringsAsFactors = F)
list_of_gps_data$actual_date <- substr(x = list_of_gps_data$filename,
start = 24,
stop = nchar(list_of_gps_data$filename)-6)
list_of_gps_data$actual_date <- as.Date(list_of_gps_data$actual_date, format = '%d_%b_%Y')
list_of_gps_data <- list_of_gps_data[order(list_of_gps_data$actual_date),]
head(list_of_gps_data)
list_of_gps_data <- as.list(list_of_gps_data$filename)
list_of_gps_data
load('gps/Gravesend_ANSData_01_Apr_2016.Rdata')
test_gps <- filter(data, !is.na(VESSEL_TYPE)) %>%
st_as_sf(coords = c('lon', 'lat'), crs = 4326) %>%
st_transform(27700) %>% #27
st_crop(st_bbox(small_grid)) %>% #61
left_join(vessel_class, by = c('VESSEL_TYPE' = 'code')) %>%
dplyr::select(group)
plot <- ggplot() +
geom_sf(data = temp) +
geom_sf(data = test_gps, alpha=0.3, size=0.5, colour='blue') +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank())
ggsave('one_day_gps_example.png', plot = plot, path = 'maps/', height = 5, width = 15, units='cm')
list_of_gps_data
load(list_of_gps_data[[1]])
head(data)
head(vessel_class)
gps_data <- filter(data, !is.na(VESSEL_TYPE)) %>%
st_as_sf(coords = c('lon', 'lat'), crs = 4326) %>%
st_transform(27700) %>% #27
st_crop(st_bbox(small_grid)) %>% #61
left_join(vessel_class, by = c('VESSEL_TYPE' = 'code')) %>%
dplyr::select(group)
gps_data
gps_data
rm(data)
gps_per_small_grid_id <- st_join(gps_data, small_grid, join = st_intersects) %>%
filter(!is.na(small_grid_id))
head(small_grid_id)
head(gps_per_small_grid_id)
# Remove geoms we don't need from this count
gps_per_small_grid_id$geometry <- NULL
head(gps_per_small_grid_id)
# Sum up by the grid square
gps_per_small_grid_id <- gps_per_small_grid_id %>%
dplyr::select(group, small_grid_id) %>%
group_by(small_grid_id, group) %>%
summarise(count = length(group))
head(gps_per_small_grid_id)
paste0('grids/small_result_', substr(x = x,
start = 24,
stop = nchar(x)-6), '.Rdata')
save(gps_per_small_grid_id, file = paste0('grids/small_result_', substr(x = x,
start = 24,
stop = nchar(x)-6), '.Rdata'))
x <- list_of_gps_data[[1]]
paste0('grids/small_result_', substr(x = x,
start = 24,
stop = nchar(x)-6), '.Rdata')
save(gps_per_small_grid_id, file = paste0('grids/small_result_', substr(x = x,
start = 24,
stop = nchar(x)-6), '.Rdata'))
rm(gps_per_small_grid_id)
a <- a+1
print(a)
sfInit(parallel=TRUE, cpus=parallel:::detectCores()-1)
sfLibrary(sf)
sfLibrary(tidyverse)
sfExport(list=list("small_grid", "vessel_class", "a"))
sfStop()
## Tidy
rm(process_gps_data, list_of_gps_data)
process_gps_data <- function(x) {
load(x)
gps_data <- filter(data, !is.na(VESSEL_TYPE)) %>%
st_as_sf(coords = c('lon', 'lat'), crs = 4326) %>%
st_transform(27700) %>% #27
st_crop(st_bbox(small_grid)) %>% #61
left_join(vessel_class, by = c('VESSEL_TYPE' = 'code')) %>%
dplyr::select(group)
rm(data)
# Count, over the year in total, how many GPS points there are in each small grid square
gps_per_small_grid_id <- st_join(gps_data, small_grid, join = st_intersects) %>%
filter(!is.na(small_grid_id))
# Remove geoms we don't need from this count
gps_per_small_grid_id$geometry <- NULL
# Sum up by the grid square
gps_per_small_grid_id <- gps_per_small_grid_id %>%
dplyr::select(group, small_grid_id) %>%
group_by(small_grid_id, group) %>%
summarise(count = length(group))
save(gps_per_small_grid_id, file = paste0('grids/small_result_', substr(x = x,
start = 24,
stop = nchar(x)-6), '.Rdata'))
rm(gps_per_small_grid_id)
a <- a+1
print(a)
}
list.files('grids/', full.names=T, pattern = 'small')
list_of_small_grid_gps_result_data <- list.files('grids/', full.names=T, pattern = 'small')
for (i in 1:length(list_of_small_grid_gps_result_data)) {
load(list_of_small_grid_gps_result_data[i])
if (i == 1) {
gps_per_small_grid_bind <- gps_per_small_grid_id
} else {
gps_per_small_grid_bind <- bind_rows(gps_per_small_grid_bind,gps_per_small_grid_id)
}
}
rm(list_of_small_grid_gps_result_data, gps_per_small_grid_id, i, small_grid)
head(gps_per_small_grid_bind)
filter(gps_per_small_grid_bind, small_grid_id == 11410)
gps_per_small_grid <- gps_per_small_grid_bind %>%
group_by(small_grid_id, group) %>%
summarise(count = sum(count))
rm(gps_per_small_grid_bind)
gps_per_small_grid
small_grid_result <- left_join(small_grid_result, gps_per_small_grid,
by = c("small_grid_id" = "small_grid_id", "group" = "group"))
rm(gps_per_small_grid)
small_grid_result
small_grid_result
tail(small_grid_result)
berths <- read_csv('berths/berths_v1.csv') %>%
st_as_sf(coords = c('x', 'y')) %>%
st_set_crs(4326) %>%
filter(berth_name != 'Coldharbour Jetty') %>%
st_transform(27700)
ggplot(data=berths) + geom_Sf()
ggplot(data=berths) + geom_sf()
small_grid_result
small_grid_result <- small_grid_result %>%
st_join(berths, join = st_intersects, left = TRUE)
small_grid_result$berth_name <- as.character(small_grid_result$berth_name)
small_grid_result
small_grid_result
## remove data where there's very few GPS points
small_grid_result <- filter(small_grid_result, count > 20)
large_grid_sailing_counts <- aggregate(data=small_grid_result[!is.na(small_grid_result$count) &
is.na(small_grid_result$berth_name),],
count ~ group + large_grid_id, FUN=sum)
large_grid_berth_counts <- aggregate(data=small_grid_result[!is.na(small_grid_result$count) &
!is.na(small_grid_result$berth_name),],
count ~ group + large_grid_id, FUN=sum)
names(large_grid_sailing_counts)[3] <- 'sailing_count'
names(large_grid_berth_counts)[3] <- 'berth_count'
large_grid_berth_counts
small_grid_result <- small_grid_result %>% left_join(large_grid_sailing_counts, by = c("large_grid_id" = "large_grid_id",
"group" = "group")) %>%
left_join(large_grid_berth_counts, by = c("large_grid_id" = "large_grid_id",
"group" = "group"))
rm(large_grid_sailing_counts, large_grid_berth_counts)
small_grid_result
# Calculate the contribution percentages
small_grid_result$contribution <- NA
small_grid_result[is.na(small_grid_result$berth_name),'contribution'] <- small_grid_result[is.na(small_grid_result$berth_name),]$count /
small_grid_result[is.na(small_grid_result$berth_name),]$sailing_count
small_grid_result[!is.na(small_grid_result$berth_name),'contribution'] <- small_grid_result[!is.na(small_grid_result$berth_name),]$count /
small_grid_result[!is.na(small_grid_result$berth_name),]$berth_count
head(small_grid_result)
small_grid_result <- rbind(small_grid_result %>% mutate(pollutant = 'NOx'),
small_grid_result %>% mutate(pollutant = 'PM'),
small_grid_result %>% mutate(pollutant = 'PM2.5'))
small_grid_result
grid_emissions
# Join the small grid result to the original emissions
small_grid_result <- left_join(small_grid_result, st_drop_geometry(grid_emissions),
by = c("large_grid_id" = "large_grid_id",
"group" = "group",
"pollutant" = "pollutant"))
small_grid_result$emissions <- NA
small_grid_result[is.na(small_grid_result$berth_name),'emissions'] <- small_grid_result[is.na(small_grid_result$berth_name),]$contribution *
small_grid_result[is.na(small_grid_result$berth_name),]$sailing
small_grid_result[!is.na(small_grid_result$berth_name),'emissions'] <- small_grid_result[!is.na(small_grid_result$berth_name),]$contribution *
small_grid_result[!is.na(small_grid_result$berth_name),]$berth
small_grid_result <- filter(small_grid_result, !is.na(emissions))
small_grid_result
#################################
rm(berths, vessel_class, the_thames)
small_grid_result <- st_centroid(small_grid_result)
### Re-organise for David
small_grid_result$x <- st_coordinates(small_grid_result)[,1]
small_grid_result$y <- st_coordinates(small_grid_result)[,2]
small_grid_result$geometry <- NULL
small_grid_result <- as.tibble(small_grid_result)
small_grid_result
result <- small_grid_result %>%
dplyr::select(x,y,group, pollutant, emissions, berth_name) %>%
spread(pollutant, emissions) %>%
rename_all(tolower) %>%
mutate(no2 = nox * 0.05,
year = 2016)
head(result)
rm(result)
nox <- grid_emissions %>%
st_drop_geometry() %>%
dplyr::select(pollutant, large_grid_id, group, sailing, berth) %>%
group_by(pollutant, group, large_grid_id) %>%
summarise(sailing_emissions = sum(sailing, na.rm=T),
berth_emissions = sum(berth, na.rm=T)) %>%
filter(pollutant == 'NOx')
no2 <- nox %>% mutate(sailing_emissions = sailing_emissions * 0.05,
berth_emissions = berth_emissions * 0.05) %>% ungroup() %>% mutate(pollutant = 'NO2')
pm <- grid_emissions %>% st_drop_geometry() %>% dplyr::select(pollutant, large_grid_id, group, sailing, berth) %>%
group_by(pollutant, group, large_grid_id) %>%
summarise(sailing_emissions = sum(sailing, na.rm=T),
berth_emissions = sum(berth, na.rm=T)) %>%
filter(pollutant == 'PM')
pm25 <- grid_emissions %>% st_drop_geometry() %>% dplyr::select(pollutant, group, large_grid_id, sailing, berth) %>%
group_by(pollutant, group, large_grid_id) %>%
summarise(sailing_emissions = sum(sailing, na.rm=T),
berth_emissions = sum(berth, na.rm=T)) %>%
filter(pollutant == 'PM2.5')
result <- bind_rows(nox, pm, pm25, no2) %>% rename_all(tolower)
result
rm(result, no2, nox, pm, pm25)