-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathraster_basics.R
More file actions
230 lines (180 loc) · 6.79 KB
/
Copy pathraster_basics.R
File metadata and controls
230 lines (180 loc) · 6.79 KB
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
here::i_am("install_packages.R")
# To run the code presented in this short-course you will need to have installed
# the following packages: Be sure to update Java to its newest version
packages = c("rsconnect", "av", "bslib",
"countrycode", "cptcity", "deSolve",
"dplyr", "DT", "ggplot2",
"htmltools", "latex2exp", "lattice",
"latticeExtra", "leaflet", "maps",
"markdown", "plotly", "purrr",
"rasterVis", "readr", "readxl",
"sf", "shiny", "shinyalert",
"shinybusy", "shinyhelper", "shinyjs",
"shinyvalidate", "shinyWidgets", "sp",
"stringr", "terra", "tidyverse",
"tinytex", "tools", "writexl",
"fasterize", "magick", "raster",
"rstudioapi", "Matrix", "lubridate",
"plot3D", "colourpicker", "ggspatial")
packagesNotInstalled <-
packages[!(packages %in% installed.packages()[, "Package"])]
if (length(packagesNotInstalled))
install.packages(packagesNotInstalled,
dependencies = TRUE,
repos = "https://cloud.r-project.org")
suppressMessages(
suppressPackageStartupMessages(
sapply(packages, require, character.only = TRUE, simplify = TRUE)
)
)
# It is worth keeping your packages and R version up-to-date. To ensure the
# former, run:
suppressWarnings(update.packages(oldPkgs = packages,
repos = "https://cloud.r-project.org"))
source("R/rasterBasePlot.R")
source("R/rasterWorldPop.R")
selectedCountry <- "Greece"
# selectedCountry <- "Italy"
# selectedCountry <- "Korea"
# selectedCountry <- "Nigeria"
rasterAgg <- 5
## rasterAgg <- 10
## rasterAgg <- 15
## Converts country name to ISO Alpha
inputISO <- countrycode(selectedCountry,
origin = 'country.name',
destination = 'iso3c')
inputISOLower <- tolower(inputISO)
## Construct a URL from which to download a TIF file.
paste("https://data.worldpop.org",
"GIS",
"Population",
"Global_2000_2020_1km_UNadj",
"2020",
inputISO,
"%s_ppp_2020_1km_Aggregated_UNadj.tif",
sep = "/") %>%
sprintf(tolower(inputISO)) ->
url
tifPath <- here("tif", basename(url))
if (!file.exists(tifPath))
download.file(url, tifPath, mode = "wb")
WorldPop <- raster(tifPath)
## WorldPop <- raster(file.choose())
WorldPop
res(WorldPop)
extent(WorldPop)
origin(WorldPop)
crs(WorldPop)
nrow(WorldPop)
ncol(WorldPop)
ncell(WorldPop)
dim(WorldPop)
## Number of grid cells that have a population count = 0
cellStats(WorldPop == 0, sum)
## Number of grid cells that have a population count
cellStats(!is.na(WorldPop), sum)
## Number of grid cells that are NA
cellStats(is.na(WorldPop), sum)
## Total estimated 2020 population count
cellStats(WorldPop, sum)
#---------------------------------------#
# Source 2: From GADM: Level1Identifier #
#---------------------------------------#
## ?readRDS
# Level1Identifier <- gadm(country = inputISO,
# level = 1,
# version = 3.6,
# path = here())
gadmFileName <- paste("gadm36_", inputISOLower, "_1_sp.rds", sep = "") # name of the .rds file
#print(gadmFileName)
#?readRDS
gadmFolder <- "gadm/" # .rds files should be stored in local gadm/ folder
if (file.exists(paste(gadmFolder, gadmFileName, sep = ""))) {
Level1Identifier <- readRDS(paste(gadmFolder, gadmFileName, sep = ""))
} else {
Level1Identifier <- getData("GADM", level = 1, country = inputISOLower)
}
print(Level1Identifier$NAME_1) # List of all States/Provinces/Regions
#-----------------------------#
# PLOTTING A COUNTRY BOUNDARY #
#-----------------------------#
plot(Level1Identifier, main = "Level 1 Administrative Boundaries")
#-------------------#
# PLOTTING A RASTER #
#-------------------#
## plot(WorldPop)
## plot(WorldPop, col = terrain.colors(255))
## par(mfrow = c(1, 2))
## image(log(WorldPop),
## col = heat.colors(10),
## main = sprintf("%s\n%s",
## "heat: 2020 UN-Adjusted Population Count (log-scale)",
## "(each grid cell is 1 km x 1 km)"))
## image(log(WorldPop),
## col = topo.colors(10),
## main = sprintf("%s\n%s",
## "topo: 2020 UN-Adjusted Population Count (log-scale)",
## "(each grid cell is 1 km x 1 km)"))
## plot(log(WorldPop),
## xlab = "Longitude",
## ylab = "Latitude",
## main = sprintf("%s\n%s",
## "2020 UN-Adjusted Population Count (log-scale)",
## "(each grid cell is 1 km x 1 km)"))
# createBasePlot(selectedCountry = selectedCountry,
# rasterAgg = 0,
# directOutput = TRUE)
suscLayer <- createSusceptibleLayer(selectedCountry, 0)
createBasePlot(
selectedCountry = selectedCountry,
susceptible = suscLayer$Susceptible,
directOutput = TRUE
)
#----------------------------------------#
# Switch back to PowerPoint Presentation #
#----------------------------------------#
#----------------------#
# AGGREGATING A RASTER #
#----------------------#
is.na(WorldPop) <- 0
WorldPop_aggr <- aggregate(WorldPop,
fact = c(rasterAgg, rasterAgg),
fun = sum,
na.rm = TRUE)
is.na(WorldPop_aggr) <- 0
names(WorldPop_aggr) <- "Susceptible"
WorldPop_aggr
summary(getValues(WorldPop_aggr))
print(sprintf("Population count before and after aggregation is equal? %s",
cellStats(WorldPop, sum) == cellStats(WorldPop_aggr, sum)))
#----------------------------------------#
# Switch back to PowerPoint Presentation #
#----------------------------------------#
#--------------------#
# EXPORTING A RASTER #
#--------------------#
## writeRaster(WorldPop,
## filename = sprintf("%s_unaggregated.nc", inputISO),
## format = "CDF",
## varname = "Susceptible",
## varunit = "Persons",
## longname = "Susceptible",
## overwrite = TRUE)
## writeRaster(WorldPop_aggr,
## filename = sprintf("%s_aggr_0000.nc", inputISO),
## format = "CDF",
## varname = "Susceptible",
## varunit = "Persons",
## longname = "Susceptible",
## overwrite = TRUE)
# writeRaster(WorldPop_aggr,
# filename = sprintf("%s_aggr_0000.tif", inputISO),
# format = "GTiff",
# varname = "Susceptible",
# varunit = "Persons",
# longname = "Susceptible",
# overwrite = TRUE)
#----------------------------------------#
# Switch back to PowerPoint Presentation #
#----------------------------------------#