-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf_osmenrich_analysis.R
150 lines (127 loc) · 2.98 KB
/
sf_osmenrich_analysis.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
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
# sf and osmdata analysis
# Last edited 2021-02-16 by @leonardovida
# CC-BY ODISSEI SoDa team
# Packages ----
# Data
library(tidyverse)
library(sf)
library(osmenrich) # remotes::install_github("sodascience/osmenrich")
library(osmdata)
library(ggmap)
# Plotting
library(firatheme) # remotes::install_github("vankesteren/firatheme")
library(ggplot2)
# Optional: use local version of osm
# osmdata::set_overpass_url("http://localhost:8888/api/interpreter")
################
# sf and osmdata
################
# Create data
# Extract Utrecht boundaries
utrecht_sf <- osmdata::opq(bbox = "utrecht nl")
# Retrieve trees using the boundaries of Utrecht
trees <- utrecht_sf %>%
osmdata::add_osm_feature(key = "natural", value = "tree") %>%
osmdata::osmdata_sf()
# Create plot of trees within the boundaries
plot_trees <- ggplot() +
geom_sf(
data=trees$osm_points,
fill="darkgreen",
color="darkgreen"
) +
theme_fira()
# Plot trees
plot_trees
# Retrieve the map of Utrecht
utrecht_map <- ggmap::get_map(
c(left = 5.0041822,
bottom = 52.026282,
right = 5.195155,
top = 52.1356715
),
maptype = "toner-background")
# Plot the Map
ggmap(utrecht_map)
# Create plot of the trees using the map of Utrecht
plot_trees_utrecht <- ggmap(utrecht_map) +
geom_sf(
data = trees$osm_points,
inherit.aes = FALSE,
fill = "darkgreen",
color = "darkgreen"
) +
theme_fira() +
labs(title = "Trees in Utrecht", colour = "")
# Plot map with trees
plot_trees_utrecht
###########
# osmenrich
###########
# Create tibble sparrows
# and transform it to sf
sf_sparrows <-
tribble(
~sparrow, ~lat, ~lon,
"Sparrow_Alice", 52.12, 5.09,
"Sparrow_Bob", 52.13, 5.08,
) %>%
sf::st_as_sf(
coords = c("lon", "lat"),
crs = 4326
)
# Print sparrows sf
sf_sparrows
# Retrieve another map of Utrecht
# but moved over the sparrows
utrecht_map <- ggmap::get_map(
c(left = 5.05,
bottom = 52.10,
right = 5.110,
top = 52.14
),
maptype = "toner-background")
# Plot the map
utrecht_map
# Create the plot of the positions of the sparrows
plot_sparrows <- ggmap(utrecht_map) +
geom_sf(
inherit.aes = FALSE,
data = sf_sparrows,
colour = "red",
fill = "red",
size = 5,
) +
labs(title = "Sparrow position", colour = "") +
theme_fira()
# Plot the sparrow positions
plot_sparrows
# Data augmentation of sparrows sf
# with trees around them within the
# boundaries of Utrecht
sf_sparrows_enriched <- sf_sparrows %>%
osmenrich::enrich_osm(
name = "n_trees",
key = "natural",
value = "tree",
r = 1000
)
# Plot the enriched data
plot_sparrows_enriched <- ggmap(utrecht_map) +
geom_sf(
data = trees$osm_points,
inherit.aes = FALSE,
fill = "darkgreen",
color = "darkgreen"
) +
geom_sf(
inherit.aes = FALSE,
data = sf_sparrows_enriched,
colour = "red",
fill = "red",
size = 5,
) +
labs(title = "Sparrow and trees position", colour = "") +
theme_fira()
# Display the plot
plot_sparrows_enriched