【发布时间】:2020-05-07 21:45:02
【问题描述】:
我想在 geoTIFF 中提取 4 个光栅子图像中心的坐标(并创建一个数据框)并绘制 2 个选定子图像的边框,这些子图像最初在 r2 图像中分割,为此我试试:
library(raster)
library(rgeos)
#create GeoTIFF raster
r <- raster(ncol=100, nrow=100)
s <- stack(lapply(1:3, function(i) setValues(r, runif(ncell(r)))))
f1 <- file.path(tempdir(), "sl1.tif")
writeRaster(s,f1, format="GTiff",datatype="FLT4S",overwrite=TRUE)
#crop in 4 sub-images
r2<-stack(raster("sl1.tif"))
SplitRas <- function(raster,ppside,save,plot){
h <- ceiling(ncol(raster)/ppside)
v <- ceiling(nrow(raster)/ppside)
agg <- aggregate(raster,fact=c(h,v))
agg[] <- 1:ncell(agg)
agg_poly <- rasterToPolygons(agg)
names(agg_poly) <- "polis"
r_list <- list()
for(i in 1:ncell(agg)){
e1 <- extent(agg_poly[agg_poly$polis==i,])
r_list[[i]] <- crop(raster,e1)
}
if(save==T){
for(i in 1:length(r_list)){
f1 <- file.path(tempdir(), paste0("sample_",i,sep=""))
writeRaster(r_list[[i]], f1,
format="GTiff",datatype="FLT4S",overwrite=TRUE)
}
}
if(plot==T){
par(mfrow=c(ppside,ppside))
for(i in 1:length(r_list)){
plot(r_list[[i]],axes=F,legend=F,bty="n",box=FALSE)
}
}
return(r_list)
}
splitRBG<-SplitRas(raster=r2,ppside=2,save=TRUE,plot=FALSE)
#
#read the 4 images
r.files <-list.files(tempdir(), pattern = "sample")
#[1] "sample_1.tif" "sample_2.tif" "sample_3.tif" "sample_4.tif"
# get coordinates data from the center of each image
RES<-NULL
for(i in 1:length(r.files)){
value <- raster::extract(?????????) ## Here Im loosing for extract the center of each image
RES<-rbind(RES,cbind(r.files[i],coordinates(value))) #create a data frame of the results
}
colnames(RES)<-c("r.files","xcoord","ycoord")
首先我想用这些信息创建一个数据框
r.files xcoord ycooord
sample_1.tif -100 -50
sample_2.tif 50 -52
sample_3.tif 120 50
sample_4.tif 120 -30
这是我的第一个问题,因为我没有找到在预期之前提取每个图像中心的方法:
#Finally draw a square with the images and points in center of coordinates only for sample_3 and sample_4
del<-c("sample_1.tif","sample_2.tif")
r.files2<-r.files [-del]
RES2<-RES[,-(1:2)]
image(r2)
for(i in 1:length(r.files2)){
e <- extent(r.files2[i])
pp <- rasterToPolygons(e, dissolve=TRUE)
plot(pp, border='green') #Only countour color
points(RES2[i][,2], RES2[i][,3],col="red")
}
您有什么更聪明的解决方案吗?谢谢
【问题讨论】: