我已经创建了一个包含文件夹中的栅格文件名的列表,然后想要使用 terra 将这些栅格导入到“堆栈”中的 R 中,最终想要使用 terra 从 shapefile 中的点处的这些栅格中提取值。我的光栅文件在我的文件夹中都是唯一命名的;但是,在某些情况下,我有类似命名的栅格(例如,canflax2017.tif、canflax2018.tif、canflax2019.tif、canflax2018_137.tif、canflax2018_51.tif、canflax2017_137.tif 等)。当我将所有栅格放在“堆栈”中时,它似乎会切断生成的栅格名称中的年份,但仅限于栅格名称中没有“_”的文件名。例如,canflax2017、canflax2018、canflax2019 都具有名称“canflax”并且不是唯一的,但 canflax2018_137、canflax2018_51 和 canflax2019_137 很好并显示整个文件名。当然,当我想从堆栈中提取值时,canflax2017、canflax2018、canflax2019 的列在输出数据帧中仅命名为 canflax、canflax.1、canflax.2 等。有什么想法可以确保整个文件名以某种方式合并到堆栈信息中吗?
代码如下:
图书馆(terra)
rasts文件列表<-list.files(path="XXX/AllRasters",pattern="tif$",full.names = TRUE) owl_stack<-rast(rastsfilelist)
猫头鹰<- vect('XX/shapefiletest.shp') test<-extract(owl_stack,owllocs)
只需确保不同栅格(文件)中的图层名称具有不同的名称。下面的示例:无论文件名如何,图层名称都是相同的,直到我更改它们为止。
l <- list.files(path = "/home/sapi/projekty/test", pattern = "above", full.names = TRUE)
l
#> [1] "/home/sapi/projekty/test/above_a.tif"
#> [2] "/home/sapi/projekty/test/above_b.tif"
#> [3] "/home/sapi/projekty/test/above_c.tif"
r <- terra::rast(l)
r
#> class : SpatRaster
#> dimensions : 25, 22, 3 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> sources : above_a.tif
#> above_b.tif
#> above_c.tif
#> names : lyr.1, lyr.1, lyr.1
#> min values : 10, 10, 10
#> max values : 10, 10, 10
w <- terra::rast("/home/sapi/projekty/test/above_a.tif")
names(w) <- "other_name"
w
#> class : SpatRaster
#> dimensions : 25, 22, 1 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : above_a.tif
#> name : other_name
#> min value : 10
#> max value : 10
terra::writeRaster(w, filename = "/home/sapi/projekty/test/above_d.tif", overwrite = TRUE)
l <- list.files(path = "/home/sapi/projekty/test", pattern = "above", full.names = TRUE)
x <- terra::rast(l)
x
#> class : SpatRaster
#> dimensions : 25, 22, 4 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> sources : above_a.tif
#> above_b.tif
#> above_c.tif
#> above_d.tif
#> names : lyr.1, lyr.1, lyr.1, other_name
#> min values : 10, 10, 10, 10
#> max values : 10, 10, 10, 10
创建于 2024-01-22,使用 reprex v2.1.0