Mask and crop a raster from shapefile in R

Mask and crop

单景

在这里插入图片描述
在这里插入图片描述

Mask

raster_mask <- mask(raster,shp)
plotRGB(raster_mask,stretch="lin")
writeRaster(raster_mask,
            "G:/Rdata/neon_data/NEONDSLandsatNDVI/NEON-DS-Landsat-NDVI/mask/197_HARV_landRGB_mask.tif")

在这里插入图片描述

Crop

raster_mask_crop <-crop(raster,extent(shp))
plotRGB(raster_mask_crop,stretch = "lin")
writeRaster(raster_mask_crop,
            "G:/Rdata/neon_data/NEONDSLandsatNDVI/NEON-DS-Landsat-NDVI/mask/197_HARV_landRGB_mask_crop.tif")

在这里插入图片描述

多景

library(pacman)
p_load(raster, sf, rgdal)
wd <- "G:/Rdata/neon_data/NEONDSLandsatNDVI/NEON-DS-Landsat-NDVI/HARV/2011/RGB/"
setwd(wd)

shp <- st_read("G:/Rdata/neon_data/NEONDSLandsatNDVI/NEON-DS-Landsat-NDVI/Mask.shp")
all_RGB <-list.files(paste0(wd),full.names = TRUE,pattern = ".tif$")
all_RGB

for (afile in all_RGB){
  rgb <- stack(afile)
  a1 <- substr(afile,73,85)
  rgb_crop<-crop(rgb, extent(shp))
  writeRaster(rgb_crop,paste0("G:/Rdata/neon_data/NEONDSLandsatNDVI/NEON-DS-Landsat-NDVI/HARV/2011/RGB/1/",a1),format="GTiff")
}

在这里插入图片描述
欢迎关注个人公众号GeoSuper
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值