-
Notifications
You must be signed in to change notification settings - Fork 2
/
04_enrich_grid.R
95 lines (77 loc) · 2.31 KB
/
04_enrich_grid.R
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
# Enrich the prediction grid with covariates
# - Moon illumination set to 0
# - Land use in grid cell
# Last edited 20220302 by @vankesteren
# Libraries
library(tidyverse)
library(lubridate)
library(osmenrich)
library(sf)
library(stars)
library(pbapply)
# load the data
grid_pred <- read_rds("data/grid_pred.rds")
landuse <- read_stars("data/land_use_2019.tif") %>% mutate(land_use_2019.tif = na_if(land_use_2019.tif, ""))
# Sun and moon elevation ----
# add moon illumination to dataset, as well as CloudCover
grid_pred <- grid_pred %>% mutate(moon_illumination = 0, CloudCover = "clear")
# Land use ----
# Land use features in grid cell
landuse_props <- pbsapply(st_geometry(grid_pred), function(cell) landuse %>% st_crop(cell) %>% table() %>% prop.table())
landuse_tbl <-
as_tibble(t(landuse_props)[,-1], .name_repair = "minimal") %>%
set_names(function(num) paste0("landtype_", num)) %>%
select(-landtype_21)
grid_pred <- bind_cols(grid_pred, landuse_tbl)
# plot to show what we just did
ggplot() +
geom_stars(data = landuse, downsample = 2) +
geom_sf(data = grid_pred, fill = "transparent", colour = "black") +
scale_fill_viridis_d(na.value = "transparent", direction = -1) +
theme_minimal() +
labs(
fill = "Land type",
x = "",
y = "",
title = "Grid cell observation enrichment",
subtitle = "Using cells of 25 m²"
)
ggsave("img/grid_enrich_landuse.png", width = 8, height = 6)
# Motorways from openstreetmaps ----
# take centroids of cells, and enrich using same procedure as before
sf_osm <- st_centroid(grid_pred) %>% select(geometry)
# 1 km radius
sf_osm <- sf_osm %>%
enrich_osm(
name = "motorway_1km",
key = "highway",
value = "motorway",
type = "lines",
kernel = "gaussian",
r = 1000
)
# 10km radius
sf_osm <- sf_osm %>%
enrich_osm(
name = "motorway_10km",
key = "highway",
value = "motorway",
type = "lines",
kernel = "gaussian",
r = 10000
)
# 25km radius
sf_osm <- sf_osm %>%
enrich_osm(
name = "motorway_25km",
key = "highway",
value = "motorway",
type = "lines",
kernel = "gaussian",
r = 25000
)
# then we combine with the grid
tbl_osm <- as_tibble(sf_osm) %>% select(starts_with("motorway"))
grid_pred <- bind_cols(grid_pred, tbl_osm)
# write to output folder
write_rds(grid_pred, "data/grid_enriched.rds")