-
Notifications
You must be signed in to change notification settings - Fork 7
/
.Rhistory
512 lines (512 loc) · 24.3 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
if(length(setdiff(display_pt$country, old$country))>0){
warning('Countries with points have been added, will need to update the point map')
}
write.csv(display_pt, "assets/maps/regions_point.csv", row.names=FALSE)
display_pt
View(display_list)
View(display_pt)
## regions_get_master_list.r
# April 29, 2016 by @jules32
## Get master list of OHI and OHI+ countries from the private Google Spreadsheet 'OHI_CountriesMaster'
## for display on ohi-science.org/projects/ohi-assessments
## https://docs.google.com/spreadsheets/d/1Xh8-36cLCEa_bppqLJu-nukVgPDT8xVEd9nxEaPYKKg/edit#gid=2018368498
#### setup ----
library(googlesheets) # install.packages('googlesheets') # by Jenny Bryan
library(tidyverse)
library(readr)
## authorize googledocs -- requires user interaction with default browser
gs_auth(new_user = TRUE)
#### read in Google Sheet, previously gs_title('OHI_CountriesMaster')----
master_list <- googlesheets::gs_url('https://docs.google.com/spreadsheets/d/1Xh8-36cLCEa_bppqLJu-nukVgPDT8xVEd9nxEaPYKKg/edit#gid=2018368498') %>%
googlesheets::gs_read()
head(master_list)
### archive copy of the Google Sheet in PRIVATE ohidev repo (not here); will need to push that repo too.
write.csv(master_list,
paste0('~/github/ohidev/OHI_CountriesMaster/OHI_CountriesMaster_', Sys.Date(), '.csv'))
#### parse information for display ----
## to be displayed, `display` column must have either 'point' or 'shapefile'
display_list <- master_list %>%
select(country = Country,
ohi_plus = OHI_plus,
phase = Phase,
map_display = Map_Display,
display = Display,
lat = Latitude,
lon = Longitude) %>%
filter(!is.na(display)) # remove rows that don't have point/shapefile
# tail(display_list)
## count number of active and completed OHI+ countries ----
n_ohi_plus <- display_list %>%
filter(ohi_plus == 'YES')
n_ohi_plus_active <- n_ohi_plus %>%
filter(phase == 'active')
cat(sprintf('Total count of OHI+ assessments is %s!', dim(n_ohi_plus)[1]))
cat(sprintf('And %s are active, %s are completed OHI+ assessments.',
dim(n_ohi_plus_active)[1],
dim(n_ohi_plus)[1]- dim(n_ohi_plus_active)[1]))
## filter shapefiles to display
display_shp <- display_list %>%
filter(map_display == 'shapefile')
## determine whether any shapefiles have been added:
old <- read.csv("assets/maps/regions_shape.csv")
if(length(setdiff(display_shp$country, old$country))>0){
warning('Countries with shapefiles have been added, will need to update the shapefile map')
}
## filter point data to display
display_pt <- display_list %>%
filter(map_display == 'point')
# error if not all fields have lat/lon data. See https://github.com/OHI-Science/issues/issues/625#issuecomment-217511758
if (NA %in% display_pt$lat | NA %in% display_pt$lon){
stop(sprintf('Missing lat or lon field for region to be displayed. Must fix or will not map proplerly.\n'))
}
## determine whether any points have been added:
old <- read.csv("assets/maps/regions_point.csv")
if(length(setdiff(display_pt$country, old$country))>0){
warning('Countries with points have been added, will need to update the point map')
}
if (nrow(display_pt) + nrow(display_shp) != nrow(display_list)) {
warning('please make sure all the countries to display have either shapefiles or lat/longs')
}
write.csv(display_shp, "assets/maps/regions_shape.csv", row.names=FALSE)
write.csv(display_pt, "assets/maps/regions_point.csv", row.names=FALSE)
master_list <- googlesheets::gs_url('https://docs.google.com/spreadsheets/d/1Xh8-36cLCEa_bppqLJu-nukVgPDT8xVEd9nxEaPYKKg/edit#gid=2018368498') %>%
googlesheets::gs_read()
head(master_list)
View(master_list)
write.csv(master_list,
paste0('~/github/ohidev/OHI_CountriesMaster/OHI_CountriesMaster_', Sys.Date(), '.csv'))
display_list <- master_list %>%
select(country = Country,
ohi_plus = OHI_plus,
phase = Phase,
map_display = Map_Display,
display = Display,
lat = Latitude,
lon = Longitude) %>%
filter(!is.na(display)) # remove rows that don't have point/shapefile
display_list <- master_list %>%
dplyr::select(country = Country,
ohi_plus = OHI_plus,
phase = Phase,
map_display = Map_Display,
display = Display,
lat = Latitude,
lon = Longitude) %>%
filter(!is.na(display)) # remove rows that don't have point/shapefile
View(display_shp)
tail(display_list)
n_ohi_plus <- display_list %>%
filter(ohi_plus == 'YES')
n_ohi_plus_active <- n_ohi_plus %>%
filter(phase == 'active')
cat(sprintf('Total count of OHI+ assessments is %s!', dim(n_ohi_plus)[1]))
cat(sprintf('And %s are active, %s are completed OHI+ assessments.',
dim(n_ohi_plus_active)[1],
dim(n_ohi_plus)[1]- dim(n_ohi_plus_active)[1]))
## filter shapefiles to display
display_shp <- display_list %>%
filter(map_display == 'shapefile')
## determine whether any shapefiles have been added:
old <- read.csv("assets/maps/regions_shape.csv")
if(length(setdiff(display_shp$country, old$country))>0){
warning('Countries with shapefiles have been added, will need to update the shapefile map')
}
## filter point data to display
display_pt <- display_list %>%
filter(map_display == 'point')
# error if not all fields have lat/lon data. See https://github.com/OHI-Science/issues/issues/625#issuecomment-217511758
if (NA %in% display_pt$lat | NA %in% display_pt$lon){
stop(sprintf('Missing lat or lon field for region to be displayed. Must fix or will not map proplerly.\n'))
}
## determine whether any points have been added:
old <- read.csv("assets/maps/regions_point.csv")
if(length(setdiff(display_pt$country, old$country))>0){
warning('Countries with points have been added, will need to update the point map')
}
if (nrow(display_pt) + nrow(display_shp) != nrow(display_list)) {
warning('please make sure all the countries to display have either shapefiles or lat/longs')
}
write.csv(display_shp, "assets/maps/regions_shape.csv", row.names=FALSE)
write.csv(display_pt, "assets/maps/regions_point.csv", row.names=FALSE)
### This script creates the leaflet map for the ohi-science.org/projects/ohi-assessments
### NOTE: The two things created by leaflet must be moved after creation
## allRegions_files folder goes to: /projects/ohi-assessments. BY HAND! (for now). But do each time.
## allRegions.html goes to: _includes/themes/OHI/maps
# For more information: http://zevross.com/blog/2014/04/11/using-r-to-quickly-create-an-interactive-online-map-using-the-leafletr-package/
# http://stackoverflow.com/questions/26435861/how-to-read-a-geojson-file-containing-feature-collections-to-leaflet-shiny-direc
# http://www.tuicode.com/article/5637dded499808840885af68
### Need to read in the shape files for each of these regions:
library(tidyverse)
library(sp)
library(rgdal)
library(raster)
# library(leafletR) # alternative method for visualization
library(leaflet)
library(htmlwidgets)
library(jsonlite)
library(RColorBrewer)
library(rgeos)
####################
# set directories
dir_M <- c('Windows' = '//mazu.nceas.ucsb.edu/ohi',
'Darwin' = '/Volumes/ohi', ### connect (cmd-K) to smb://mazu/ohi
'Linux' = '/home/shares/ohi')[[ Sys.info()[['sysname']] ]]
if (Sys.info()[['sysname']] != 'Linux' & !file.exists(dir_M)){
warning(sprintf("The Mazu directory dir_M set in src/R/common.R does not exist. Do you need to mount Mazu: %s?", dir_M))
}
####################
# uses leaflet and htmlwidgets to save html file <- 2 options for PC/Mac
region_poly <- readOGR(dsn='/var/data/ohi/git-annex/Global/NCEAS-Regions_v2014/data/website_OHIplus_regions',
layer="allRegions")
# region_poly <- readOGR(dsn=file.path(dir_M, 'git-annex/Global/NCEAS-Regions_v2014/data/website_OHIplus_regions'),
# layer="allRegions")
region_poly_data <- read.csv('assets/maps/regions_shape.csv')
## This is run in case there are polygons in the shapefile that are no longer included:
region_poly <- region_poly[region_poly@data$country %in% region_poly_data$country, ]
## Add color data
colors <- data.frame(phase = c("active", "completed"),
color= c('#0083A3', '#00ADDD'))
region_poly@data <- region_poly@data %>%
left_join(region_poly_data, by="country") %>%
left_join(colors, by="phase")
popup_poly <- paste0('<b>', region_poly@data$display, '</b>',
'<br/>', "status: ", region_poly@data$phase)
## The data we want to display as points:
points <- read.csv("assets/maps/regions_point.csv")
popup_points <- paste0('<b>', points$display, '</b>',
'<br/>', "status: ", points$phase) # use this for new line: , "<br/>")
## point ideas:
##https://sites.google.com/site/gmapsdevelopment/
icon_new <- makeIcon(
iconUrl = "http://maps.google.com/mapfiles/ms/micons/blue-dot.png",
iconWidth = 25, iconHeight = 27,
iconAnchorX = 15, iconAnchorY =27)
m <- leaflet(width="100%", height="600px") %>%
setView(-30, 30, 2) %>%
addTiles(options=tileOptions(noWrap=TRUE)) %>%
# addTiles(options=tileOptions(minZoom=3, noWrap=TRUE)) %>%
#addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
# addTiles(options = tileOptions(noWrap = TRUE)) %>%
# fitBounds(-180, -70, 180, 80) %>%
addPolygons(data = region_poly,
#fillColor = myPalette(nrow(regionAll)),
fillColor = region_poly@data$color,
popup=popup_poly,
#stroke=FALSE,
color = "#A8A8A8",
weight = 1,
opacity = 0.5,
fillOpacity = 0.4) %>%
addMarkers(data=points, ~lon, ~lat, popup = ~as.character(popup_points), icon=icon_new)
saveWidget(m, file="allRegions.html", selfcontained=FALSE)
### move allRegions.html to the correct place in _includes subdirectory
unlink("_includes/themes/OHI/maps/allRegions.html", recursive=TRUE)
file.rename(from="allRegions.html", to="_includes/themes/OHI/maps/allRegions.html")
unlink("allRegions.html")
### move allRegions_files folder to the correct place in /projects/ohi-assessments
### although this seems overkill, the map won't display if these packages have been updated and there's a mismatch; see https://github.com/OHI-Science/issues/issues/625
unlink("projects/ohi-assessments/allRegions_files", recursive=TRUE) # delete old folder
warning('Now move `/allRegions_files` to `/projects/ohi-assessments/allRegions_files`!')
warning('Seriously, did you move `/allRegions_files` to `/projects/ohi-assessments/allRegions_files`!?')
# we should script this someday but actually a pain to loop through subdirectories and copy
## regions_get_master_list.r
# April 29, 2016 by @jules32
## Get master list of OHI and OHI+ countries from the private Google Spreadsheet 'OHI_CountriesMaster'
## for display on ohi-science.org/projects/ohi-assessments
## https://docs.google.com/spreadsheets/d/1Xh8-36cLCEa_bppqLJu-nukVgPDT8xVEd9nxEaPYKKg/edit#gid=2018368498
#### setup ----
library(googlesheets) # install.packages('googlesheets') # by Jenny Bryan
library(tidyverse)
library(readr)
## authorize googledocs -- requires user interaction with default browser
gs_auth(new_user = TRUE)
#### read in Google Sheet, previously gs_title('OHI_CountriesMaster')----
master_list <- googlesheets::gs_url('https://docs.google.com/spreadsheets/d/1Xh8-36cLCEa_bppqLJu-nukVgPDT8xVEd9nxEaPYKKg/edit#gid=2018368498') %>%
googlesheets::gs_read()
head(master_list)
### archive copy of the Google Sheet in PRIVATE ohidev repo (not here); will need to push that repo too.
write.csv(master_list,
paste0('~/github/ohidev/OHI_CountriesMaster/OHI_CountriesMaster_', Sys.Date(), '.csv'))
#### parse information for display ----
## to be displayed, `display` column must have either 'point' or 'shapefile'
display_list <- master_list %>%
dplyr::select(country = Country,
ohi_plus = OHI_plus,
phase = Phase,
map_display = Map_Display,
display = Display,
lat = Latitude,
lon = Longitude) %>%
dplyr::filter(!is.na(display)) # remove rows that don't have point/shapefile
tail(display_list)
## count number of active and completed OHI+ countries ----
n_ohi_plus <- display_list %>%
filter(ohi_plus == 'YES')
n_ohi_plus_active <- n_ohi_plus %>%
filter(phase == 'active')
cat(sprintf('Total count of OHI+ assessments is %s!', dim(n_ohi_plus)[1]))
cat(sprintf('And %s are active, %s are completed OHI+ assessments.',
dim(n_ohi_plus_active)[1],
dim(n_ohi_plus)[1]- dim(n_ohi_plus_active)[1]))
## filter shapefiles to display
display_shp <- display_list %>%
filter(map_display == 'shapefile')
## determine whether any shapefiles have been added:
old <- read.csv("assets/maps/regions_shape.csv")
if(length(setdiff(display_shp$country, old$country))>0){
warning('Countries with shapefiles have been added, will need to update the shapefile map')
}
## filter point data to display
display_pt <- display_list %>%
filter(map_display == 'point')
# error if not all fields have lat/lon data. See https://github.com/OHI-Science/issues/issues/625#issuecomment-217511758
if (NA %in% display_pt$lat | NA %in% display_pt$lon){
stop(sprintf('Missing lat or lon field for region to be displayed. Must fix or will not map proplerly.\n'))
}
## determine whether any points have been added:
old <- read.csv("assets/maps/regions_point.csv")
if(length(setdiff(display_pt$country, old$country))>0){
warning('Countries with points have been added, will need to update the point map')
}
if (nrow(display_pt) + nrow(display_shp) != nrow(display_list)) {
warning('please make sure all the countries to display have either shapefiles or lat/longs')
}
write.csv(display_shp, "assets/maps/regions_shape.csv", row.names=FALSE)
write.csv(display_pt, "assets/maps/regions_point.csv", row.names=FALSE)
### This script creates the leaflet map for the ohi-science.org/projects/ohi-assessments
### NOTE: The two things created by leaflet must be moved after creation
## allRegions_files folder goes to: /projects/ohi-assessments. BY HAND! (for now). But do each time.
## allRegions.html goes to: _includes/themes/OHI/maps
# For more information: http://zevross.com/blog/2014/04/11/using-r-to-quickly-create-an-interactive-online-map-using-the-leafletr-package/
# http://stackoverflow.com/questions/26435861/how-to-read-a-geojson-file-containing-feature-collections-to-leaflet-shiny-direc
# http://www.tuicode.com/article/5637dded499808840885af68
### Need to read in the shape files for each of these regions:
library(tidyverse)
library(sp)
library(rgdal)
library(raster)
# library(leafletR) # alternative method for visualization
library(leaflet)
library(htmlwidgets)
library(jsonlite)
library(RColorBrewer)
library(rgeos)
####################
# set directories
dir_M <- c('Windows' = '//mazu.nceas.ucsb.edu/ohi',
'Darwin' = '/Volumes/ohi', ### connect (cmd-K) to smb://mazu/ohi
'Linux' = '/home/shares/ohi')[[ Sys.info()[['sysname']] ]]
if (Sys.info()[['sysname']] != 'Linux' & !file.exists(dir_M)){
warning(sprintf("The Mazu directory dir_M set in src/R/common.R does not exist. Do you need to mount Mazu: %s?", dir_M))
}
####################
# uses leaflet and htmlwidgets to save html file <- 2 options for PC/Mac
region_poly <- readOGR(dsn='/var/data/ohi/git-annex/Global/NCEAS-Regions_v2014/data/website_OHIplus_regions',
layer="allRegions")
# region_poly <- readOGR(dsn=file.path(dir_M, 'git-annex/Global/NCEAS-Regions_v2014/data/website_OHIplus_regions'),
# layer="allRegions")
region_poly_data <- read.csv('assets/maps/regions_shape.csv')
## This is run in case there are polygons in the shapefile that are no longer included:
region_poly <- region_poly[region_poly@data$country %in% region_poly_data$country, ]
## Add color data
colors <- data.frame(phase = c("active", "completed"),
color= c('#0083A3', '#00ADDD'))
region_poly@data <- region_poly@data %>%
left_join(region_poly_data, by="country") %>%
left_join(colors, by="phase")
popup_poly <- paste0('<b>', region_poly@data$display, '</b>',
'<br/>', "status: ", region_poly@data$phase)
## The data we want to display as points:
points <- read.csv("assets/maps/regions_point.csv")
popup_points <- paste0('<b>', points$display, '</b>',
'<br/>', "status: ", points$phase) # use this for new line: , "<br/>")
## point ideas:
##https://sites.google.com/site/gmapsdevelopment/
icon_new <- makeIcon(
iconUrl = "http://maps.google.com/mapfiles/ms/micons/blue-dot.png",
iconWidth = 25, iconHeight = 27,
iconAnchorX = 15, iconAnchorY =27)
m <- leaflet(width="100%", height="600px") %>%
setView(-30, 30, 2) %>%
addTiles(options=tileOptions(noWrap=TRUE)) %>%
# addTiles(options=tileOptions(minZoom=3, noWrap=TRUE)) %>%
#addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
# addTiles(options = tileOptions(noWrap = TRUE)) %>%
# fitBounds(-180, -70, 180, 80) %>%
addPolygons(data = region_poly,
#fillColor = myPalette(nrow(regionAll)),
fillColor = region_poly@data$color,
popup=popup_poly,
#stroke=FALSE,
color = "#A8A8A8",
weight = 1,
opacity = 0.5,
fillOpacity = 0.4) %>%
addMarkers(data=points, ~lon, ~lat, popup = ~as.character(popup_points), icon=icon_new)
saveWidget(m, file="allRegions.html", selfcontained=FALSE)
### move allRegions.html to the correct place in _includes subdirectory
unlink("_includes/themes/OHI/maps/allRegions.html", recursive=TRUE)
file.rename(from="allRegions.html", to="_includes/themes/OHI/maps/allRegions.html")
unlink("allRegions.html")
### move allRegions_files folder to the correct place in /projects/ohi-assessments
### although this seems overkill, the map won't display if these packages have been updated and there's a mismatch; see https://github.com/OHI-Science/issues/issues/625
unlink("projects/ohi-assessments/allRegions_files", recursive=TRUE) # delete old folder
warning('Now move `/allRegions_files` to `/projects/ohi-assessments/allRegions_files`!')
warning('Seriously, did you move `/allRegions_files` to `/projects/ohi-assessments/allRegions_files`!?')
# we should script this someday but actually a pain to loop through subdirectories and copy
airquality
summary(mtcars)
library(here)
here()
usethis::use_tidy_thanks("openscapes/demo")
usethis::use_rmarkdown_template()
usethis::use_readme_md()
usethis::use_readme_rmd()
usethis::use_tidy_coc()
#### setup ----
library(googlesheets) # install.packages('googlesheets') # by Jenny Bryan
library(tidyverse)
library(readr)
## authorize googledocs -- requires user interaction with default browser
gs_auth(new_user = TRUE)
#### read in Google Sheet, previously gs_title('OHI_CountriesMaster')----
master_list <- googlesheets::gs_url('https://docs.google.com/spreadsheets/d/1Xh8-36cLCEa_bppqLJu-nukVgPDT8xVEd9nxEaPYKKg/edit#gid=2018368498') %>%
googlesheets::gs_read()
head(master_list)
tail(master_list)
### archive copy of the Google Sheet in PRIVATE ohidev repo (not here); will need to push that repo too.
write.csv(master_list,
paste0('~/github/ohidev/OHI_CountriesMaster/OHI_CountriesMaster_', Sys.Date(), '.csv'))
### archive copy of the Google Sheet in PRIVATE ohidev repo (not here); will need to push that repo too.
write.csv(master_list,
paste0('~/github/ohi-science/ohidev/OHI_CountriesMaster/OHI_CountriesMaster_', Sys.Date(), '.csv'))
#### parse information for display ----
## to be displayed, `display` column must have either 'point' or 'shapefile'
display_list <- master_list %>%
dplyr::select(country = Country,
ohi_plus = OHI_plus,
phase = Phase,
map_display = Map_Display,
display = Display,
lat = Latitude,
lon = Longitude) %>%
dplyr::filter(!is.na(display)) # remove rows that don't have point/shapefile
## count number of active and completed OHI+ countries ----
n_ohi_plus <- display_list %>%
filter(ohi_plus == 'YES')
n_ohi_plus
n_ohi_plus_active <- n_ohi_plus %>%
filter(phase == 'active')
cat(sprintf('Total count of OHI+ assessments is %s!', dim(n_ohi_plus)[1]))
cat(sprintf('And %s are active, %s are completed OHI+ assessments.',
dim(n_ohi_plus_active)[1],
dim(n_ohi_plus)[1]- dim(n_ohi_plus_active)[1]))
## filter shapefiles to display
display_shp <- display_list %>%
filter(map_display == 'shapefile')
## determine whether any shapefiles have been added:
old <- read.csv("assets/maps/regions_shape.csv")
if(length(setdiff(display_shp$country, old$country))>0){
warning('Countries with shapefiles have been added, will need to update the shapefile map')
}
## filter point data to display
display_pt <- display_list %>%
filter(map_display == 'point')
# error if not all fields have lat/lon data. See https://github.com/OHI-Science/issues/issues/625#issuecomment-217511758
if (NA %in% display_pt$lat | NA %in% display_pt$lon){
stop(sprintf('Missing lat or lon field for region to be displayed. Must fix or will not map proplerly.\n'))
}
## determine whether any points have been added:
old <- read.csv("assets/maps/regions_point.csv")
if(length(setdiff(display_pt$country, old$country))>0){
warning('Countries with points have been added, will need to update the point map')
}
if (nrow(display_pt) + nrow(display_shp) != nrow(display_list)) {
warning('please make sure all the countries to display have either shapefiles or lat/longs')
}
write.csv(display_shp, "assets/maps/regions_shape.csv", row.names=FALSE)
write.csv(display_pt, "assets/maps/regions_point.csv", row.names=FALSE)
### Need to read in the shape files for each of these regions:
library(tidyverse)
library(sp)
library(rgdal)
library(raster)
# library(leafletR) # alternative method for visualization
library(leaflet)
library(htmlwidgets)
library(jsonlite)
library(RColorBrewer)
library(rgeos)
####################
# set directories
dir_M <- c('Windows' = '//mazu.nceas.ucsb.edu/ohi',
'Darwin' = '/Volumes/ohi', ### connect (cmd-K) to smb://mazu/ohi
'Linux' = '/home/shares/ohi')[[ Sys.info()[['sysname']] ]]
if (Sys.info()[['sysname']] != 'Linux' & !file.exists(dir_M)){
warning(sprintf("The Mazu directory dir_M set in src/R/common.R does not exist. Do you need to mount Mazu: %s?", dir_M))
}
####################
# uses leaflet and htmlwidgets to save html file <- 2 options for PC/Mac
# region_poly <- readOGR(dsn='/var/data/ohi/git-annex/Global/NCEAS-Regions_v2014/data/website_OHIplus_regions',
# layer="allRegions")
region_poly <- readOGR(dsn=file.path(dir_M, 'git-annex/Global/NCEAS-Regions_v2014/data/website_OHIplus_regions'),
layer="allRegions")
## This is run in case there are polygons in the shapefile that are no longer included:
region_poly <- region_poly[region_poly@data$country %in% region_poly_data$country, ]
region_poly_data <- read.csv('assets/maps/regions_shape.csv')
## This is run in case there are polygons in the shapefile that are no longer included:
region_poly <- region_poly[region_poly@data$country %in% region_poly_data$country, ]
## Add color data
colors <- data.frame(phase = c("active", "completed"),
color= c('#0083A3', '#00ADDD'))
region_poly@data <- region_poly@data %>%
left_join(region_poly_data, by="country") %>%
left_join(colors, by="phase")
popup_poly <- paste0('<b>', region_poly@data$display, '</b>',
'<br/>', "status: ", region_poly@data$phase)
## The data we want to display as points:
points <- read.csv("assets/maps/regions_point.csv")
popup_points <- paste0('<b>', points$display, '</b>',
'<br/>', "status: ", points$phase) # use this for new line: , "<br/>")
## point ideas:
##https://sites.google.com/site/gmapsdevelopment/
icon_new <- makeIcon(
iconUrl = "http://maps.google.com/mapfiles/ms/micons/blue-dot.png",
iconWidth = 25, iconHeight = 27,
iconAnchorX = 15, iconAnchorY =27)
m <- leaflet(width="100%", height="600px") %>%
setView(-30, 30, 2) %>%
addTiles(options=tileOptions(noWrap=TRUE)) %>%
# addTiles(options=tileOptions(minZoom=3, noWrap=TRUE)) %>%
#addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
# addTiles(options = tileOptions(noWrap = TRUE)) %>%
# fitBounds(-180, -70, 180, 80) %>%
addPolygons(data = region_poly,
#fillColor = myPalette(nrow(regionAll)),
fillColor = region_poly@data$color,
popup=popup_poly,
#stroke=FALSE,
color = "#A8A8A8",
weight = 1,
opacity = 0.5,
fillOpacity = 0.4) %>%
addMarkers(data=points, ~lon, ~lat, popup = ~as.character(popup_points), icon=icon_new)
saveWidget(m, file="allRegions.html", selfcontained=FALSE)
### move allRegions.html to the correct place in _includes subdirectory
unlink("_includes/themes/OHI/maps/allRegions.html", recursive=TRUE)
file.rename(from="allRegions.html", to="_includes/themes/OHI/maps/allRegions.html")
unlink("allRegions.html")
### move allRegions_files folder to the correct place in
### /projects/ohi-assessments although this seems overkill, the map won't
### display if these packages have been updated and there's a mismatch; see
### https://github.com/OHI-Science/issues/issues/625
unlink("projects/ohi-assessments/allRegions_files", recursive=TRUE) # delete old folder
warning('Now move `/allRegions_files` to `/projects/ohi-assessments/allRegions_files`!')
warning('Seriously, did you move `/allRegions_files` to `/projects/ohi-assessments/allRegions_files`!?')
source('~/github/ohi-science/ohi-science.github.io/assets/maps/regions_get_master_list.r')
library(googlesheets)
detach("package:googlesheets", unload = TRUE)
remove.packages("googlesheets")
install.packages('googlesheets4')
#### setup ----
library(googlesheets4) # install.packages('googlesheets') # by Jenny Bryan