-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 17.8 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
library(sf)
library(tmap)
tmap_mode("view")
library(terra)
# library(httr2)
# library(ncdf4)
library(stars)
# library(lubridate)
# library(leaflet)
# library(leaflet.extras2)
# library(dplyr)
########################################################
#https://www.eea.europa.eu/en/datahub/datahubitem-view/6fc8ad2d-195d-40f4-bdec-576e7d1268e4
setwd("C:/Users/Administrator/Desktop/test_analysis/corridors/corridors_chr")
NATURA <- st_read("Natura/natura_WGS84.shp")
merged_Med <- st_read("examined_area.shp")
normalize_weights<-TRUE
remove_parts<-"NO"
# Origin shapefiles (could be a single one shapefile)
start_code<- "GR3000019"
start_area<-NATURA[NATURA[,1][[1]]==start_code,]
start_code_2<-"GR4220005"
start_area_2<-NATURA[NATURA[,1][[1]]==start_code_2,]
start_code_3<-"GR4210021"
start_area_3<-NATURA[NATURA[,1][[1]]==start_code_3,]
start_code_4<-"GR4210026"
start_area_4<-NATURA[NATURA[,1][[1]]==start_code_4,]
# Destination shapefile
end_code<-"GR4320006"
end_area<-NATURA[NATURA[,1][[1]]==end_code,]
rm(NATURA) #we delete it from memory because its very heavy
#https://data.marine.copernicus.eu/product/MEDSEA_ANALYSISFORECAST_PHY_006_013/description
nc_data <- read_stars("currents.nc")
currents_xy <- nc_data[,,,1, drop = TRUE]
cost_raster_input_x <- rast(currents_xy[1])
crs(cost_raster_input_x) <- st_crs(merged_Med)$wkt
cost_raster_yes_x <- crop(cost_raster_input_x, st_bbox(merged_Med)[c(1,3,2,4)], mask=TRUE)
cost_raster_input_y <- rast(currents_xy[2])
crs(cost_raster_input_y) <- st_crs(merged_Med)$wkt
cost_raster_yes_y <- crop(cost_raster_input_y, st_bbox(merged_Med)[c(1,3,2,4)], mask=TRUE)
rm(nc_data) #we delete it from memory because its very heavy
rm(currents_xy) #we delete it from memory because its very heavy
end_areas <- end_area
origin_areas <- rbind(start_area, start_area_2, start_area_3, start_area_4)
source("C:/Users/Administrator/Documents/packages/SeaGames/R/sea_game_theory.R")
npoints <- 10
mask_shapefile <- NULL
k_neighbors <- 7
nearest_grid_nodes <- 4
niters <- 50
lambda <- 1
component_u <- cost_raster_yes_x
component_v <- cost_raster_yes_y
destination_areas <- start_area_2#end_area
origin_areas <- start_area#rbind(start_area, start_area_2, start_area_3, start_area_4)
all_networks <- FALSE
component_u <- crop(component_u, ext(rbind(origin_areas, destination_areas)))
component_v <- crop(component_v, ext(rbind(origin_areas, destination_areas)))
# for (lambda in c(1e-6, 1, 1e+6)){
set.seed(42)
tic <- Sys.time()
out <- SeaGames(component_u, component_v, origin_areas, destination_areas,
npoints = npoints, lambda = lambda, niters = niters,
k_neighbors = k_neighbors,
nearest_grid_nodes = nearest_grid_nodes,
mask_shapefile = NULL, all_networks = FALSE)
Sys.time() - tic
# Several parts of the code for Sea Currents to Connectivity Transformation
# are taken from the SeaGraphs R package
# https://github.com/cadam00/SeaGraphs
if (!is(component_u, "SpatRaster"))
stop("component_u must be a SpatRaster object.")
if (!is(component_v, "SpatRaster"))
stop("component_v must be a SpatRaster object.")
if ( (npoints <= 0) )
stop("npoints must must be a positive number.")
if (npoints != as.integer(npoints))
stop("npoints must be an integer number.")
if ( npoints < nrow(origin_areas))
warning(
paste0("npoints < nrow(origin_areas). Some origin areas will",
" not have any point.")
)
if ( npoints < nrow(destination_areas))
warning(
paste0("npoints < nrow(destination_areas). Some destination areas will",
" not have any point.")
)
if ( (k_neighbors <= 0) )
stop("k_neighbors must must be a positive number.")
if (k_neighbors != as.integer(k_neighbors))
stop("k_neighbors must be an integer number.")
if ( (nearest_grid_nodes <= 0) )
stop("nearest_grid_nodes must must be a positive number.")
if (nearest_grid_nodes != as.integer(nearest_grid_nodes))
stop("nearest_grid_nodes must be an integer number.")
if ( (niters < 0) )
stop("niters must must be a non-negative number.")
if (niters != as.integer(niters))
stop("niters must be an integer number.")
crs_component_u <- crs(component_u)
crs_component_v <- crs(component_v)
if (crs_component_u != crs_component_v)
stop(
paste0("Different coordinate system between component_u and component_v.",
" Check with crs(component_u) and crs(component_v).")
)
if (ext(component_u) != ext(component_v))
stop(
paste0("Different extent between component_u and component_v.",
" Check with ext(component_u) and ext(component_v).")
)
if (any(res(component_u) != res(component_v)))
stop(
paste0("Different resolution between component_u and component_v.",
" Check with res(component_u) and res(component_v).")
)
if (!((is(origin_areas, "sf") && is(origin_areas, "data.frame")) |
is(origin_areas, "sfc")))
stop(
paste0("origin_areas arguement must be both sf and data.frame or sfc.")
)
if (!((is(destination_areas, "sf") && is(destination_areas, "data.frame")) |
is(destination_areas, "sfc")))
stop(
paste0("destination_areas arguement must be both sf and data.frame or sfc.")
)
if (!all(st_is(origin_areas, 'MULTIPOLYGON') |
st_is(origin_areas, 'sfc_MULTIPOLYGON') |
st_is(origin_areas, 'POLYGON') |
st_is(origin_areas, 'sfc_POLYGON')
))
stop(
"origin_areas must be rows of 'POLYGON' or'MULTIPOLYGON' geometry type.")
if (!all(st_is(destination_areas, 'MULTIPOLYGON') |
st_is(destination_areas, 'sfc_MULTIPOLYGON') |
st_is(destination_areas, 'POLYGON') |
st_is(destination_areas, 'sfc_POLYGON')
))
stop(
"destination_areas must be rows of 'POLYGON' or'MULTIPOLYGON' geometry type.")
if (st_crs(origin_areas)$wkt != crs_component_u){
warning(
paste0(
"Different resolution or coordinates among component_u and",
" origin_areas:\nproject crs(component_u) on origin_areas."
)
)
origin_areas <- st_transform(origin_areas, crs = crs_component_u)
}
if (st_crs(destination_areas)$wkt != crs_component_u){
warning(
paste0(
"Different resolution or coordinates among component_u and",
" destination_areas:\nproject crs(component_u) on destination_areas."
)
)
destination_areas <- st_transform(destination_areas, crs = crs_component_u)
}
if (!is.null(mask_shapefile)){
is_mask_shapefile_sf <- is(mask_shapefile, "sf")
is_mask_shapefile_SpatVector <- is(mask_shapefile, "SpatVector")
if (is_mask_shapefile_sf) {
if (st_crs(mask_shapefile)$wkt != crs_component_u){
warning(
paste0(
"Different resolution or coordinates among component_u and",
" mask_shapefile:\nproject crs(component_u) on mask_shapefile"
)
)
mask_shapefile <- st_transform(mask_shapefile, crs = crs_component_u)
}
} else if (is_mask_shapefile_SpatVector) {
if (crs(mask_shapefile) != crs_component_u){
warning(
paste0(
"Different resolution or coordinates among component_u and",
" mask_shapefile:\nproject crs(component_v) on mask_shapefile")
)
mask_shapefile <- project(mask_shapefile, crs_component_u)
}
} else {
stop("mask_shapefile must be either 'sf' or 'SpatVector' class.")
}
component_u <- crop(component_u, mask_shapefile, mask=TRUE)
component_v <- crop(component_v, mask_shapefile, mask=TRUE)
}
sf_u <- st_as_sf(as.polygons(component_u))
origin_areas <- st_intersection(origin_areas, sf_u)
destination_areas <- st_intersection(destination_areas, sf_u)
st_crs(origin_areas)$wkt == crs_component_u
?st_intersection
covr::report()
covr::report()
library(SeaGames)
library(sf)
set.seed(42, "Mersenne-Twister", sample.kind="Rejection")
component_u <- get_component_u()
component_v <- get_component_v()
origin_areas <- get_origin_areas()
destination_areas <- get_destination_areas()
out <- suppressWarnings(
SeaGames(component_u, component_v, origin_areas,
destination_areas, npoints = 1, lambda = 1,
niters = 10, k_neighbors = 7, nearest_grid_nodes = 4,
mask_shapefile = NULL, all_networks = FALSE)
)
withWarnings <- function(expr) {
myWarnings <- NULL
wHandler <- function(w) {
myWarnings <<- c(myWarnings, list(w))
invokeRestart("muffleWarning")
}
val <- withCallingHandlers(expr, warning = wHandler)
list(value = val, warnings = myWarnings)
}
masked_result
masked_result <- suppressWarnings(
withWarnings(SeaGames(component_u, component_v, origin_areas,
destination_areas, npoints = 1, lambda = 1,
niters = 2, k_neighbors = 7,
nearest_grid_nodes = 4,
mask_shapefile = mask_shapefile,
all_networks = FALSE)))$warnings
## Examples of mask usage
mask_shapefile <- st_as_sf(st_as_sfc(st_bbox(component_u),
crs=terra::crs(component_u)))
mask_shapefile <- st_crop(mask_shapefile, terra::ext(mask_shapefile) / 1.2)
masked_result <- suppressWarnings(
withWarnings(SeaGames(component_u, component_v, origin_areas,
destination_areas, npoints = 1, lambda = 1,
niters = 2, k_neighbors = 7,
nearest_grid_nodes = 4,
mask_shapefile = mask_shapefile,
all_networks = FALSE)))$warnings
is(masked_result[[3]]
,"simpleWarning")
masked_result
masked_result <- suppressWarnings(
withWarnings(SeaGames(component_u, component_v,
st_transform(origin_areas,
crs = "+init=EPSG:4269"),
st_transform(destination_areas,
crs = "+init=EPSG:4269"),
npoints = 1, lambda = 1,
niters = 2, k_neighbors = 7,
nearest_grid_nodes = 4,
mask_shapefile =
st_transform(mask_shapefile,
crs = "+init=EPSG:4269"),
all_networks = FALSE)))$warnings
masked_result
covr::report()
library(SeaGames)
set.seed(42)
component_u <- get_component_u()
component_v <- get_component_v()
origin_areas <- get_origin_areas()
destination_areas <- get_destination_areas()
# Warnings are thrown because of reassuring that components u/v and
# origin/destination areas are intersecting
out <- SeaGames(component_u, component_v, origin_areas,
destination_areas, npoints = 1, niters = 10)
library(SeaGames)
library(terra)
## terra 1.7.83
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
tmap_mode("view")
## tmap mode set to interactive viewing
# Get example u and v components
component_u <- get_component_u()
component_v <- get_component_v()
# Plot each component
par(mfrow=c(1,2), las=1)
plot(component_u, main="u")
plot(component_v, main="v")
origin_areas <- get_origin_areas()
destination_areas <- get_destination_areas()
tm_shape(st_geometry(origin_areas)) +
tm_polygons(fill="x") +
tm_shape(st_geometry(destination_areas)) +
tm_polygons(fill="x")
tm_shape(origin_areas) +
tm_polygons(fill="x") +
tm_shape(destination_areas) +
tm_polygons(fill="x")
tm_shape(origin_areas) +
tm_polygons() +
tm_shape(destination_areas) +
tm_polygons()
tm_shape(origin_areas) +
tm_polygons(col="#1AFF1A") +
tm_shape(destination_areas) +
tm_polygons(col="#FFC20A")
tm_shape(origin_areas, col="#1AFF1A") +
tm_polygons() +
tm_shape(destination_areas, col="#FFC20A") +
tm_polygons()
sf::rbind
citation('SeaGames')
corridors
set.seed(42)
# Warnings are thrown because of reassuring that components u/v and
# origin/destination areas are intersecting
corridors <- SeaGames(component_u = component_u,
component_v = component_v,
origin_areas = origin_areas,
destination_areas = destination_areas,
npoints = 1,
niters = 10)
set.seed(42)
# Warnings are thrown because of reassuring that components u/v and
# origin/destination areas are intersecting
corridors <- SeaGames(component_u = component_u,
component_v = component_v,
origin_areas = origin_areas,
destination_areas = destination_areas,
npoints = 10,
niters = 10)
set.seed(42)
# Warnings are thrown because of reassuring that components u/v and
# origin/destination areas are intersecting
corridors <- SeaGames(component_u = component_u,
component_v = component_v,
origin_areas = origin_areas,
destination_areas = destination_areas,
npoints = 10,
niters = 50)
out$net_result_congestion
library(sf)
names(corridors$solution_edges)[6] <- "uₗ"
tm_shape(st_as_sf(out$net_result_congestion, "edges"))+
tm_lines(col = "black",alpha = 0.05) +
tm_shape(corridors$solution_edges) +
tm_lines(col = "uₗ",lwd="uₗ", scale=10,
palette=colorRampPalette(c("blue", "red"))(10), n=10)+
tm_shape(out$origin_points) + tm_dots(col="#1AFF1A", size=0.1) +
tm_shape(out$destination_points) + tm_dots(col="#FFC20A", size=0.1)
names(corridors$solution_edges)[6] <- "uₗ"
tm_shape(st_as_sf(out$net_result_congestion, "edges"))+
tm_lines(col = "black",alpha = 0.05) +
tm_shape(corridors$solution_edges) +
tm_lines(col = "uₗ",lwd="uₗ", scale=10,
palette=colorRampPalette(c("blue", "red"))(10), n=10)+
tm_shape(corridors$origin_points) + tm_dots(col="#1AFF1A", size=0.1) +
tm_shape(corridors$destination_points) + tm_dots(col="#FFC20A", size=0.1)
names(corridors$solution_edges)[6] <- "uₗ"
tm_shape(st_as_sf(corridors$net_result_congestion, "edges"))+
tm_lines(col = "black",alpha = 0.05) +
tm_shape(corridors$solution_edges) +
tm_lines(col = "uₗ",lwd="uₗ", scale=10,
palette=colorRampPalette(c("blue", "red"))(10), n=10)+
tm_shape(corridors$origin_points) + tm_dots(col="#1AFF1A", size=0.1) +
tm_shape(corridors$destination_points) + tm_dots(col="#FFC20A", size=0.1)
library(ggplot2)
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc_times_c_l)
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc_times_c_l,
type = "l")
par(mfrow=1, las=1)
par(mfrow=c(1,1), las=1)
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc_times_c_l,
type = "l")
set.seed(42)
# Warnings are thrown because of reassuring that components u/v and
# origin/destination areas are intersecting
corridors <- SeaGames(component_u = component_u,
component_v = component_v,
origin_areas = origin_areas,
destination_areas = destination_areas,
npoints = 10,
niters = 100)
names(corridors$solution_edges)[6] <- "uₗ"
tm_shape(st_as_sf(corridors$net_result_congestion, "edges"))+
tm_lines(col = "black",alpha = 0.05) +
tm_shape(corridors$solution_edges) +
tm_lines(col = "uₗ",lwd="uₗ", scale=10,
palette=colorRampPalette(c("blue", "red"))(10), n=10)+
tm_shape(corridors$origin_points) + tm_dots(col="#1AFF1A", size=0.1) +
tm_shape(corridors$destination_points) + tm_dots(col="#FFC20A", size=0.1)
par(mfrow=c(1,1), las=1)
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc_times_c_l,
type = "l")
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
type = "l")
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
ylab = bquote(RMSE(u_{l_{_{perc}}}),
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
title = expression(RMSE(u_{l_{_{perc}}}),
expression(RMSE(u_{l_{_{perc}}})
expression(RMSE(u[l[[perc]]])
)
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
title = expression(RMSE(u[l[[perc]]])),
lwd = 2,
type = "l")
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
main = expression(RMSE(u[l[[perc]]])),
lwd = 2,
type = "l")
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
main = expression(RMSE(u[l[perc]])),
lwd = 2,
type = "l")
plot(x = corridors$metrics_df$niters,
y = corridors$metrics_df$rmse_u_l_perc,
main = expression(RMSE(u[l[perc]])),
xlab = "Number of iterations",
ylab = "",
lwd = 2,
type = "l")
tm_shape(st_as_sf(corridors$net_result_congestion, "edges"))+
tm_lines(col = "black",alpha = 0.05) +
tm_shape(corridors$solution_edges) +
tm_lines(col = "uₗ",lwd="uₗ", scale=10,
palette=colorRampPalette(c("blue", "red"))(10), n=10)+
tm_shape(corridors$origin_points) + tm_dots(col="#1AFF1A", size=0.1) +
tm_shape(corridors$destination_points) + tm_dots(col="#FFC20A", size=0.1)
devtools::build_manual()
SeaGames
EGCorrs::get_origin_areas()
EGCorrs::get_origin_areas()
ext(EGCorrs::get_origin_areas())
sf::ext(EGCorrs::get_origin_areas())
sf::st_bbox(EGCorrs::get_origin_areas())[1:4]
sf::st_bbox(EGCorrs::get_origin_areas())[1:4]
print(sf::st_bbox(EGCorrs::get_origin_areas())[1:4], digits=13)
print(sf::st_bbox(EGCorrs::get_origin_areas())[1:4], digits=15)
dim(origin_areas)
origin_areas <- EGCorrs::get_origin_areas()
all.equal(terra::ext(origin_areas)[1:4],
c(xmin = 22.8696861096665,
ymin = 36.0416671380519,
xmax = 23.1385529333280,
ymax = 36.3942126714625
)
)
all.equal(sf::st_bbox(origin_areas)[1:4],
c(xmin = 22.8696861096665,
ymin = 36.0416671380519,
xmax = 23.1385529333280,
ymax = 36.3942126714625
)
)
print(sf::st_bbox(EGCorrs::get_origin_areas())[1:4], digits=16)
all.equal(sf::st_bbox(origin_areas)[1:4],
c(xmin = 22.86968610966647,
ymin = 36.04912309180337,
xmax = 23.13855293332797,
ymax = 36.39421267146248
)
)
dim(origin_areas)
print(sf::st_bbox(EGCorrs::get_destination_areas())[1:4], digits=16)
1794.93 / 60
1794.93 / 60 * 50
1794.93 / 60 / 60
0.5 * 50
use_gpl_license(version = 3, include_future = TRUE)
usethis::use_gpl_license(version = 3, include_future = TRUE)
devtools::build_manual()
withr::with_options(list(repos = c(CRAN = "https://cloud.r-project.org/")),
{callr::default_repos()
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran")) })
withr::with_options(list(repos = c(CRAN = "https://cloud.r-project.org/")),
{callr::default_repos()
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran")) })
withr::with_options(list(repos = c(CRAN = "https://cloud.r-project.org/")),
{callr::default_repos()
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran")) })
devtools::build_manual()
usethis::use_coverage(type="codecov")
usethis::use_pkgdown()
usethis::use_github()
usethis::use_git()
usethis::use_github()
usethis::use_github(protocol="https")
pkdown::build_site_github_pages()
pkgdown::build_site_github_pages()
pkgdown::build_site()
pkgdown::build_site()
usethis::use_github_action_check_standard()
use_github_action("check-standard")
usethis::use_github_action("check-standard")
usethis::use_coverage(type="codecov")
usethis::use_coverage()
usethis::use_github_action()
usethis::use_pkgdown()
usethis::use_git()
usethis::use_github(protocol="https")
pkgdown::build_site_github_pages()
uninstall.packages("SeaGames")
remove.packages("SeaGames")
pkgdown::build_site()
# Run once to configure your package to use and deploy pkgdown
usethis::use_pkgdown_github_pages()
sethis::use_pkgdown()
usethis::use_pkgdown()
usethis::use_pkgdown()
pkgdown::build_site()
pkgdown::build_site()