使用bbox在R中剪辑图像

问题描述

我有一个 shapefile 的边界框。我想使用这个 bBox 来剪辑我同时拥有的光栅和 shapefile 图像。

我尝试使用 gIntersection 和crop(见下文)。不确定是否需要将 bBox 转换为多边形才能执行此操作。

raster = county raster 
plot_parcel_bb = bBox 
county_shp = county shape

    #4.Use bounding Box to clip the appropriate county imagery tile (raster&vector in QGIS)

#Check class
class(raster) 
class(plot_parcel_bb)

#Check extents
extent(raster)
extent(plot_parcel_bb)

#set the raster image extent to match the shp extent
new_raster<- setExtent(raster,ext = county_shp)
extent(new_raster)

#check the raster and plot parcels crs
st_crs(new_raster)
st_crs(plot_parcel_bb)

#make sure both crs's match 
crs(new_raster) <- "epsg:4326"
st_crs(new_raster)


#this does not overlap
cropped<- crop(new_raster,extent(plot_parcel_bb),filename = "cropped",snap = 'near')

我是否需要覆盖图像 shapefile 和光栅并从中剪辑 bBox?也许我在这里错误的点开始。我不确定首先要采取哪个步骤。

在这个网站的正确位置 - https://datacarpentry.org/r-raster-vector-geospatial/11-vector-raster-integration/

谢谢

解决方法

请参阅 ?raster::crop?terra::crop

中的示例

示例数据

library(raster)
p <- shapefile(system.file("external/lux.shp",package="raster"))
r <- raster(extent(p) + 3,resolution=0.1)
values(r) <- 1:ncell(r)

使用多边形范围裁剪栅格

x <- crop(r,p)

据我所知,您似乎对一些基础知识有些困惑,最好先学习教程,例如来自https://rspatial.org/