Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BIS-4D_masterclass
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Helfenstein, Anatol
BIS-4D_masterclass
Commits
8ad5a64d
Commit
8ad5a64d
authored
2 years ago
by
Helfenstein, Anatol
Browse files
Options
Downloads
Patches
Plain Diff
trying to designate dynamic LU covariates as categorical and plot with LU colors
parent
b1043b60
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
00_TUTORIAL_script.R
+89
-19
89 additions, 19 deletions
00_TUTORIAL_script.R
with
89 additions
and
19 deletions
00_TUTORIAL_script.R
+
89
−
19
View file @
8ad5a64d
...
...
@@ -18,10 +18,12 @@
# Load required packages --------------------------------------------------
# Empty memory and workspace; Load required packages ----------------------
gc
()
rm
(
list
=
ls
())
pkgs
<-
c
(
"tidyverse"
,
"foreach"
,
"raster"
,
"terra"
,
"sf"
,
"mapview"
,
"ranger"
,
"caret"
,
"viridis"
)
"caret"
,
"viridis"
,
"hexbin"
)
# make sure 'mapview' pkg installed from github to avoid pandoc error:
# remotes::install_github("r-spatial/mapview")
lapply
(
pkgs
,
library
,
character.only
=
TRUE
)
...
...
@@ -362,45 +364,113 @@ r_stack_LU_1910_present <- stack(
names
(
r_stack_LU_1910_present
)
<-
paste0
(
"LU_"
,
seq
(
1910
,
format
(
Sys.Date
(),
"%Y"
),
1
))
# calculating mode using raster pkg takes 100 times longer than using terra,
# so convert to
s
patRaster using terra and then perform terra::modal()
# so convert to
S
patRaster using terra and then perform terra::modal()
sr_LU_1910_present
<-
rast
(
r_stack_LU_1910_present
)
# Obtain land use for every location, with coordinates x and y for every year t
# between 1953 and 2022, by assigning the same class as in the temporally nearest
# year for which a map is available.
# LU_xyt_1km
for year
19
53
& 2022
sr_LU_xyt_1km
<-
imap
(
c
(
19
53
,
2022
),
~
{
#
Compute
LU_xyt_1km
, here only for 2 of the 70 years, e.g.
19
90
& 2022
sr_LU_xyt_1km
<-
imap
(
c
(
19
90
,
2022
),
~
{
i
<-
.x
sr_LU_1910_present
[[
paste0
(
"LU_"
,
i
)]]
})
%>%
rast
(
.
)
output
<-
sr_LU_1910_present
[[
paste0
(
"LU_"
,
i
)]]
setNames
(
output
,
paste0
(
"LU_"
,
i
))
})
%>%
rast
(
.
)
# Sidenote: since output of foreach is list, rast() combines into multilayer SpatRaster
# In the same manner, assign the land use class that occurred most frequently in
# the 5, 10, 20 and 40 years prior to and including year t...
# LU_xyt_delta5_1km
for year
19
53
& 2022
sr_LU_xyt_delta5_1km
<-
imap
(
c
(
19
53
,
2022
),
~
{
#
Compute
LU_xyt_delta5_1km
, here only for 2 of the 70 years, e.g.
19
90
& 2022
sr_LU_xyt_delta5_1km
<-
imap
(
c
(
19
90
,
2022
),
~
{
i
<-
.x
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
4
,
i
))]],
na.rm
=
TRUE
)
output
<-
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
4
,
i
))]],
na.rm
=
TRUE
)
setNames
(
output
,
paste0
(
"LU_"
,
i
,
"_delta5"
))
})
%>%
rast
(
.
)
# LU_xyt_delta10_1k
m for year
19
53
& 2022
sr_LU_xyt_delta10_1km
<-
imap
(
c
(
19
53
,
2022
),
~
{
#
Compute
LU_xyt_delta10_1k
, here only for 2 of the 70 years, e.g.
19
90
& 2022
sr_LU_xyt_delta10_1km
<-
imap
(
c
(
19
90
,
2022
),
~
{
i
<-
.x
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
9
,
i
))]],
na.rm
=
TRUE
)
output
<-
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
9
,
i
))]],
na.rm
=
TRUE
)
setNames
(
output
,
paste0
(
"LU_"
,
i
,
"_delta10"
))
})
%>%
rast
(
.
)
# LU_xyt_delta20_1km
for year
19
53
& 2022
sr_LU_xyt_delta20_1km
<-
imap
(
c
(
19
53
,
2022
),
~
{
#
Compute
LU_xyt_delta20_1km
, here only for 2 of the 70 years, e.g.
19
90
& 2022
sr_LU_xyt_delta20_1km
<-
imap
(
c
(
19
90
,
2022
),
~
{
i
<-
.x
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
19
,
i
))]],
na.rm
=
TRUE
)
output
<-
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
19
,
i
))]],
na.rm
=
TRUE
)
setNames
(
output
,
paste0
(
"LU_"
,
i
,
"_delta20"
))
})
%>%
rast
(
.
)
# LU_xyt_delta40_1km
for year
19
53
& 2022
sr_LU_xyt_delta40_1km
<-
imap
(
c
(
19
53
,
2022
),
~
{
#
Compute
LU_xyt_delta40_1km
, here only for 2 of the 70 years, e.g.
19
90
& 2022
sr_LU_xyt_delta40_1km
<-
imap
(
c
(
19
90
,
2022
),
~
{
i
<-
.x
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
39
,
i
))]],
na.rm
=
TRUE
)
output
<-
modal
(
sr_LU_1910_present
[[
paste0
(
"LU_"
,
seq
(
i
-
39
,
i
))]],
na.rm
=
TRUE
)
setNames
(
output
,
paste0
(
"LU_"
,
i
,
"_delta40"
))
})
%>%
rast
(
.
)
# combine dynamic LU covariates for 1990 and designate as categorical covariates
sr_LU_xyt_1990_1km
<-
as.factor
(
c
(
sr_LU_xyt_1km
$
LU_1990
,
sr_LU_xyt_delta5_1km
$
LU_1990_delta5
,
sr_LU_xyt_delta10_1km
$
LU_1990_delta10
,
sr_LU_xyt_delta20_1km
$
LU_1990_delta20
,
sr_LU_xyt_delta40_1km
$
LU_1990_delta40
))
# combine dynamic LU covariates for 2022
sr_LU_xyt_2022_1km
<-
c
(
sr_LU_xyt_1km
$
LU_2022
,
sr_LU_xyt_delta5_1km
$
LU_2022_delta5
,
sr_LU_xyt_delta10_1km
$
LU_2022_delta10
,
sr_LU_xyt_delta20_1km
$
LU_2022_delta20
,
sr_LU_xyt_delta40_1km
$
LU_2022_delta40
)
# In the following lines of code, we designate proper names to the LU categories/classes...
# read in tables with reclassified values for dynamic LU covariates
ls_tbl_recl
<-
foreach
(
tbl
=
1
:
length
(
v_cov_dyn_names
))
%do%
readr
::
read_csv
(
paste0
(
"data/covariates/organism/"
,
gsub
(
".grd"
,
""
,
v_cov_dyn_names
)[
tbl
],
"_reclassify_dyn.csv"
))
# tibble of all LU classes used in dynamic LU covariates
tbl_LU_xyt_classes
<-
distinct
(
ls_tbl_recl
[[
length
(
ls_tbl_recl
)]][,
3
:
4
])
%>%
drop_na
()
%>%
rename
(
id
=
1
,
landuse
=
2
)
# list of tibbles of LU classes specific to each dynamic LU covariate
# (not all contain all classes)
ls_tbl_LU_xyt_classes
<-
map2
(
rep
(
list
(
tbl_LU_xyt_classes
),
length
(
nlyr
(
sr_LU_xyt_1990_2022_1km
))),
map
(
as.list
(
sr_LU_xyt_1990_2022_1km
),
~
unique
(
.x
)[,
1
]),
~
filter
(
.x
,
id
%in%
.y
)
)
# designate as categorical variables and assign proper classes
foreach
(
i
=
1
:
length
(
ls_tbl_LU_xyt_classes
))
%do%
{
levels
(
sr_LU_xyt_1990_2022_1km
[[
i
]])
<-
as.data.frame
(
ls_tbl_LU_xyt_classes
[[
i
]])
}
# list of cols for plotting
cols
<-
c
(
"#57c4ad"
,
"#e6e1bc"
,
"#f6d3e8"
,
"#999b99"
,
"#035f05"
,
"#e68ae2"
,
"#eae205"
,
"#e18c0c"
,
"#7809c7"
,
"#f8050f"
,
"#0206fa"
)
cols
[
pull
(
ls_tbl_LU_xyt_classes
[[
1
]],
id
)]
ls_cols
<-
map2
(
rep
(
list
(
cols
),
length
(
nlyr
(
sr_LU_xyt_decades_1km
))),
ls_tbl_LU_xyt_classes
,
~
.x
[
pull
(
.y
,
id
)])
# plot and save plots to disk
foreach
(
i
=
1
:
nlyr
(
sr_LU_xyt_decades_1km
))
%do%
{
pdf
(
paste0
(
"out/maps/explorative/covariates/dynamic/m_"
,
names
(
sr_LU_xyt_decades_1km
)[
i
],
".pdf"
),
width
=
9
,
height
=
9
)
plot
(
sr_LU_xyt_decades_1km
[[
i
]],
col
=
ls_cols
[[
i
]])
dev.off
()
}
# visualize dynamic LU covariate
mapView
(
sr_LU_xyt_delta40_1km
[[
1
]],
#att = "description",
col.regions
=
c
(
""
,
""
,
""
,
"#006164"
,
"#db4325"
,
"white"
,
"#386cb0"
,
"#ffff99"
),
layer.name
=
"Land use around 1960"
)
# Dynamic covariates: peat occurrence -------------------------------------
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment