forked from tmieno2/R-as-GIS-for-Economists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EffectiveDataExtraction.Rmd
1835 lines (1365 loc) · 68.6 KB
/
EffectiveDataExtraction.Rmd
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
```{r setup, echo = FALSE}
library(tufte)
library(knitr)
knitr::opts_chunk$set(
echo = TRUE,
cache = TRUE,
comment = NA,
message = FALSE,
warning = FALSE,
tidy = FALSE,
cache.lazy = FALSE
)
opts_knit$set(
root.dir = "/Users/tmieno2/Box/Teaching/AAEA R/GIS"
)
```
```{r packages, echo=FALSE, warning=FALSE, cache = FALSE}
#--- load packages ---#
suppressMessages(library(data.table))
suppressMessages(library(dtplyr))
suppressMessages(library(rgdal))
suppressMessages(library(stringr))
suppressMessages(library(rgeos))
suppressMessages(library(sf))
suppressMessages(library(parallel))
suppressMessages(library(ggplot2))
suppressMessages(library(raster))
suppressMessages(library(tidyverse))
suppressMessages(library(stargazer))
suppressMessages(library(tmap))
suppressMessages(library(exactextractr))
suppressMessages(library(prism))
suppressMessages(library(microbenchmark))
suppressMessages(library(future.apply))
suppressMessages(library(lubridate))
suppressMessages(library(velox))
suppressMessages(library(maps))
suppressMessages(library(bench))
suppressMessages(library(tictoc))
suppressMessages(library(profvis))
```
```{r, eval = F}
setwd("/Users/tmieno2/Box/Teaching/AAEA R/GIS")
```
# Introduction
First, do not even bother to read this section if you have never encountered a case where you feel like you need to speed up your raster-polygon extraction jobs. The time you will spend to process the information presented here will not justify the time you can save by taking advantage of knowledge you gain by reading this section.
In Chapter ?, we saw that **raster::extract()** is much slower than **exactextracr::exact_extract()** and **velox::extract()**. Here, we will compare
## Take-away messages
+ If you intend or expect to use the raster layers for other projects of different geographic or temporal focus, then it might make sense to combine multiple raster layers into multi-band raster files and save them, which can be used for much faster data extraction than multiple individual single-band raster files
+ If this is the last time you are extracting values from the raster files, then you do not want to brick them before value extraction. **stack()** them and then use either **exact_extract()** or **velox::extract()**.
+ Do not parallelize over geographic units within an **sf** object
+ Do not parallelize in a way that each core work on a single layer at a time.
# Alternative ways to extract values and speed comparison
I the previous sections, we learned how to extract values from raster layers for point and polygons data. There are several other ways of extracting values from a raster layer. If you are satisfied with `raster::extact()` points data and `exact_extract()` for polygons data, then look no further.
For most of the applications, **raster::extract()** and **exact_extract()** are sufficient. However, when you need to process many many large raster files and you desire to shorten the processing time, there are better options that can reduce processing time substantially if used right. The first option uses the **velox** package, and the second option uses cell numbers to extract values. Here, we discuss the basics of how they work. How to utilized them effectively will be discussed in [Chapter ?]().
### The **velox**^[Its github page is [here](http://philipphunziker.com/velox/).] package
At the time of writing, **velox** is not available from CRAN as it was pulled out from the CRAN due to maintenance issues.^[This package is in the process of changing its maintainer (see [here](https://github.com/hunzikp/velox/issues/43)). Hopefully, it will be back on CRAN soon.] So, you need to install the **velox** package using the following code:
```{r install_velox, eval = F}
require(devtools)
#--- install velox ---#
devtools::install_github('hunzikp/velox')
```
You may encounter an error when you try to install the package^[See [here](https://github.com/hunzikp/velox/issues/44)]. I did encounter an error. For me, installing this version of **Rcpp** solved the problem.
```{r install_rcpp, eval = F}
install.packages("Rcpp", repos="https://RcppCore.github.io/drat")
```
But, you may encounter a different problem.
```{r other_problmes}
vr_raster <- velox(prism_tmax_0702_fn)
```
```{r velox_extract}
head(vr_raster$extract(KS_county, df = TRUE))
```
### Extraction by cell numbers
This option is meaningful only when you intend to extract values from multiple raster files that are of exactly the same resolution and geographic coverage (e.g., daily PRISM raster files). This option makes use of the **exact_extact()** and **raster::cellFromXY()**.
First, we get the coordinates of the overlapping cells by adding the **include_XY = TRUE** option to **exact_extract()** as follows:
```{r extract_xy_coordinates, results = "hide"}
XY <- exact_extract(prism_tmax_0701_KS, KS_county, include_xy = TRUE, progress = FALSE) %>%
bind_rows(.id = "county_id") %>%
dplyr::select(county_id, x, y, coverage_fraction)
#--- take a look ---#
XY
```
We then use the coordinates to extract cell numbers using the **raster::cellFromXY()** as follows:
```{r get_cellid}
prism_cell_id <- cellFromXY(prism_tmax_0701_KS, XY[, c("x", "y")])
```
Now, assign the extracted cell numbers to **XY**.
```{r cellid_xy}
XY <- mutate(XY, prism_cell_id = prism_cell_id)
#--- take a look ---#
head(XY)
```
As you can see, this dataset tells us which county overlaps which cells (represented by cell numbers) along with their coverage fractions. We can use this data to extract values from another raster layer that is known to have exactly the same spatial resolution and geographic extent. Here, consider extracting data from **prism_tmax_0701**.
```{r combine_data}
#--- extract unique cell numbers ---#
cell_num_ls <- XY$prism_cell_id %>% unique
#--- get tmax data ---#
tmax_data <- data.frame(
#--- cell number as ids ---#
prism_cell_id = cell_num_ls,
#--- extract values associated with cells ---#
value = getValues(prism_tmax_0702)[cell_num_ls]
)
#--- take a loo ---#
head(tmax_data)
```
We can merge the extracted data to **XY** and calculate coverage-weighted tmax for each county.
```{r final_step}
XY <- left_join(XY, tmax_data, by = "prism_cell_id") %>%
group_by(county_id) %>%
#--- get coverage-weighted tmax values by county ---#
summarize(tmax = sum(value * coverage_fraction) / sum(coverage_fraction))
#--- take a look ---#
head(XY)
```
While this option looks rather awkward compared to the other options, a variant of this option (extracting values from many raster files at the same time) turns out to be by far the fastest option (though associated with higher risk if not careful) for some data extraction applications with many temporally disaggregated raster files. The validity of this option depends on the fact that other raster layers you would like to extract values from have exactly the same spatial resolution and geographic extent as the raster layer you obtained the polygon-cell(s) (or point-cell(s)) correspondence. Many applications have this trait. A good example is PRISM. All the daily PRISM raster files have exactly the same spatial resolution and geographic extent. This option can cause serious errors if the spatial resolution and geographic extent of the raster file is different. A good example of that is CDL data, which changed their spatial resolution from 56 meter to 30 meter in late 2000s' due to the change in the underlying remote sensing images^[see Section 3 of (this website)[https://www.nass.usda.gov/Research_and_Science/Cropland/sarsfaqs2.php]]. Therefore, it is important to confirm that all the raster layers are indeed of the same spatial resolution and extent prior to using this extraction method.
### **terra::extract()**
While **terra::extract()** is certainly faster than its counterpart in the **raster** package (its predecessor), it is still far slower than *exact_extractr()* and *velox::extract()* especially when you are dealing with large files (e.g., CDL data).
Here, we will compare **terra::extract()** and **exact_extract()** using a PRISM layer and HUC units^[HUC consists of many hydrologic units covering the entire mainland US. See map below.]. Let's do some preparation^[I am importing the HUC shape file twice because there does not seem any function from the **terra** package that converts an **sf** object to **SpatRaster** at the time of writing.].
```{r pic_huc, echo = F, eval = F}
prism_huc <- tm_shape(prism_tmax_0701_KS) +
tm_raster() +
tm_shape(HUC_sf) +
tm_polygons(alpha = 0)
tmap_save(prism_huc, file = "./Data/prism_HUC_us.png", dpi = 300)
```
```{r echo = F, fig.cap="Map of HUC units overlaid on PRISM data", fig.margin = T}
knitr::include_graphics("./Data/prism_HUC_us.png")
```
```{r terra_preparem, cache = F, results = "hide"}
library(terra)
#--- read a PRISM dataset as RasterLayer ---#
prism_tmax_temp <- raster(prism_tmax_0702_fn)
#--- import HUC as an sf and reproject ---#
HUC_sf <- st_read(dsn = here::here("Data", "huc250k_shp"), layer = "huc250k") %>%
st_transform(projection(prism_tmax_temp)) %>%
simplify()
#--- read a PRISM dataset as SpatRaster ---#
prism_rs_temp <- rast(prism_tmax_0702_fn)
#--- import HUC as SpatVector and reproject ---#
HUC_spat <- vect(here::here("Data", "huc250k_shp", "huc250k.shp")) %>%
#--- project to the CRS of the raster file ---#
project(., prism_rs_temp)
```
Let's compare the two approaches.
```{r compare_extract_terra, cache = T}
#--- terra::extract() ---#
tic()
temp <- terra::extract(prism_rs_temp, HUC_spat)
toc()
#--- exact_extract() ---#
tic()
temp <- exact_extract(prism_tmax_0701_KS, HUC_sf, progress = F)
toc()
```
As you can see, **terra::extract()** is much slower than **exact_extract()**. You would save much more time using **exact_extract()** for a much larger raster file (e.g., CDL data). Unless the speed of **terra::extract()** improves dramatically, there is no reason to use it over other much faster approaches at least at the moment.
<!-- ```{r, cache=TRUE}
ggplot() +
geom_sf(data=urnrd_isc,aes(fill=factor(new_id)),alpha=0.3) +
scale_fill_viridis(discrete=TRUE,option='magma',name='') +
theme_bw() +
theme(
legend.position='bottom'
)
``` -->
<!-- wells <- st_read(dsn = "~/Dropbox/Teaching/UNL/EconometricsMaster/aecn892_2017/labs/bookdown/Data/", "registration")
st_write(filter(wells,nrdname=='Upper Republican'),dsn = "~/Dropbox/Teaching/UNL/EconometricsMaster/aecn892_2017/labs/bookdown/Data/",layer='URNRD_wells.shp',driver='ESRI Shapefile',layer_options = 'OVERWRITE=YES', update = TRUE) -->
# Single raster layer
## Processing time at varying levels of cell and polygon density
```{r read_data, eval = F}
library(cdlTools)
#--- download the CDL data for Iowa in 2015 ---#
IA_cdl_2015 <- getCDL("Iowa", 2015)$IA2015
#--- save it as a GeoTif file ---#
velox(IA_cdl_2015)$write(path = "./Data/IA_cdl_2015.tif", overwrite = T)
```
```{r create_lower_resolution_cdl, eval = F}
IA_cdl_2015_2 <- aggregate(IA_cdl_2015, fact = 2)
values(IA_cdl_2015_2) <- getValues(IA_cdl_2015_2) %>% round(digits = 0)
velox(IA_cdl_2015_2)$write(path = "./Data/IA_cdl_2015_ag_2.tif", overwrite = T)
IA_cdl_2015_5 <- aggregate(IA_cdl_2015, fact = 5)
values(IA_cdl_2015_5) <- getValues(IA_cdl_2015_5) %>% round(digits = 0)
velox(IA_cdl_2015_5)$write(path = "./Data/IA_cdl_2015_ag_5.tif", overwrite = T)
IA_cdl_2015_10 <- aggregate(IA_cdl_2015, fact = 10)
values(IA_cdl_2015_10) <- getValues(IA_cdl_2015_10) %>% round(digits = 0)
velox(IA_cdl_2015_10)$write(path = "./Data/IA_cdl_2015_ag_10.tif", overwrite = T)
IA_cdl_2015_20 <- aggregate(IA_cdl_2015, fact = 20)
values(IA_cdl_2015_20) <- getValues(IA_cdl_2015_20) %>% round(digits = 0)
velox(IA_cdl_2015_20)$write(path = "./Data/IA_cdl_2015_ag_20.tif", overwrite = T)
```
```{r prepare_grids_over_IA}
IA_boundary <- st_as_sf(map("state", "iowa", plot = FALSE, fill = TRUE))
raster_ls <- c("./Data/IA_cdl_2015.tif", paste0("./Data/IA_cdl_2015_ag_", c(2, 5, 10, 20), ".tif"))
grids_ls <- lapply(c(10, 20, 50, 100),
function(x) st_make_grid(IA_boundary, n = c(x, x)) %>%
st_as_sf() %>%
st_transform(projection(raster("./Data/IA_cdl_2015.tif")))
)
```
Both **exactextracr::exact_extract()** and **velox::extract()** use C++ as their backend and are much faster than **raster::extract()**. But, which one is faster? Does the winner change depending on the nature of the raster and polygon data? We try to answer this question here. Specifically, we compare **exactextracr::exact_extract()** and **velox::extract** for various levels of cell and polygon density. The base raster file we use is Iowas CDL data for 2015, which has `r ncell(raster("./Data/IA_cdl_2015.tif"))` cells. We then aggregate it using the **raster::aggregate()** function to the factor of 2, 5, 10, and 20. The resulting cell numbers after the aggregations are `r ncell(raster("./Data/IA_cdl_2015_ag_2.tif"))` , `r ncell(raster("./Data/IA_cdl_2015_ag_5.tif"))`, `r ncell(raster("./Data/IA_cdl_2015_ag_10.tif"))`, and `r ncell(raster("./Data/IA_cdl_2015_ag_20.tif"))` cells, respectively. For polygon data, we use regular grids drawn over IA state border with varying spatial resolutions. A set of the number of polygons we will look at are `r nrow(grids_ls[[1]])`, `r nrow(grids_ls[[2]])`, `r nrow(grids_ls[[3]])`, and `r nrow(grids_ls[[4]])`.
https://github.com/rstudio/profvis/issues/72
```{r single_layer_not_parallelized, eval = FALSE}
pars_data <- expand.grid(
raster = seq_along(raster_ls),
polygons = seq_along(grids_ls)
) %>%
data.table()
ee_nonpar <- function(raster_file, polygons){
temp <- raster(raster_file) %>%
exact_extract(., polygons) %>%
rbindlist(idcol = "id") %>%
.[, .N, by = .(id, value)]
return(temp)
gc()
}
v_nonpar <- function(raster_file, polygons){
temp <- velox(raster_file)$extract(polygons, df = TRUE) %>%
data.table() %>%
setnames(names(.), c("id", "value")) %>%
.[, .N, by = .(id, value)]
return(temp)
gc()
}
mb_wrap <- function(i){
temp_raster_file <- raster_ls[pars_data[i, raster]]
temp_polygons <- grids_ls[[pars_data[i, polygons]]]
mb <- microbenchmark(
"e" = {
temp <- ee_nonpar(temp_raster_file, temp_polygons)
},
"p" = {pause(30)},
"v" = {
temp <- v_nonpar(temp_raster_file, temp_polygons)
},
times = 5
) %>%
data.table() %>%
.[, ncells := ncell(raster(temp_raster_file))] %>%
.[, npolygons := nrow(temp_polygons)]
return(mb)
}
mb_nonpar <- lapply(1:nrow(pars_data), mb_wrap) %>%
rbindlist()
saveRDS(mb_nonpar, "./Data/mb_non_par_static.rds")
```
The figure displays the results of the comparison using **microbenchmark()** with 10 replications^[It would be better to have more replications, but it just takes too much time. If you want to be absolutely sure about the reliability of this benchmarking test. Try with more replications. Nobody is stopping you. It will sure take you very very long though.]. At the smallest numbers of polygons, **exact_extract()** performs just as well as **velox::extract()** at low raster cell numbers, but performs clearly better at very high raster cell numbers. At 200 million cells, **exact_extract()** completes value extraction 15 seconds faster. The computation time of **exact_extract()** naturally goes up as the number of polygons increases (not sure what happened with # of polygons at 342). It is particularly interesting that **exact_extract()** seems to have an overhead of about 10 seconds irrespective of the cell density when the number of polygons is 7962. On the other hand, **velox::extract()** is much faster at low cell densities when the number of polygons is 7962. Indeed, it seems like the performance of **velox::extract()** is not affected by the number of polygons at all.
```{r disp_mb_nonpar}
mb_nonpar <- readRDS("./Data/mb_non_par_static.rds") %>%
.[expr != "p", ]
npolygons_ls <- mb_nonpar[, npolygons] %>% unique()
mb_nonpar_plot <- mb_nonpar %>%
.[, .(time = mean(time)) , by = .(expr, ncells, npolygons)] %>%
.[, npolygons_txt := paste0("# of polygons = ", npolygons)] %>%
.[, npolygons_txt := factor(npolygons_txt, levels = paste0("# of polygons = ", npolygons_ls))] %>%
.[, method := ifelse(expr == "e", "exact_extract()", "velox::extract()")]
ggplot(mb_nonpar_plot) +
geom_line(aes(y = time/1e9, x = ncells/1e6, color = method)) +
geom_point(aes(y = time/1e9, x = ncells/1e6, color = method)) +
facet_wrap(npolygons_txt ~ .) +
scale_fill_discrete(name = "") +
xlab("# of cells in the raster layers (million cells)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
Looking at the profile of **velox::extact()** and **exact_extact()** at extreme cases is revealing. First, let's try profiling them for the case of **IA_cdl_2015** (highest cell density) and only one polygon. For the **velox** approach, we divide the whole process into two parts: **velox()** (to read the raster file) and **velox::extact()**. As can be seen in the profiling result, it takes about $8$ seconds to transform the imported data, and then spends about $10$ seconds to extract values for the single polygon^[Time numbers are in milliseconds]. Notice also that it allocated about 6 GB of memory. This is because **velox()** reads in all the raster values data^[**getRasterData** function you can see in the left bottom part of the figure is responsible for reading the data.] and hold it in memory. Once all the data is read, then values are extracted, where the computation happens in C++ utilizing the boost Geometry library.
```{r prof_v}
profvis({
vrs <- velox("./Data/IA_cdl_2015.tif")
values <- vrs$extract(grids_ls[[4]][1, ])
})
```
Note that **profvis()**^[or more precisely **Rprof()**, the results of which it visualizes] does not include time spent outside of R is not included in the profile^[https://github.com/rstudio/profvis/issues/72]. So, the sum of the time from **profvis()** is smaller than the actual time the process spent to complete. The elapsed time by **system.time()** from the following code is similar to the number we saw in the benchmark figure.
```{r system_all_v}
system.time({
vrs <- velox("./Data/IA_cdl_2015.tif")
values <- vrs$extract(grids_ls[[4]][1, ])
})
```
As you can see, system spends about 10 seconds^[system time refers to the time spent by the operating system on behalf of the current R process (see William Dunlap's explanation [here](https://r.789695.n4.nabble.com/Meaning-of-proc-time-td2303263.html#a2306691)).]. Further breaking down the whole process into two, you can see that it takes about 2 seconds for the OS to import the data, and about 7 seconds to transfer the data between R and C++^[This is only my guess. I really do not know how the system is spending time exactly. But, they seem to be the most significant job that the OS is handling on behalf of R.].
```{r system_1_v}
system.time({
vrs <- velox("./Data/IA_cdl_2015.tif")
})
```
```{r system_2_v}
system.time({
values <- vrs$extract(grids_ls[[4]][1, ])
})
```
This is in stark contrast to **exact_extract()**. It takes only 0.23 seconds to complete the process. Also, note that it used only 2.5 MB of memory. This is because **raster::raster()** does not read the raster values data in memory unlike **velox()**. Without actually reading in all the data at the beginning, **exact_extract()** uses **raster::getsValueBlock()** to read in only the relevant part of the data for the polygon.
```{r }
profvis({
IA_CDL_2015 <- raster("./Data/IA_cdl_2015.tif")
temp <- exact_extract(IA_CDL_2015, grids_ls[[4]][1, ])
})
```
As you can see, system is much less involved in the whole process compared to the **velox** approach.
```{r }
system.time({
temp <- exact_extract(raster("./Data/IA_cdl_2015.tif"), grids_ls[[4]][1,])
})
```
Now, let's take a look at how much it takes to extract values for all the $7962$ polygons.
```{r }
profvis({
vrs <- velox("./Data/IA_cdl_2015.tif")
values <- vrs$extract(grids_ls[[4]])
})
```
Naturally, more memory was allocated to the extraction part at about $5.5$ GB.
```{r }
system.time({
values <- vrs$extract(grids_ls[[4]])
})
```
So, it took about 9 seconds more to extract values for $7961$ more polygons (both user and system times included). On the other hand, **exact_extract()** uses 18GB of memory, which is much larger than that of the **velox** approach.
```{r }
profvis({
temp <- exact_extract(raster("./Data/IA_cdl_2015.tif"), grids_ls[[4]])
})
```
Similar to the 1-polygon case, most of the computation happens on R with the system comprising a relatively small portion of the entire process.
```{r }
system.time({
temp <- exact_extract(raster("./Data/IA_cdl_2015.tif"), grids_ls[[4]])
})
```
Here are the summary of findings here:
+ the **velox** approach is faster (slower) than the **exact_extract** approach when the number of raster cells are small (large) and the number of polygons are large (small).
+ the **exact_extract()** approach is more memory-efficient when the number of polygons is small, but becomes worse than the **velox** approach when the number of polygons is large.
Now, if you are doing only a single extraction task with one raster file against one polygon file and you will never repeat the same process, it does not really matter which option you go for. Just go for the one you are comfortable with. 15 seconds difference is not much if you are doing it only once. You have 9 raster files (say CDL data from 2010 to 2018)? It still does not really matter which option to pick. 135 seconds is still not much. The time you need to spend to test which one is faster (the time you spend to code the test and actually running the test) could easily kill much of the 135 second you try to save in the first place. You are doing thousands of value extractions? Then, you might want to do a quick test first.
---
With that said, we will still examine how parallelization over polygons can help reduce computation time. Here, We use 5 cores. For the **velox** approach, since velox first reads in the whole raster values data, extraction of values for one polygon can be done very quickly by first cropping the raster to the polygon you are working on and then extract values for it.
```{r }
vrs <- velox("./Data/IA_cdl_2015.tif")
system.time({
vrs_copy <- vrs$copy()
vrs_copy$crop(grids_ls[[4]][1,])
dt_return <- vrs_copy$extract(grids_ls[[4]][1,])
})
```
Note also that **vrs$copy()** does not double the memory of holding the raster data in memory.
```{r }
profvis({
vrs_copy <- vrs$copy()
vrs_copy$crop(grids_ls[[4]][1,])
dt_return <- vrs_copy$extract(grids_ls[[4]][1,])
})
```
For **exact_extract()**, cropping first does not make sense at least just for one polygon.
```{r }
system.time({
temp <- exact_extract(raster("./Data/IA_cdl_2015.tif"), grids_ls[[4]][1, ])
})
system.time({
temp <- raster("./Data/IA_cdl_2015.tif") %>%
crop(., grids_ls[[4]][1, ]) %>%
exact_extract(., grids_ls[[4]][1, ])
})
```
Also, notice that the amount of time you process a single polygon is significant at about $0.2$ seconds. This is much much slower than how the **velox** approach handle a single polygon. This suggests that looping over polygons one by one would not work well for the **exact_extract** approach. Instead, we can divide the polygons to several blocks and then process them in parallel. For **exact_extract()**, this has an added benefit of reducing the overhead it has when it needs to handle many many cells.
## Parallelized over polygons
```{r }
source("./Codes/ParallelizedSingleRasterExtract.R")
```
```{r single_layer_parallelized, eval = FALSE}
mb_wrap_par_e <- function(i){
temp_raster_file <- raster_ls[pars_data[i, raster]]
temp_polygons <- grids_ls[[pars_data[i, polygons]]]
mb <- microbenchmark(
"e_one" = {
temp <- ee_par_one(temp_raster_file, temp_polygons)
},
"e_block" = {
temp <- ee_par_block(temp_raster_file, temp_polygons)
},
times = 1
) %>%
data.table() %>%
.[, ncells := ncell(raster(temp_raster_file))] %>%
.[, npolygons := nrow(temp_polygons)]
return(mb)
}
mb_par_e <- lapply(1:nrow(pars_data), mb_wrap_par_e) %>%
rbindlist()
saveRDS(mb_par_e, "./Data/mb_par_static_e.rds")
#--------------------------
# Only velox
#--------------------------
polygons <- grids_ls[[1]]
raster_layer <- "./Data/IA_cdl_2015.tif"
# tic()
# v_par_block("./Data/IA_cdl_2015.tif", grids_ls[[1]])
# toc()
tic()
v_par_one("./Data/IA_cdl_2015.tif", grids_ls[[1]])
toc()
# tic()
# v_nonpar("./Data/IA_cdl_2015_ag_20.tif", grids_ls[[4]])
# toc()
mb_wrap_par_v <- function(i){
temp_raster_file <- raster_ls[pars_data[i, raster]]
temp_polygons <- grids_ls[[pars_data[i, polygons]]]
mb <- microbenchmark(
"v_one" = {
temp <- v_par_one(temp_raster_file, temp_polygons)
},
"v_block" = {
temp <- v_par_block(temp_raster_file, temp_polygons)
},
times = 1
) %>%
data.table() %>%
.[, ncells := ncell(raster(temp_raster_file))] %>%
.[, npolygons := nrow(temp_polygons)]
return(mb)
}
mb_par_v <- lapply(1:nrow(pars_data), mb_wrap_par_v) %>%
rbindlist()
saveRDS(mb_par_v, "./Data/mb_par_static_v.rds")
```
```{r disp_mb_par}
mb_par_e <- readRDS("./Data/mb_par_static_e.rds")
mb_par_v <- readRDS("./Data/mb_par_static_v.rds")
mb_par <- rbind(mb_par_e, mb_par_v)
npolygons_ls <- mb_par[, npolygons] %>% unique()
mb_par_plot <- mb_par %>%
.[, .(time = median(time)) , by = .(expr, ncells, npolygons)] %>%
.[, npolygons_txt := paste0("# of polygons = ", npolygons)] %>%
.[, npolygons_txt := factor(npolygons_txt, levels = paste0("# of polygons = ", npolygons_ls))]
ggplot(mb_par_plot) +
geom_line(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
geom_point(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
facet_wrap(npolygons_txt ~ .) +
scale_fill_discrete(name = "") +
xlab("# of cells in the raster layers (million cells)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
```{r }
ggplot(mb_par_plot[expr != "e_one",]) +
geom_line(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
geom_point(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
facet_wrap(npolygons_txt ~ .) +
scale_fill_discrete(name = "") +
xlab("# of cells in the raster layers (million cells)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
```{r }
mb_all <- rbind(mb_nonpar_plot, mb_par_plot, fill = TRUE)
ggplot(mb_all[expr %in% c("v", "v_one", "v_block"),]) +
geom_line(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
geom_point(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
facet_wrap(npolygons_txt ~ .) +
scale_fill_discrete(name = "") +
xlab("# of cells in the raster layers (million cells)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
```{r }
mb_all <- rbind(mb_nonpar_plot, mb_par_plot, fill = TRUE)
ggplot(mb_all[expr %in% c("e", "e_block"),]) +
geom_line(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
geom_point(aes(y = time/1e9, x = ncells/1e6, color = expr)) +
facet_wrap(npolygons_txt ~ .) +
scale_fill_discrete(name = "") +
xlab("# of cells in the raster layers (million cells)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
# Extracting values from many raster files of the same spatial resolution and extent for the static polygons
```{r read_data, eval = F}
#/*=================================================*/
#' # Data preparation
#/*=================================================*/
#--------------------------
# PRISM
#--------------------------
date <- "2014-04-01"
get_prism_dailys(
type = "ppt",
date = date,
keepZip = FALSE
)
date_text <- str_remove_all(date, "-")
folder_name <- paste0("PRISM_ppt_stable_4kmD2_", date_text, "_bil")
file_name <- paste0("PRISM_ppt_stable_4kmD2_", date_text, "_bil.bil")
file_path <- paste0("./Data/PRISM/", folder_name, "/", file_name)
prism_rs <- raster(file_path)
#--------------------------
# HUC
#--------------------------
HUC <- st_read(dsn = "./Data/huc250k_shp", layer = "huc250k") %>%
st_transform(projection(prism_rs))
# tic()
# HUC_cells <- raster::extract(prism_rs, HUC, cellnumbers = TRUE)
# toc()
# saveRDS(HUC_cells, "./Data/HUC_cellnumbers.rds")
#--------------------------
# KS county
#--------------------------
KS_county <- st_as_sf(map("county", "kansas", plot = FALSE, fill = TRUE)) %>%
st_transform(projection(prism_rs))
```
```{r data_read_run, echo = F}
#/*=================================================*/
#' # Read the data necessary for benchmarking
#/*=================================================*/
date <- "2014-04-01"
date_text <- str_remove_all(date, "-")
folder_name <- paste0("PRISM_ppt_stable_4kmD2_", date_text, "_bil")
file_name <- paste0("PRISM_ppt_stable_4kmD2_", date_text, "_bil.bil")
file_path <- paste0("./Data/PRISM/", folder_name, "/", file_name)
prism_rs <- raster(file_path)
#--------------------------
# HUC
#--------------------------
HUC <- st_read(dsn = "./Data/huc250k_shp", layer = "huc250k") %>%
st_transform(projection(prism_rs))
# tic()
# HUC_cells <- raster::extract(prism_rs, HUC, cellnumbers = TRUE)
# toc()
# saveRDS(HUC_cells, "./Data/HUC_cellnumbers.rds")
#--------------------------
# KS county
#--------------------------
KS_county <- st_as_sf(map("county", "kansas", plot = FALSE, fill = TRUE)) %>%
st_transform(projection(prism_rs))
```
```{r prepare_bricks_stacks_grd, echo = F}
#/*=================================================*/
#' # Prepare bricks, stacks, and grd for benchmarking
#/*=================================================*/
#===================================
# Create RasterStack and RasterBrick ()
#===================================
it_ls <- c( 1, 5, 10 , 20, 50)
gen_stack <- function(i, file_path) {
temp <- stack(rep(file_path, i, quick = TRUE))
return(temp)
}
gen_brick <- function(i, file_path) {
temp <- brick(stack(rep(file_path, i, quick = TRUE)))
return(temp)
}
brick_ls <- lapply(it_ls, function(x) gen_brick(x, file_path))
stack_ls <- lapply(it_ls, function(x) gen_stack(x, file_path))
#===================================
# Create multi-band raster file
#===================================
save_stack <- function(it, file_path) {
temp <- stack(rep(file_path, it, quick = TRUE))
writeRaster(temp, paste0("./Data/multi_grid_", it, ".grd"), overwrite = T)
}
lapply(it_ls, function(x) save_stack(x, file_path))
```
In some cases, you need to process many raster files of the same spatial resolution and extent for the static polygons (polygons that do not alter theirs shapes over time). For example, suppose you need daily precipitation data by county for the entire mainland US for 30 years. You would be processing more than 10,000 raster files. Fortunately, processing many raster files of the same spatial resolution and extent can be done much more efficiently than processing the same number of raster files with differing spatial characteristics. There are many alternative ways to tackle the challenge of processing so many files. We will compare various alternatives including naive ones (which R users who are not experienced in spatial data handling may fall into the trap of doing) and smarter ones.
We will first benchmark the following four options. For each approach, we will use either **exact_extract()** and **velox::extract()**. Since the code to implement this benchmark is too long to display here, it is saved as a separate R program, named ......
+ Approach 1: process 50 individually-stored raster files one by one (no parallelization)
+ Approach 2: process 50 individually-stored raster files one by one with parallelization (5 cores)
+ Approach 3: stack 50 individually-stored raster files first and then process into a RasterStack and then process it
+ Approach 4: stack and then brick 50 individually-stored raster files process into a RasterBrick and then process it
Approach 1 is a rather naive way of completing the task. Approach 2 is also naive. Yes, it will be faster than option 2 because it's the parallelized version of option 1, but still naive. The problem with these approaches is that you repeat the process of identifying the topological relations (intersection) of the raster cells and polygons over and over again 50 times. This is naive because the underlying topological relations stays exactly the same across all the raster layers (look at the title of this section) and also because topological relations identification is the most time-consuming part of the whole value extraction task. So, better approaches should avoid repeating that process. Approaches 3 and 4 do exactly that. They first stack (Approach 3) and brick (Approach 4) 50 files of the raster files and then extract values.
For the benchmarking, we will be using a single raster (PRISM) file and then replicate it many times to act like we are dealing with many raster files^[This way, this demonstration does not populate your computer with many individual PRISM files.]. We use **HUC** (`r nrow(HUC)` polygons) as the polygon to which values are extracted to.
```{r prallelize_multiple_files_large_polygon, echo = FALSE, eval = FALSE}
read_RL_ee <- function(){ exact_extract(raster(file_path), HUC) }
read_RL_v <- function(){ velox(raster(file_path))$extract(HUC) }
#--------------------------
# block-parallel
#--------------------------
files_ls <- rep(file_path, it)
file_len <- length(files_ls)
num_core <- 5
files_per_core <- file_len/num_core
extract_by_block_ee <- function(i, files_per_core) {
#--- files processed by core ---#
start_file_index <- (i-1) * files_per_core + 1
#--- indexes for files to process ---#
file_index <- seq(
from = start_file_index,
to = min((start_file_index + files_per_core), file_len),
by = 1
)
data_temp <- files_ls[file_index] %>% # get file names
#--- stack files ---#
stack() %>%
#--- extract ---#
exact_extract(., HUC)
return(data_temp)
}
extract_by_block_v <- function(i, files_per_core) {
#--- files processed by core ---#
start_file_index <- (i-1) * files_per_core + 1
#--- indexes for files to process ---#
file_index <- seq(
from = start_file_index,
to = min((start_file_index + files_per_core), file_len),
by = 1
)
data_temp <- velox(stack(files_ls[file_index]))$extract(HUC)
return(data_temp)
}
# tic()
# a <- velox(paste0("./Data/multi_grid_", files_per_core, ".grd"))$extract(HUC)
# toc()
# tic()
# a <- velox(paste0("./Data/multi_grid_", files_per_core, ".grd"))$extract(HUC, function(x) mean(x, na.rm = TRUE))
# toc()
plan(multiprocess, workers = 5)
#--------------------------
# Run benchmark
#--------------------------
mb <- microbenchmark(
#--- one by one ---#
"method_1_ee" = { temp <- lapply(1:it, function(x) read_RL_ee()) },
"method_1_v" = { temp <- lapply(1:it, function(x) read_RL_v()) },
#--- parallelized one by one ---#
"method_2_ee" = { temp <- future_lapply(1:it, function(x) read_RL_ee()) },
"method_2_v" = { temp <- future_lapply(1:it, function(x) read_RL_v()) },
#--- stack and extract all at once ---#
"method_3_ee" = { temp <- exact_extract(stack(rep(file_path, it)), HUC) },
"method_3_v" = { temp <- velox(stack(rep(file_path, it)))$extract(HUC) },
#--- brick and extract all at once ---#
"method_4_ee" = { temp <- exact_extract(brick(stack(rep(file_path, it))), HUC) },
"method_4_v" = { temp <- velox(brick(stack(rep(file_path, it))))$extract(HUC) },
#--- pre-maid multi-band ---#
"method_5_ee" = { temp <- exact_extract(stack(paste0("./Data/multi_grid_", it, ".grd")), HUC) },
"method_5_v" = { temp <- velox(paste0("./Data/multi_grid_", it, ".grd"))$extract(HUC) },
#--- parallelization by block (individual files stacked before extract) ---#
"method_6_ee" = { temp <- future_lapply(1:num_core, function(x) extract_by_block_ee(x, files_per_core))},
"method_6_v" = { temp <- future_lapply(1:num_core, function(x) extract_by_block_v(x, files_per_core))},
#--- parallelization by block (multi-band raster file) ---#
"method_7_ee" = { temp <- future_lapply(1:num_core, function(x) exact_extract(stack(paste0("./Data/multi_grid_", files_per_core, ".grd")), HUC))},
"method_7_v" = { temp <- future_lapply(1:num_core, function(x) velox(paste0("./Data/multi_grid_", files_per_core, ".grd"))$extract(HUC))},
times = 1
)
saveRDS(mb, "./Data/mb_many_filed_large_polygons.rds")
```
```{r gen_tables, echo = FALSE}
mb_plot_data <- readRDS("./Data/mb_many_filed_large_polygons.rds") %>%
data.table() %>%
.[, time := time/1e9] %>%
.[, extract_type := ifelse(str_detect(expr, "ee"), "exact_extract()", "velox::extract()")] %>%
.[, method := str_extract(expr, "method_[0-9]{1}") %>% gsub("method_", "Option ", .)] %>%
.[, option_number := gsub("Option ", "", method) %>% as.numeric()]
```
The figure below shows the results of the benchmarking of the four approaches. As expected, Approach 1 is the slowest, and Approach 2 does better than Approach 1 because the extraction process is parallelized. Approaches 3 does better than these two naive approaches even though it is not parallelized. This is the benefit of not repeating topological relations identifications over and over again. Approaches 4 does better than the naive ones, but does worse than Approach 3. This may come as a surprise as extracting values from a RasterBrick is supposed to be faster than from a RasterStack. The problem of Approach 4 is that it takes long enough to just create a RasterBrick to kill all the time saving coming from extracting values from a RasterBrick instead of a RasterStack. Finally, **velox::extract()** does always better (sometimes significantly) than **exact_extract()**. But, as we saw earlier, this may change depending of the raster cell and polygon density.
```{r mb_table_first_4}
ggplot(mb_plot_data[option_number <= 4, ]) +
geom_bar(aes(y = time, x = factor(method), fill = extract_type), stat = "identity", position = "dodge") +
scale_fill_discrete(name = "") +
xlab("Options to process 50 raster layers (or bands)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
We now consider the following three additional approaches.
+ Approach 5: each of the 5 cores stacks 10 individually-stored raster files and then processes it
+ Approach 6: process a 50-band raster file ("grd")
+ Approach 7: each of the 5 cores reads a 10-band raster file ("grd") and processes it
Approach 5 simply parallelize Approach 3, which is the fastest among the four approaches tested earlier. Approach 6 reads in a 50-band raster file which was created by combining the 50 raster layers priori, and then extract values for the polygons. In Approach 7, each of the five cores reads in a 10-band raster file (created and saved priori), extract values, and then combine them.
The following figure shows the benchmarking results of the three approaches and also Approach 3:
```{r mb_table_first_4}
ggplot(mb_plot_data[option_number %in% c(3, 5, 6, 7), ]) +
geom_bar(aes(y = time, x = factor(method), fill = extract_type), stat = "identity", position = "dodge") +
scale_fill_discrete(name = "") +
xlab("Options to process 50 raster layers (or bands)") +
ylab("Time (seconds)") +
theme(
legend.position = "bottom"
)
```
## Small polygons
```{r prallelize_multiple_files_small_polygon}
it <- 50
read_RL_ee <- function(){ exact_extract(raster(file_path), KS_county) }
read_RL_v <- function(){ velox(raster(file_path))$extract(KS_county) }
#--------------------------
# block-parallel
#--------------------------
files_ls <- rep(file_path, it)
file_len <- length(files_ls)
num_core <- 5
files_per_core <- file_len/num_core
extract_by_block_ee <- function(i, files_per_core) {
#--- files processed by core ---#
start_file_index <- (i-1) * files_per_core + 1
#--- indexes for files to process ---#
file_index <- seq(
from = start_file_index,
to = min((start_file_index + files_per_core), file_len),
by = 1
)
data_temp <- files_ls[file_index] %>% # get file names
#--- stack files ---#
stack() %>%
#--- extract ---#
exact_extract(., KS_county)
return(data_temp)
}
extract_by_block_v <- function(i, files_per_core) {
#--- files processed by core ---#
start_file_index <- (i-1) * files_per_core + 1
#--- indexes for files to process ---#
file_index <- seq(
from = start_file_index,
to = min((start_file_index + files_per_core), file_len),
by = 1
)
data_temp <- velox(stack(files_ls[file_index]))$extract(KS_county)
return(data_temp)
}
# tic()
# a <- velox(paste0("./Data/multi_grid_", files_per_core, ".grd"))$extract(KS_county)
# toc()
# tic()
# a <- velox(paste0("./Data/multi_grid_", files_per_core, ".grd"))$extract(KS_county, function(x) mean(x, na.rm = TRUE))
# toc()
#--------------------------
# Run benchmark
#--------------------------
mb_poly_small <- microbenchmark(
#--- one by one ---#
"method_1_ee" = { temp <- lapply(1:it, function(x) read_RL_ee()) },
"method_1_v" = { temp <- lapply(1:it, function(x) read_RL_v()) },
#--- parallelized one by one ---#
"method_2_ee" = { temp <- future_lapply(1:it, function(x) read_RL_ee()) },
"method_2_v" = { temp <- future_lapply(1:it, function(x) read_RL_v()) },
#--- stack and extract all at once ---#
"method_3_ee" = { temp <- exact_extract(stack(rep(file_path, it)), KS_county) },
"method_3_v" = { temp <- velox(stack(rep(file_path, it)))$extract(KS_county) },
#--- brick and extract all at once ---#
"method_4_ee" = { temp <- exact_extract(brick(stack(rep(file_path, it))), KS_county) },
"method_4_v" = { temp <- velox(brick(stack(rep(file_path, it))))$extract(KS_county) },
#--- pre-maid multi-band ---#
"method_5_ee" = { temp <- exact_extract(stack(paste0("./Data/multi_grid_", it, ".grd")), KS_county) },
"method_5_v" = { temp <- velox(paste0("./Data/multi_grid_", it, ".grd"))$extract(KS_county) },
#--- parallelization by block (individual files stacked before extract) ---#
"method_6_ee" = { temp <- future_lapply(1:num_core, function(x) extract_by_block_ee(x, files_per_core))},
"method_6_v" = { temp <- future_lapply(1:num_core, function(x) extract_by_block_v(x, files_per_core))},
#--- parallelization by block (multi-band raster file) ---#
"method_7_ee" = { temp <- future_lapply(1:num_core, function(x) exact_extract(stack(paste0("./Data/multi_grid_", files_per_core, ".grd")), KS_county))},
"method_7_v" = { temp <- future_lapply(1:num_core, function(x) velox(paste0("./Data/multi_grid_", files_per_core, ".grd"))$extract(KS_county))},
times = 1
)
saveRDS(mb_poly_small, "./Data/mb_many_filed_small_polygons.rds")