繁体   English   中英

如何从 R 中的大栅格中提取像素值?

[英]How can I extract pixels values from a large raster in R?

我正在尝试通过与 SPDF ( SpatialPolygonsDataFrame )相交从栅格中提取像素值,但出现错误:

 library(rgeos)
 library(raster)

我必须对其他一些栅格和 SPDF 执行相同的操作,类似于以下摘要中的那些:

 My SPDF:
 rings<-readOGR("SPDF.shp")

  sumarry(SPDF)
  Object of class SpatialPolygonsDataFrame
 Coordinates:
    min        max
 x -73.99045 -44.241589
 y -18.04159   5.271841
 Is projected: FALSE 
 proj4string : [ +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs]
 Data attributes:

      Hectares                       
  Min.   :     52    
  1st Qu.:  31110                              
  Median : 141736                              
  Mean   : 442267                              
  3rd Qu.: 531011                              
  Max.   :4203563


 My raster data :
 ras_PI<-raster("ras_PI.tif")

 ras_PI
 class      : RasterLayer 
 dimensions : 86662, 111765, 9685778430  (nrow, ncol, ncell)
 resolution : 0.0002689995, 0.0002690002  (x, y)
 extent     : -73.97832, -43.91358, -18.04061, 5.271491  (xmin, xmax, ymin, ymax)
 crs        : +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs 
 source     : C:/Users/kleds/OneDrive/Documentos/mestrado/PRODES_APs/pAP_PI.tif 
 names      : pAP_PI 
 values     : 0, 1  (min, max)

这里开始我的代码:

dirs<-"~/prodes/PRODES_APs"


 work_dirs<-"~/prodes/PRODES_APs"

#Create a for to define the rasters directory, and to be used in the subsequent for

for (m in 1:length(dirs)) {
files<-file.path(dirs[m],list.files(path = dirs[m], pattern = ".tif"))
nomes <- list.files(path = dirs[m], pattern = ".tif")
nomes <- substr(nomes,1,nchar(nomes)-4)


 }


#create a for to call simultaneously raster layer of interest, and each SPDF (initial polygons, rings and control)


#vectors to use in the for 
AP<-c("PI","TI","UN","US")
AW <- c("arc","wood")
km<-c("min","1km_","2km_","3km_","4km_","5km_","6km_","7km_","8km_","9km_",10km_,"10km","20km","30km","40km","50km","60km","70km", "controle")

 #empty Data Frame to save my results
 results<-data.frame()



for (a in 1:(min(length(files), length(AP)))){
 setwd(work_dirs)

 r<-files[a]
 i<- AP[a]
 map<- raster(r)

for(k in AW){

for(j in km){


   # deffine the directory 
  setwd(paste0("~/prodes/buff_",k,"/AP_rings"))
  getwd()


 # Call each SPDF 
  SPDF<- readOGR(".", paste0("ring",k,j, i))

  names(SPDF)[names(rings) == "X__i__"] <- "TIPO"


  # reproject the SPDF  to ALbers 
  rings <- spTransform(rings, CRSobj = "+proj=longlat +ellps=GRS80 
 +towgs84=0,0,0,0,0,0,0 +no_defs ")

  #Extract the pixels values 
  ( extrc <- extract(map, SPDF, na.rm=T) )

  #proportion calculation for each class

 (class.prop = lapply(extrc, function(x) 
 {prop.table(table(factor(x,levels=c(0,1))))}))


  p.prop = setNames(
    do.call(
      rbind.data.frame,
      class.prop),
    c("Desmatado","natural"))

  p.prop$ID<-seq_along(p.prop[,1])


  rings$ID<- 1:length(SPDF)


  freq <- merge(SPDF, p.prop) #add to polygons

  frequenc<-as.data.frame(freq)

  View(frequenc)



  results <- rbind(results, frequenc)
  setwd("~/prodes/resultados")
  write.table(results, file="resultados.txt", sep="\t", row.names=F)

}
}
}

错误:在我上面的代码中

   ( extrc <- extract(ras_PI, rings, na.rm=T) )

    Cannot allocate large size vector 225.4 mb

该消息似乎很清楚。 您正在提取大量数据。

所以要么有很多环和/或它们相当大。 因此,您可能需要提供 function 参数来extract或循环环。

也许你做错了什么——但我们没有什么可说的,因为你指的是我们没有的数据。 show(rings)可能会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM