-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmosaic_rasters.R
79 lines (65 loc) · 1.85 KB
/
mosaic_rasters.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
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
# ---
# title: "Mosaic rasters downloaded from earth engine"
# author: "Brendan Casey"
# created: "2024-08-05"
# description:
# This script creates a mosaic of raster files downloaded from
# Google Earth Engine. It includes steps to list directories,
# load raster files, and generate a mean mosaic of these rasters.
# ---
# 1. Setup ----
## 1.1 Load packages ----
library(terra) # processing rasters
library(tidyverse) # data manipulation and visualization
library(sf) # handling spatial vector data
## 1.2 Import data ----
aoi <- st_read("0_data/external/alberta/Alberta.shp")
# 2. Mosaic rasters ----
# This section creates a mosaic of raster files from a list of .tif
# files in a directory.
## 2.1 List directories ----
path_name <- paste0(
"/Users/brendancasey/Library/CloudStorage/",
"[email protected]/My Drive/",
"gee_exports/"
)
## 2.2 Focal 500 ----
fl <- list.files(
path_name,
pattern = "focal_image_500.*\\.tif$",
recursive = TRUE,
full.names = TRUE
)
### 2.2.1 Load and mosaic rasters ----
rasters <- lapply(fl, rast)
m <- do.call(terra::mosaic, c(rasters, fun = "mean"))
# rasters <- terra::sprc(fl)
# m <- terra::mosaic(rasters, fun = "mean")
### 2.2.2 Crop to aoi ----
aoi_tr <- st_transform(aoi, st_crs(m))
m <- mask(m, aoi_tr)
### 2.2.3 Save as .tif ----
writeRaster(
m,
filename = "0_data/manual/predictor/gee/focal_image_500.tif",
overwrite = TRUE
)
## 2.3 Focal 0 ----
fl <- list.files(
path_name,
pattern = "focal_image_0.*\\.tif$",
recursive = TRUE,
full.names = TRUE
)
### 2.3.1 Load and mosaic rasters ----
rasters <- lapply(fl, rast)
m <- do.call(terra::mosaic, c(rasters, fun = "mean"))
### 2.3.2 Crop to aoi ----
aoi_tr <- st_transform(aoi, st_crs(m))
m <- mask(m, aoi_tr)
### 2.3.3 Save as .tif ----
writeRaster(
m,
filename = "0_data/manual/predictor/gee/focal_image_0.tif",
overwrite = T
)