Skip to content

Commit 9431609

Browse files
authored
use worldpop in quickstart vignette (#231)
1 parent 0cceafe commit 9431609

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## General
44

5-
- Quickstart vignette uses the ESA Landcover resource
6-
instead of CHIRPS (#201).
5+
- Quickstart vignette uses the WorldPop resource instead of CHIRPS, not
6+
relying on a working internet connection (#230).
77

88
## New features
99

vignettes/quickstart.Rmd

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ are important for the subsequent processing. The function will add a unique
9898
identifier column called 'assetid' that is used to uniquely identify each
9999
asset in the portfolio.
100100

101-
```{r init_portfolio, dpi = 50, out.width="120%", out.height="120%"}
101+
```{r init_portfolio}
102102
# copying package internal resource to a temporary location
103103
outdir <- file.path(tempdir(), "mapme.biodiversity")
104104
dir.create(outdir)
@@ -107,7 +107,7 @@ file.copy(resource_dir, outdir, recursive = TRUE)
107107
108108
sample_portfolio <- init_portfolio(
109109
x = aoi_gridded,
110-
years = 2015,
110+
years = 2010:2015,
111111
outdir = file.path(outdir, "res"),
112112
tmpdir = outdir,
113113
verbose = TRUE
@@ -148,19 +148,20 @@ function. For this, we inspect the names of the returned object:
148148
names(available_indicators())
149149
```
150150

151-
Say, we are interested in the landcover indicator.
151+
Say, we are interested in the `population_count` indicator.
152152
We can learn more about this indicator and its required resources by using
153153
either of the commands below or, if you are viewing the online version, head
154-
over to the [landcover](https://mapme-initiative.github.io/mapme.biodiversity/reference/landcover.html) documentation.
154+
over to the [population_count](https://mapme-initiative.github.io/mapme.biodiversity/reference/population_count.html) documentation.
155155

156156
```{r helppage_indicator, eval = FALSE}
157-
?landcover
158-
help(landcover)
157+
?population_count
158+
help(population_count)
159159
```
160160

161161
By inspecting the help page we learned that this indicator requires the
162-
`esalandcover` resource and it does not require to specify and further
163-
arguments.
162+
`worldpop` resource and it requires to specify two extra arguments: the population
163+
statistic to calculate and the eninge to be used for the calculation (learn
164+
more about engines [here](https://mapme-initiative.github.io/mapme.biodiversity/articles/terminology.html)).
164165

165166
With that information at hand, we can start to retrieve the required resource.
166167
We can learn about all available resources using the <code>available_resources()</code>
@@ -170,27 +171,27 @@ function:
170171
names(available_resources())
171172
```
172173

173-
For the purpose of this vignette, we are going to download the `esalandcover`
174+
For the purpose of this vignette, we are going to download the `worldpop`
174175
resource. We can get more detailed information about a given resource, by using
175176
either of the commands below to open up the help page. If you are viewing the
176177
online version of this documentation, you can simply head over to the
177-
[esalandcover](https://mapme-initiative.github.io/mapme.biodiversity/reference/esalandcover.html)
178+
[worldpop](https://mapme-initiative.github.io/mapme.biodiversity/reference/worldpop.html)
178179
resource documentation.
179180

180181
```{r helppage_resource, eval = FALSE}
181-
?esalandcover
182-
help(esalandcover)
182+
?worldpop
183+
help(worldpop)
183184
```
184185

185-
We can now make the `esalandcover` resource available for our portfolio. We will
186+
We can now make the `worldpop` resource available for our portfolio. We will
186187
use a common interface that is used for all resources, called <code>get_resources()</code>.
187188
We have to specify our portfolio object and the names of the resource(s) we wish
188189
to download. Additional arguments for the specific resource can be specified.
189190
The output of the function is the portfolio object with its attributes appended
190191
for the new resource, thus we simply can overwrite the `sample_portfolio` variable.
191192

192193
```{r get_esalandcover}
193-
sample_portfolio <- get_resources(x = sample_portfolio, resources = "esalandcover")
194+
sample_portfolio <- get_resources(x = sample_portfolio, resources = "worldpop")
194195
```
195196

196197
In case you want to download more than one resource, you can use the same interface
@@ -200,7 +201,7 @@ for a resource are simply added as usual:
200201
```{r get_multi_resources, eval = FALSE}
201202
sample_portfolio <- get_resources(
202203
x = sample_portfolio,
203-
resources = c("esalandcover", "gfw_treecover"),
204+
resources = c("worldpop", "gfw_treecover"),
204205
vers_treecover = "GFC-2021-v1.9"
205206
)
206207
```
@@ -210,55 +211,47 @@ sample_portfolio <- get_resources(
210211
The next step consists of calculating specific indicators. Note that each
211212
indicator requires one or more resources that were made available via the
212213
<code>get_resources()</code> function explained above. Here, we are going
213-
to calculate the `landcover` indicator which is based on the `esalandcover`
214+
to calculate the `population_count` indicator which is based on the `worldpop`
214215
resource. Since the resource has been made available in the previous step, we
215216
can continue requesting the calculation of our desired indicator. Note the
216217
command below would issue an error in case a required resource has not been made
217218
available via <code>get_resources()</code> beforehand.
218219

219220
```{r calc_indicator}
220-
sample_portfolio <- calc_indicators(sample_portfolio, indicators = "landcover")
221+
sample_portfolio <- calc_indicators(sample_portfolio, indicators = "population_count",
222+
stats_popcount = "sum", engine = "zonal")
221223
```
222224

223225
Now let's take a look at the results. We will select only some of the metadata
224226
and the output indicator column to get a clearer picture of what has happened.
225227

226228
```{r select_cols}
227-
(sample_portfolio <- sample_portfolio %>% select(assetid, WDPAID, landcover))
229+
(sample_portfolio <- sample_portfolio %>% select(assetid, WDPAID, population_count))
228230
```
229231

230232
We obtained a new listed column in our sf object that is called like the requested
231233
indicator. For each asset in our portfolio, this column contains a tibble with
232-
variable rows and 4 columns. Let's have a closer look at one of these objects.
234+
6 rows and two columns. Let's have a closer look at one of these objects.
233235

234236
```{r investigate_indicator}
235-
sample_portfolio$landcover[10]
237+
sample_portfolio$population_count[10]
236238
```
237239

238-
For each asset, the result is a tibble in long format indicating the absolute
239-
area (in ha) and percentage of different landcover types for the year 2015
240-
(make sure to read the detailed indicator documentation via `?landcover`).
241-
Let's quickly visualize the results for a single asset. For readability of the
242-
plot, we will aggregate several different types of forest into a single
243-
class before:
240+
For each asset, the result is a tibble in long format indicating the population
241+
sum per year (make sure to read the detailed indicator documentation via `?population_count`).
242+
Let's quickly visualize the results for a single asset:
244243

245-
```{r plot_landcover, echo = FALSE, dpi = 50, out.width="120%", out.height="120%"}
244+
```{r plot_landcover, echo = FALSE}
246245
data <- sample_portfolio %>%
247246
filter(assetid == 10) %>%
248247
st_drop_geometry() %>%
249-
unnest(landcover)
250-
251-
perc <- data$percentage
252-
names(perc) <- data$classes
253-
perc <- sort(perc, decreasing = TRUE)
254-
index_forest <- grep("forest", names(perc))
255-
perc <- c(sum(perc[index_forest]), perc[-index_forest])
256-
names(perc)[1] = "forest"
257-
258-
barplot(perc, main = "Distribution of landcover in 2015",
259-
xlab = "Landcover class", ylab = "Percentage / 100",
260-
col = c("forestgreen", "orange", "lightgreen"))
248+
unnest(population_count)
261249
250+
pop <- data$popcount_sum
251+
names(pop) <- data$year
252+
barplot(pop, main = "Population totals over time",
253+
xlab = "Year", ylab = "Persons",
254+
col = "steelblue")
262255
```
263256

264257
If you wish to conduct your statistical analysis in R, you can use `{tidyr}` functionality
@@ -270,7 +263,7 @@ to keep the size of the data object relatively small.
270263
geometries <- select(sample_portfolio, assetid)
271264
sample_portfolio %>%
272265
st_drop_geometry() %>%
273-
tidyr::unnest(landcover) %>%
266+
tidyr::unnest(population_count) %>%
274267
filter(assetid == 3)
275268
```
276269

@@ -298,7 +291,9 @@ plan(multisession, workers = 6) # set up parallel plan with 6 concurrent threads
298291
with_progress({
299292
portfolio <- calc_indicators(
300293
sample_portfolio,
301-
indicators = "landcover"
294+
indicators = "population_count",
295+
stats_popcount = "sum",
296+
engine = "zonal"
302297
)
303298
})
304299

0 commit comments

Comments
 (0)