如何在R中使用多边形shapefile裁剪图像?

时间:2018-11-20 08:21:27

标签: r crop raster shapefile

library(raster)
library(rgdal)

图片

RGBimage <- stack("E:/Amit Haldar/Work/1.0 Projects/RGB/D035_Georef.tif")
stackImage <- stack(RGBimage[[1]], RGBimage[[2]], RGBimage[[3]])
plotRGB(stackImage)

Raster (RGB) image

形状文件

Fiber <- readOGR("E:/Amit Haldar/Work/1.0 Projects/Shp/Landuse2018.shp")
data <- Fiber
data$vb<- paste(data$SECTOR, data$ESTATE, data$COMPNO, sep="")
shp <- subset(data, vb=="D035")
crs(shp) <- "+proj=utm +zone=47 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
shp_simp <- gSimplify(shp, tol = 3, topologyPreserve = TRUE)
plot(shp_simp)

shape boundary

print(shp_simp)
class       : SpatialPolygons 
features    : 1 
extent      : 759548.4, 760468.4, -13233.4, -12185.39  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 

print(stackImage)
class       : RasterStack 
dimensions  : 21660, 20544, 444983040, 3  (nrow, ncol, ncell, nlayers)
resolution  : 0.0562666, 0.0562666  (x, y)
extent      : 759433.3, 760589.2, 9986667, 9987886  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=47 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : TEW_D035_Georef.1, TEW_D035_Georef.2, TEW_D035_Georef.3 
min values  :                 0,                 0,                 0 
max values  :               255,               255,               255 

我正在尝试使用形状文件裁剪图像。形状文件范围不一定与图像相同。多边形覆盖了图像的一部分。我尝试了几种方法

proj4string(stackImage) <- proj4string(shp_simp)

然后

cropImage <- crop(stackImage, extent(shp_simp), snap="out")

Error in .local(x, y, ...) : extents do not overlap

我试图变换投影

compExtent <- spTransform(shp_simp, CRS(proj4string(stackImage)))

cropImage <- crop(stackImage, extent(compExtent), snap="out")

仍然是相同的错误

Error in .local(x, y, ...) : extents do not overlap

我要去哪里错了?感谢您的提前支持。

0 个答案:

没有答案