-
Notifications
You must be signed in to change notification settings - Fork 1
/
23_SPAM_cell_by_buffer.R
41 lines (27 loc) · 978 Bytes
/
23_SPAM_cell_by_buffer.R
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
# author: Fernando Aramburu Merlos
# date: 2022-07-21
# get SPAM cells inside each buffer (and their weight)
# needed to quickly analyze crop area coverage by each buffer
# setup ---------
host <- system("hostname", TRUE)
if (host == "LAPTOP-ST129J47") {
setwd("C:/Users/ferar/OneDrive - University of Nebraska-Lincoln/GYGA")
} else if (host == "LRDAH-DX5B0R3") {
setwd("C:/Users/faramburumerlos2/OneDrive - University of Nebraska-Lincoln/GYGA")
}
library(terra)
library(data.table)
# cells --------
# to which buffer each SPAM cell belong?
# spam
mz <- rast("data/SPAM/harvested_area_global_merged_v2/spam_global_H_Maiz_A.tif")
# buffers
bf <- vect("data/world_buffers/buffer_zones.shp")
d <- cells(mz, bf, exact = TRUE, touches = TRUE)
d <- as.data.table(d)
dbf <- as.data.table(as.data.frame(bf))
dbf[, ID:= 1:.N]
dd <- merge(d, dbf, by = "ID", all = TRUE)
dd[, ID:=NULL]
dir <- "data/SPAM/cell_coverage"
fwrite(dd, file.path(dir, "SPAM_cells_per_bf.csv"))