【问题标题】:extracting multiple raster pixel values with shapefile and looping it with a list of radiuses使用 shapefile 提取多个光栅像素值并使用半径列表对其进行循环
【发布时间】:2020-07-04 17:21:26
【问题描述】:

我要解决的问题是我想使用包含多个点位置的 shapefile 提取多个栅格的像素值,对于每个点我想提取多个栅格“时间序列”的值,并且还循环遍历要在提取函数中输入的缓冲区半径列表,我想要的最终结果是每个缓冲区的每个站点的 csv

到目前为止,我确实设法使用光栅库在 R 下编写了一个程序,我的代码由两个循环组成,第一个循环进入每个光栅并提取与 shapefile 中多个位置的位置相对应的像素值,第二个包含要循环以提取每个像素半径的均值、最大值和最小值的半径列表

但此时代码过于松散,没有有效集成半径循环

library(raster)
library(rgdal)
library(stringr)
library(ggplot2)
library(lubridate)

raster_files <- list.files('J:/nvc',pattern="*.tif",full.names = T)
r_name<- list.files(path='J:/nvc',pattern="*.tif",full.names = F)
shape<-readOGR('J:/nvc/locations/locations.shp')
radius <- read.csv('J:/nvc/radius.csv')
rList <- list() 
statList <- list() 


for (j in 1:nrow(radius)){
  for(i in 1:length(raster_files)){
    ras <- raster(raster_files[i])
    stack <- stack(ras)
    crs(stack) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84"
    mean <- raster::extract(stack,shape,buffer=j,fun=mean,df=TRUE)
    min <- raster::extract(stack,shape,buffer=j,fun=max,df=TRUE)
    max <- raster::extract(stack,shape,buffer=j,fun=min,df=TRUE)
    Name <- r_name[i]
    
    statList[[i]] <- data.frame(max)
    df <- do.call(rbind.data.frame,statList)
  }
}

注意:我只是命名 3 个尝试的地点,alpha bravo 和 charlie

半径为 1,2,4,6,8,10,20 公里

栅格被命名为 1h-2020042601 , 1h-2020042602,1h-2020042603 ..... 例如:每小时 1 个栅格

【问题讨论】:

    标签: python r gis raster r-raster


    【解决方案1】:

    这是一个最小的、可重现的、独立的示例。 您不需要遍历栅格。制作一个 RasterStack

    library(raster)
    #raster_files <- list.files('J:/nvc',pattern="*.tif",full.names = T)
    raster_files <- system.file("external/rlogo.grd", package="raster")
    stack <- stack(raster_files)
    points <- cbind(x=c(48, 53, 7), y=c(63, 43, 28))
    bufs <- c(5, 10)     
    

    循环的一种方法是使用 lapply

    x <- lapply(bufs, function(b) extract(stack, points, buffer=b))
    

    返回一个列表列表。在这种情况下为

    # 2 buffers
    length(x)
    #[1] 2
    # 3 points
    sapply(x, length)
    #[1] 3 3
    

    计算所需的统计数据

    r <- rapply(x, function(i) c(min(i, na.rm=TRUE), mean(i, na.rm=TRUE), max(i, na.rm=TRUE)))
    

    然后把它很好地组合在一起

    r <- matrix(r, ncol=3, byrow=TRUE)
    colnames(r) <- c("min", "mean", "max")
    r <- cbind(buf=rep(bufs, each=nrow(points)),
        point=rep(1:nrow(points), length(bufs)), r)
    r
    #     buf point min     mean max
    #[1,]   5     1   0 119.4375 255
    #[2,]   5     2   0 132.6292 255
    #[3,]   5     3  81 182.5958 255
    #[4,]  10     1   0 156.1361 255
    #[5,]  10     2   0 154.1814 255
    #[6,]  10     3  74 189.7354 255
    

    【讨论】:

    • 非常感谢您的解决方案,它对我帮助很大,但我必须为给您带来的不便深表歉意,最小最大值和平均统计数据是在提取函数中的每个缓冲区内计算的,例如: ``` extract(stack,points,buffer=b,fun=max) ``` 还有最小值和平均值
    • 它们不是“意图”(至少不是该函数的作者),这只是一种选择。在这种情况下,这是低效的;因为你需要提取 3 次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2021-12-01
    • 2021-09-30
    • 2022-06-15
    相关资源
    最近更新 更多