-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
173 lines (125 loc) · 4.87 KB
/
index.Rmd
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
---
title: "The squirrel problem"
author: "Javier Saez Gallego"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
<!-- Modify the default CSS -->
<style>
.navbar-inverse .navbar-nav>.active>a, .navbar-inverse .navbar-nav>.active>a:hover, .navbar-inverse .navbar-nav>.active>a:focus {
color: #f5f5f5;
background-color: #4a524c;
}
.navbar-inverse {
background-color: #999999;
border-color: #cccccc;
}
</style>
<!-- END CSS -->
```{r setup, include=FALSE}
library(flexdashboard)
library(leaflet)
library(raster)
library(plotly)
library(readxl)
library(dplyr)
# Load path and map
path_points = read.csv("Files/path_coordinates_solution.csv")
spain_img = raster("Files/GlobCover_Spain.tif")
# transform raster values to be plotted nicely
# map labels to roughness values
globCover = c(11,14,20,30,40,50,60,70,90,100,
110,120,130,140,150,160,170,180,190,200,210,220,230)
roughness = c(0.10, 0.10, 0.30, 0.30, 1.50, 1.50, 1.50, 1.50, 1.50, 1.50,
1.50, 0.50, 0.10, 0.03, 0.05, 0.50, 0.60, 0.20, 1.00, NA, NA, NA, NA)
small_img_plot = reclassify(spain_img,cbind(globCover,roughness))
```
Map
=======================================================================
```{r}
# The raster is too big to be displayed so let's shrink it
agg_small_img_plot = raster::aggregate(small_img_plot,fact=3)
# Make a leaflet visualization
pal2 = colorNumeric("RdYlGn", domain = NULL)
ll_map = leaflet() %>%
# Base maps
addProviderTiles('Esri.WorldGrayCanvas',group="Esri Grey") %>%
addProviderTiles('Esri.WorldTopoMap',group="Esri Topo") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri Image") %>%
# # Roughness info
addRasterImage(agg_small_img_plot, opacity = 0.5,colors=pal2, group="Roughness map") %>%
addLegend(position="bottomleft", pal = pal2, values = values(agg_small_img_plot),
title = "Roughness",
opacity = 1) %>%
# path info
addPolylines(lng=path_points$lon,lat=path_points$lat,group="Path") %>%
# Control groups
addLayersControl(
baseGroups = c("Esri Grey","Esri Image","Esri Topo"),
overlayGroups = c("Roughness map", "Path"),
options = layersControlOptions(collapsed = F)
)
ll_map
```
Path statistics
=======================================================================
Row
-----------------------------------------------------------------------
###
```{r, echo=FALSE, message=FALSE, warning=FALSE}
# Get values of the pixels where the squirrel steps
values_glob = values(spain_img)[path_points$n]
tab_summary = sort(table(values_glob))
tab_labels = as.numeric(names(tab_summary))
# Create some nice labels for each type of land
legend = read_excel("Files/Globcover2009_Legend.xls")
idx = mapply(function(x) which(x == legend$Value), tab_labels)
# Prepare colors
pal2 = colorNumeric("RdYlGn", domain = c(0,1.5))
idx2 = mapply(function(x) which(x == globCover), tab_labels)
col_roughness = pal2(roughness[idx2])
# Make the plot
plot_ly( y = tab_summary, x = names(tab_summary),
text = legend$Label[idx],
type = 'bar', hoverinfo = 'text',
marker = list(color = col_roughness)) %>%
layout(title = "Land cover along the shortest path",
xaxis = list(title = "Land cover type"),
yaxis = list(title = "count"))
```
Row
-----------------------------------------------------------------------
###
```{r, echo=FALSE, message=FALSE, warning=FALSE}
# Get the colors
values_glob_plot = values(small_img_plot)[path_points$n]
col_roughness_all = pal2(values_glob_plot)
# Get the hover text
idx = mapply(function(x) which(x == legend$Value), values_glob)
text_hover = legend$Label[idx]
# Make the plot
plot_ly( x=1:length(values_glob_plot),y = values_glob,
marker=list(color =col_roughness_all ),
text = text_hover) %>%
layout(title = "",
xaxis = list(title = "Step number"),
yaxis = list(title = "Land cover type"))
```
Info
=======================================================================
Row
-----------------------------------------------------------------------
### Intro
> Can a squirrel cross from north to south spain without touching the ground?
The answer is: **obviously not**. But, another question arises, and this one is not so easy to answer:
> If a squirrel had to go from the north of Spain to the south, touching the ground as little as posisble: which way would it follow?
You have found the answer! Browse trough the interactive map or [download](https://github.com/jsga/GlobCover_maps_squirrel/blob/master/Files/path_coordinates_solution.csv) the coordinates of the path. The code is publicly available in [this](https://github.com/jsga/GlobCover_maps_squirrel) repository. Feel free to share and comment it.
Row
-------------------------------------
### The solution
[To be filled up, work in progress]
### Contact
Send me an [email](mailto:[email protected]).