r语言 - 如何从堆叠栅格层(asc格式)提取值到发生(csv)数据?


#Loading climate data
bio6 <- raster("bio_06.asc")
bio5 <- raster("bio_05.asc")
predictors <- stack(bio5, bio6) predictors`
#Loading occurrence data which is in long and lat
occ <- read.csv("presence_locations.csv")[,-1]

现在,当我尝试将值提取到occ坐标时,我得到以下错误:

`presvals <- extract(predictors, occ)
Error in extract(predictors, occ) : 
object of type 'S4' is not subsettable

在研究了很多之后,我没有得到我需要的信息,大多数文章建议转换成我不需要的csv格式。[我试过的链接没有给我任何想法(https://gis.stackexchange.com/questions/403555/raster-subset-error-in-r-error-object-of-type-s4-is-not-subsettable)

这是预测因子。

> predictors
class      : RasterStack 
dimensions : 9605, 7866, 76024090, 2  (nrow, ncol, ncell, nlayers)
resolution : 0.0001297718, 0.0001297718  (x, y)
extent     : 35.5506, 36.65584, 35.58337, 37.13761  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
names      : bio_05, bio_06 

这是发生率数据

> occ
Longitude Latitude
1    35.16556 36.25056
2    36.01378 36.55494
3    36.26056 36.97625

我也尝试了背景点。它在提取NA值从栅格中提取值时返回的NAs

# background data
backg <- randomPoints(predictors, 1000)
absvals <- extract(predictors, backg)
> absvals
bio_05 bio_06
[1,]     NA     NA
[2,]     NA     NA

**如果有人告诉我如何修复这个错误,我将非常感激。* *

示例数据from terra::predict

logo <- rast(system.file("ex/logo.tif", package="terra"))   
names(logo) <- c("red", "green", "blue")
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 
66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 
22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)

用2列矩阵提取

e <- extract(logo, p)
head(e)
#  red green blue
#1 194   205  237
#2 147   161  208
#3 142   156  203
#4 221   228  254
#5 162   173  229
#6 115   129  174

使用SpatVector提取

v <- vect(p, crs="+proj=longlat")
v
# class       : SpatVector 
# geometry    : points 
# dimensions  : 20, 0  (geometries, attributes)
# extent      : 4, 95, 7, 73  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs
ve <- extract(logo, v)
head(ve)
#  ID red green blue
#1  1 194   205  237
#2  2 147   161  208
#3  3 142   156  203
#4  4 221   228  254
#5  5 162   173  229
#6  6 115   129  174

由于您的示例不可复制,我只是下载了一些随机的WorldClim历史气候数据(cf在这里)。另外,我希望您不介意使用{terra}代替{raster}:

library(terra)
#> terra 1.6.49
# get data
file <- "wc2.1_10m_bio.zip"
download.file("https://geodata.ucdavis.edu/climate/worldclim/2_1/base/wc2.1_10m_bio.zip", destfile = file) 
unzip(file)
# adjust extension when working with asc files
files <- list.files(pattern = "tif$", full.names = TRUE)[15:16]
files
#> [1] "./wc2.1_10m_bio_5.tif" "./wc2.1_10m_bio_6.tif"
# load raster data
r <- rast(files)
r
#> class       : SpatRaster 
#> dimensions  : 1080, 2160, 2  (nrow, ncol, nlyr)
#> resolution  : 0.1666667, 0.1666667  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> sources     : wc2.1_10m_bio_5.tif  
#>               wc2.1_10m_bio_6.tif  
#> names       : wc2.1_10m_bio_5, wc2.1_10m_bio_6 
#> min values  :       -29.68600,       -72.50025 
#> max values  :        48.08275,        26.30000
# create vector data
p <- data.frame("lon" = c(35.16556, 36.01378, 36.26056),
"lat" = c(36.25056, 36.55494, 36.97625)) |> vect(crs = "epsg:4326")
p
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 3, 0  (geometries, attributes)
#>  extent      : 35.16556, 36.26056, 36.25056, 36.97625  (xmin, xmax, ymin, ymax)
#>  coord. ref. : lon/lat WGS 84 (EPSG:4326)
# extract values from r by p
result <- extract(r, p)
result
#>   ID wc2.1_10m_bio_5 wc2.1_10m_bio_6
#> 1  1              NA              NA
#> 2  2        31.14160         6.70880
#> 3  3        31.52525         3.71825
# export extracted values to disk in csv format
write.csv(result, "result.csv")

创建于2022-12-18与reprex v2.0.2

最新更新