簡體   English   中英

R中與柵格的空間相關性

[英]Spatial Correlation in R with rasters

我想創建一個空間相關性,以了解當地降水量如何與指定年度期間的年降水量相關。 我希望結果是一個柵格圖層,顯示周圍區域與特定點的相關程度。 我在下面附上了一張圖片,顯示了 1910-2019 年期間 Akcakale 周圍的降水相關性,其中未顯示 <3 和 >-3 的相關性。

有沒有辦法使用 raster 包在 R 上執行此操作?

1910-2019年Akcakale降水對比

所以,我已經想通了。 此代碼需要以 SpatialPointsDataFrame(使用 sp)和空白柵格(tempraster)的形式給出一個位置,以便結果進入。var_stack 是感興趣變量的柵格堆棧。 在這種情況下,我使用了 CHELSA 提供的月降水量值。 這將返回一個柵格文件,顯示與所提供位置的空間相關性。

library(raster)
library(sp)


# Load rasters
files <- list.files(pattern = "pr_", full.names = T)
for(i in 1:length(files)){
  temp <- raster(files[i])
  if(i == 1) precip_stack <- stack(temp)
  if(i ! = 1) precip_stack <- addLayer(precip_stack,temp)
 }

# Create tempraster
resolution <- res(precip_stack)
tempraster <- raster(
  nrows = ny, ncols = nx,
  crs = '+init=EPSG:4326',
  xmn = -180, xmx = 180,
  ymn = -90, ymx = 90,
  res = c(resolution,resolution)
)

#Create location. An example. For SPDFs, lon before lat.

location <- as.data.frame(cbind("Reigate", -0.20651382829652656, 
51.237555987546344))

location$V2 <- as.numeric(location$V2)
location$V3 <- as.numeric(location$V3)

location <- SpatialPointsDataFrame(location[2:3], location)

SpatialCorr <- function(location,var_stack,tempraster){
  loc_extract <- raster::extract(var_stack,location, df =T)
  loc_extract <- t(loc_extract)
  loc_extract <- loc_extract[-1,]

  lay_vars <- var_stack[]

  for(i in 1:nrow(lay_vars)){
    temp <- lay_vars[i,]

    if(is.na(temp)==F){
      res <- cor(loc_extract,temp)
    }

    if(is.na(temp)==T) res <- NA

    if(i == 1) Output <- res
    if(i != 1) Output <- rbind(Output,res)
  }

  tempraster[] <- Output
  result <- tempraster
  return(result)
}

Reigate_pr_cor <- SpatialCorr(location,precip_stack,tempraster)
plot(Reigate_pr_cor)

示例數據

library(terra)
library(geodata)
w <- geodata::worldclim_country("Belgium", "prec", ".")
pt <- cbind(5, 51)

解決方案

loc <- extract(w, pt) |> unlist()
x <- app(w, \(i) cor(i, loc))

和一張地圖

plot(x)
points(pt)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM