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



我正在字符串中通过与SPDF(SpatialPolygonsDataFrame(的intersect从光栅中提取像素值,但给出了一个错误:

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

消息看起来非常清晰。您正在提取大量数据。

因此,要么有很多环,要么它们相当大。因此,您可能需要为extract提供一个函数参数,或者在环上循环。

也许你做错了什么——但我们无法判断,因为你指的是我们没有的数据。show(rings)可能有助于

最新更新