簡體   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