Я пытаюсь извлечь значения пикселей из растра путем вкрапления с 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