首页 > 解决方案 > 在 R 中更改分辨率、裁剪和绘制光栅图像

问题描述

我正在使用 landsat-7 图像数据(TIF 文件),您可以从中下载 USGS 地球资源管理器。TIF 文件有 9 个图像,因此我希望能够将图像的每个分辨率更改为 10x10,然后将图像裁剪到我感兴趣的特定区域,然后重新绘制每个图像。这在R中可能吗?我已经包含了我对一个特定图像的尝试的一些实际/伪代码。由于确切的图像太大而无法上传,我已经包含了一个可以下载图像的区域 - 但是,任何图像示例都可以让我通过。

library(raster)
library(rgdal)
grey <- raster("image") ##loading image
gr.disaggregate <- disaggregate(gr.disaggregate, fact=3) ##turning into 10x10 from 30x30
r.pts <- rasterToPoints(gr.disaggregate, spatial=TRUE) ##This keeps running forever until R aborts. 
## if it did my thought process would be the following
geo.prj <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" 
r.pts <- spTransform(r.pts, CRS(geo.prj)) 
r.pts@data <- data.frame(r.pts@data, long=coordinates(r.pts)[,1],
                         lat=coordinates(r.pts)[,2])                         
ext <- extent(xmin, xmax, ymin, ymax)
r.pts2 <- crop(r.pts, ext) 
##I would then want to replot the resolution increased/cropped image. 

更新:我正在使用的任何示例图像都可以在这里找到。更新了注释以准确描述问题。

感谢您的帮助,谢谢!

标签: rstatisticsgeospatialr-rasterrgdal

解决方案


要放大或缩小(更改分辨率),您可以在 r中使用aggregate或运行 作为示例disaggregate

library(raster)

grey <- raster("image") ##loading image
#this function will give you the resolution of your raster
res(grey)

#disaggregate/downscale from 30 x 30 resolution to 10 x 10 (factor = 3)
grey_agg <- disaggregate(grey, fact=3)

##this function will give you the resolution of your raster after changing resolution
res(grey_agg)

这是基于此解决方案
根据裁剪,您可以使用cropmask功能

#load the required shapefile (namely as shp)
grey_cropped <- crop(grey_upscaled, shp)

推荐阅读