R 中的栅格:给定像素大小的栅格图像子集
raster in R: Subset a raster image given a pixel size
我想在给定坐标中心点和所需的 x 和 y 大小(以像素为单位)的情况下对光栅图像进行子集化。在我的例子中:
library(raster)
set.seed(5)
##Create a raster
r <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84", xmn=0, xmx=50, ymn=0, ymx=50)
s1 <- stack(lapply(1:4, function(i) setValues(r, runif(ncell(r)))))
# Create 10 random points in the raster
pts <- data.frame(pts=sampleRandom(s1, 10, xy=TRUE)[,1:2], status=rep(c("A","B"),5))
现在,我想创建 10 张新图像,它们将是一个子集(子集图像的 X 期望宽度 = 5 像素,子集图像的 Y 期望宽度 = 5 像素),中心点 (pts $x, pts$y), 具有相同的像素大小。最后,将每个图像保存为 GeoTIFF:
for(i in 1:length(pts)){
writeRaster(r[[i]],filename=paste(pts,"_",i,sep=""),
format="GTiff",datatype="FLT4S",overwrite=TRUE)
}
这可能吗?
嘿,我能够通过围绕您的点创建方形缓冲区来做到这一点,该缓冲区大到足以捕获 5 x 5 的光栅。然后我使用这些缓冲区来剪辑你的光栅。这是我的代码和注释:
library(raster)
library(rgeos)
set.seed(5)
##Create a raster
r <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84", xmn=0, xmx=50, ymn=0, ymx=50)
s1 <- stack(lapply(1:4, function(i) setValues(r, runif(ncell(r)))))
# Create 10 random points in the raster
pts <- data.frame(pts=sampleRandom(s1, 10, xy=TRUE)[,1:2], status=rep(c("A","B"),5))
pts_points = SpatialPointsDataFrame(pts[,1:2], data.frame(pts[,3]))
## Creating square buffers around your points
pts_buffer = gBuffer(pts_points, byid = TRUE, capStyle = "SQUARE", width = 12.5)
## looping through all your points
for (i in 1:nrow(pts_buffer)){
square = pts_buffer[i,]
val = square@data$pts...3. ## Gets the value of the point ("A" or "B")
## Contains a stack of raster clipped for the current point for the raster stack
clipped = mask(s1, square)
## Export the clipped raster here
}
缓冲区使用 12.5 的宽度以确保创建 5 x 5 的光栅。要更改栅格的子集大小,只需更改缓冲区的宽度即可!
这是缓冲点在栅格顶部的样子:
在代码中,我们遍历每个正方形并将栅格裁剪到该区域,下面是栅格堆栈在被其中一个区域裁剪后的样子:
让我知道这是否有帮助,或者是否有任何需要澄清的地方!
我想在给定坐标中心点和所需的 x 和 y 大小(以像素为单位)的情况下对光栅图像进行子集化。在我的例子中:
library(raster)
set.seed(5)
##Create a raster
r <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84", xmn=0, xmx=50, ymn=0, ymx=50)
s1 <- stack(lapply(1:4, function(i) setValues(r, runif(ncell(r)))))
# Create 10 random points in the raster
pts <- data.frame(pts=sampleRandom(s1, 10, xy=TRUE)[,1:2], status=rep(c("A","B"),5))
现在,我想创建 10 张新图像,它们将是一个子集(子集图像的 X 期望宽度 = 5 像素,子集图像的 Y 期望宽度 = 5 像素),中心点 (pts $x, pts$y), 具有相同的像素大小。最后,将每个图像保存为 GeoTIFF:
for(i in 1:length(pts)){
writeRaster(r[[i]],filename=paste(pts,"_",i,sep=""),
format="GTiff",datatype="FLT4S",overwrite=TRUE)
}
这可能吗?
嘿,我能够通过围绕您的点创建方形缓冲区来做到这一点,该缓冲区大到足以捕获 5 x 5 的光栅。然后我使用这些缓冲区来剪辑你的光栅。这是我的代码和注释:
library(raster)
library(rgeos)
set.seed(5)
##Create a raster
r <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84", xmn=0, xmx=50, ymn=0, ymx=50)
s1 <- stack(lapply(1:4, function(i) setValues(r, runif(ncell(r)))))
# Create 10 random points in the raster
pts <- data.frame(pts=sampleRandom(s1, 10, xy=TRUE)[,1:2], status=rep(c("A","B"),5))
pts_points = SpatialPointsDataFrame(pts[,1:2], data.frame(pts[,3]))
## Creating square buffers around your points
pts_buffer = gBuffer(pts_points, byid = TRUE, capStyle = "SQUARE", width = 12.5)
## looping through all your points
for (i in 1:nrow(pts_buffer)){
square = pts_buffer[i,]
val = square@data$pts...3. ## Gets the value of the point ("A" or "B")
## Contains a stack of raster clipped for the current point for the raster stack
clipped = mask(s1, square)
## Export the clipped raster here
}
缓冲区使用 12.5 的宽度以确保创建 5 x 5 的光栅。要更改栅格的子集大小,只需更改缓冲区的宽度即可!
这是缓冲点在栅格顶部的样子:
在代码中,我们遍历每个正方形并将栅格裁剪到该区域,下面是栅格堆栈在被其中一个区域裁剪后的样子:
让我知道这是否有帮助,或者是否有任何需要澄清的地方!