【问题标题】:Changing resolution, cropping and plotting Raster images in R在 R 中更改分辨率、裁剪和绘制光栅图像
【发布时间】:2020-04-08 14:40:49
【问题描述】:

我正在处理 landsat-7 图像数据(TIF 文件),您可以从 USGS earth explorer 下载这些数据。 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. 

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

感谢您的帮助,谢谢!

【问题讨论】:

  • 你能提供一个可重现的例子吗? gr.disaggregate 的属性是什么?您收到什么错误消息?
  • gr.disaggregate 将是 10x10 分辨率的图像,而不是最初的 30x30。没有错误消息,它只是继续运行(R 中止并在一段时间后关闭)。
  • 您无需将 gr.disaggregate 栅格转换为点即可对其进行裁剪。只需使用crop( gr.disaggregate, extent) (注意在栅格的同一坐标系中定义范围)。

标签: r statistics geospatial r-raster rgdal


【解决方案1】:

要放大或缩小(更改分辨率),您可以在 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)

这是基于this solution
根据裁剪,您可以使用crop 或mask 函数

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

【讨论】:

  • 我已经按照上面的代码执行了大部分操作。我已经编辑了我的问题,以便更好地了解我想要完成的工作
  • 我收集到您的问题出在 rasterToPoints 函数中,因为它需要永远运行。您是否尝试过在 Rstudio 之外运行它?也许,跳过 rastertopoint 函数并将光栅保存为 ascii 文件,然后将其作为数据点读取。
猜你喜欢
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
相关资源
最近更新 更多