-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_map_no_interaction_selected.R
227 lines (188 loc) · 6.82 KB
/
show_map_no_interaction_selected.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
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
###############################################
#### Module for the map without interaction only selected ####
###############################################
# This is a map module, for only visualisation, no interaction with the user
# Only shows the selected stations
######################################################################
# Output Module ----
######################################################################
show_map_no_select_output <- function(id) {
ns <- NS(id)
tagList(
leafletOutput(ns('map_select'))
)
}
######################################################################
# Server Module ----
######################################################################
show_map_no_select_server <- function(id,
data_stations,
change_tab
) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
# Initialisation icons ----
# Icons for the reference stations
icons_stations <- iconList(
lml_selected = makeIcon(iconUrl = "images/ref_selected_txt.png",
iconWidth = 24, iconHeight = 16))
# Icons for the knmi stations
icons_knmis <- iconList(
knmi_selected = makeIcon(iconUrl = "images/knmi_selected_txt.png",
iconWidth = 30, iconHeight = 16))
# Generate base map ----
output$map_select <- renderLeaflet({
ns("map_select")
leaflet() %>%
# addTiles() %>%
addProviderTiles(
'Esri.WorldGrayCanvas' # option 1
#'Esri.WorldTopoMap' # option 2
) %>%
addProviderTiles(
'CartoDB.PositronOnlyLabels' # option 1
) %>%
addDrawToolbar(
targetGroup = 'Selected',
polylineOptions = FALSE,
markerOptions = FALSE,
polygonOptions = FALSE,
circleOptions = FALSE,
circleMarkerOptions = FALSE,
rectangleOptions = FALSE
) %>%
addScaleBar(position = "bottomleft")
})
# Change view to centre stations
set_view_stations <- function(){
# Check if there is data
data_snsrs_col <- get_locations_coordinates(data_stations())$station_loc
# If there are stations then zoom to them
if(!is.null(data_snsrs_col)){
# calculate mean location
mean_lat <- mean(data_snsrs_col$lat)
mean_lon <- mean(data_snsrs_col$lon)
# create proxy of the map
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>% setView(mean_lon, mean_lat, zoom = 8)
}else{
# Zoom to the default
# create proxy of the map
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>% setView(5.384214, 52.153708 , zoom = 8)
}
}
# Add knmi stations to the map
add_knmi_map <- function(){
# Get the data
data_snsrs <- get_locations_coordinates(data_stations())$station_loc
if(is.null(data_snsrs)){
# clear all weather stations from the map
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>%
# Clear weather markers
clearGroup("weather")
}else{
# Get the station data
data_snsrs <- data_snsrs %>%
# Only show the selected
dplyr::filter(station_type == "KNMI", selected)
# Update map with new markers to show selected
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>% clearGroup("weather") # Clear markers
# Put stations on map
if(nrow(data_snsrs) > 0){
proxy %>%
addMarkers(data = data_snsrs, ~lon, ~lat,
icon = icons_knmis["knmi_selected"],
label = lapply(as.list(data_snsrs$station), HTML),
layerId = ~station,
group = "weather")
}
}
}
# Add the sensors to the map
add_sensors_map <- function(){
# # Check if there is data
data_snsrs <- get_locations_coordinates(data_stations())$station_loc
if(is.null(data_snsrs) ){
#Clear all sensors from the map
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>%
# Clear sensoren markers
clearGroup("sensoren")
}else{
# Get the sensor data
data_snsrs <- data_snsrs %>%
# Only show the selected
dplyr::filter(station_type == "sensor", selected)
# Update map with new markers to show selected
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>%
# Clear sensor markers
clearGroup("sensoren")
# Add the sensors to the map
if(nrow(data_snsrs) > 0){
proxy %>%
addCircleMarkers(data = data_snsrs, ~lon, ~lat,
stroke = TRUE,
weight = 2,
label = lapply(data_snsrs$station, HTML),
layerId = ~station,
fillOpacity = 0.7,
radius = 5,
color = data_snsrs$col,
group = "sensoren"
)
}
}
}
# Add reference stations to the map
add_lmls_map <- function(){
# # Check if there is data
data_snsrs <- get_locations_coordinates(data_stations())$station_loc
if(is.null(data_snsrs)){
# Clear all reference stations from the map
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>%
# Clear reference markers
clearGroup("reference")
}else{
# Get the reference stations
data_snsrs <- data_snsrs %>%
# Only show the selected
dplyr::filter(station_type == "ref", selected)
# Update map with new markers to show selected
proxy <- leafletProxy('map_select') # set up proxy map
proxy %>%
# Clear reference markers
clearGroup("reference")
# Add the stations to the map
if(nrow(data_snsrs) > 0){
proxy %>%
addMarkers(data = data_snsrs, ~lon, ~lat,
icon = icons_stations["lml_selected"],
label = lapply(as.list(data_snsrs$station), HTML),
layerId = ~station,
group = "reference")
}
}
}
# Observers and ObserveEvents ----
# Create list where to observe changes to react on
tolisten <- reactive({
list(change_tab())
})
# Observe if new data is available-> redraw map
observeEvent(tolisten(),{
# Add the new situation to the map
isolate(add_lmls_map())
isolate(add_sensors_map())
isolate(add_knmi_map())
# Zoom to the stations
set_view_stations()
# }
# }
})
})
}