[英]How to change the resolution of a raster layer in R
我正在使用 R 中的幾個高分辨率柵格圖層。 我正在運行的某些分析的細節水平過高,因此我想通過降低分辨率來加快速度。
坐標系是 UTM,所以單位是米。 分辨率說它是 30, 30 (x, y)。 所以看起來這里的分辨率是30m。
有人可以告訴我如何將分辨率更改為 120m 嗎? 我已閱讀 resample() 和 projectRaster() 函數的幫助,但它們似乎需要具有所需分辨率的模板光柵,而我沒有。
這是我的柵格圖層之一的示例:
alt.utm
類:光柵層
尺寸:4572、2495、11407140(nrow、ncol、ncell)
分辨率 : 30, 30 (x, y)
范圍:421661、496511、4402939、4540099(xmin、xmax、ymin、ymax)
坐標。 參考 : +proj=utm +zone=13 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
數據源:在內存中
名稱:層
值:1485.127、4275.202(最小值、最大值)
library(raster)
#get some sample data
data(meuse.grid)
gridded(meuse.grid) <- ~x+y
meuse.raster <- raster(meuse.grid)
res(meuse.raster)
#[1] 40 40
#aggregate from 40x40 resolution to 120x120 (factor = 3)
meuse.raster.aggregate <- aggregate(meuse.raster, fact=3)
res(meuse.raster.aggregate)
#[1] 120 120
#disaggregate from 40x40 resolution to 10x10 (factor = 4)
meuse.raster.disaggregate <- disaggregate(meuse.raster, fact=4)
res(meuse.raster.disaggregate)
#[1] 10 10
以下是如何執行此操作的示例。 (鏈接到原始)
#########################################################################
# SubsampleImageRaster.r
#
# This function demonstrates resampling of raster images to a new
# spatial resolution using the R raster package.
#
# Author: Rick Reeves
# Date created: 6-October- 2010
# Date modified:
# NCEAS
#
#########################################################################
#
SubsampleImageRaster <- function()
{
library(raster)
sOutFile <- ""
resampleFactor <- 4 # For test, subsample incoming image by factor of 10
# Read the mosaic components, stored in a subfolder, into a raster object list.
# Within the same loop, obtain the (geographic) extent of each component.
# Note: these images do not have same spatial extent, so they cant be stored
# in a rasterStack. Instead, use a list of rasterLayers.
setwd("./ForUseCase")
inFiles <- list.files(pattern="*.tif")
nFiles <- length(inFiles)
inputRaster <- raster()
CoarseResampRaster <- raster()
FineResampRaster <- raster()
for (iCtr in 1 : nFiles)
{
message(sprintf("resampling file: %s",inFiles[iCtr]))
inputRaster <- raster(inFiles[iCtr])
# The aggregate() / disaggregate methods resample rasters to COARSER (bigger cells)
# and FINER (smaller cells) resolutions, respectively
CoarseResampRaster <- aggregate(inputRaster,fact=resampleFactor,fun=mean)
sOutFile <- sprintf("CoarseSubsamp%s",inFiles[iCtr])
writeRaster(CoarseResampRaster,filename=sOutFile,format="GTiff",datatype="INT1U",overwrite=TRUE)
FineResampRaster <- disaggregate(inputRaster,fact=resampleFactor,fun=mean)
sOutFile <- sprintf("FineSubsamp%s",inFiles[iCtr])
writeRaster(FineResampRaster,filename=sOutFile,format="GTiff",datatype="INT1U",overwrite=TRUE)
}
message("resample demo")
browser()
# second method: use the resample() method from raster package
# Simple example:
# This code produces a resampled raster, 's',
# with correct resampled values. e.g.;
# s[] prints a vector of resampled cell values.
r <- raster(nrow=3, ncol=3)
r[] <- 1:ncell(r)
s <- raster(nrow=10, ncol=10)
s <- resample(r, s, method='bilinear')
# Useful example:
# Resample a satellite image, stored in a GeoTiff file
# into a NEW raster with 2x spatial resolution in
# both dimensions (four times the number of cells)
# Here is the technique:
# 1) Create a new raster object with the correct 'resampled' number of cells.
# 2) Set the extent (geographic 'bounding box') of the new raster
# to the extent of the original raster
# 3) Generate the resampled raster.
resampleFactor <- .5 # reduce the cell size by 50% and double the number of rows and columns.
inputRaster <- raster("TmB50MosaicImg1.tif")
inCols <- ncol(inputRaster)
inRows <- nrow(inputRaster)
resampledRaster <- raster(ncol=(inCols / resampleFactor), nrow=(inRows / resampleFactor))
extent(resampledRaster) <- extent(inputRaster)
# The resample method will write the resampled raster image to a NEW disk file..
resampledRaster <- resample(inputRaster,resampledRaster,datatype="INT1U",method='bilinear',filename="testOutResamp.tif",overwrite=TRUE)
# Or, use writeRaster method to create the output file.
writeRaster(resampledRaster,filename="ResampleProduct.tif",format="GTiff",datatype="INT1U",overwrite=TRUE)
# end
}
我已經嘗試了3種不同的選項來升級DEM文件。 首先我像這樣使用gdal_translate
:
from CMD:
gdal_translate -tr 0.1 0.1 "C:\dem.tif" "C:\dem_0.1.tif"
在R:
res(raster('C:\dem_0.1.tif')
[1] 0.1 0.1
然后我嘗試了R中raster
包的aggregate
和resample
命令:
#
r <- raster("dem.tif")
> res(r)
[1] 0.0002777778 0.0002777778 # original dem resolution
#
r_up_0.1 <- aggregate(r, fact = 0.1/res(r)) # aggregate output
> res(r_up_0.1)
[1] 0.1 0.1
#
s <- raster(nrow = 10, ncol = 10)
extent(s) <- extent(r)
s <- resample(r, s, method = 'bilinear') # resample output
> res(s)
[1] 0.1000278 0.1000278
這些是輸出: 3個輸出被升級為res = 0.1x0.1
,它們之間存在一些差異,但我打算使用gdal輸出。
希望這可以幫助。
這是一個遲到的答案,但創建模板柵格和使用projectRaster
非常簡單。 您可以將模板光柵分辨率設置為您想要的任何值,如果您想要矩形而不是方形單元格,則包括兩個值:
# Create the template raster, with 120m cells
TEMPLATE.RASTER <- raster(extent(ORIGINAL.RASTER), resolution = 120,
crs = st_crs(ORIGINAL.RASTER)$proj4string)
# Project the original raster to the new resolution
PROJECTED.RASTER <- projectRaster(from = ORIGINAL.RASTER, to = TEMPLATE.RASTER)
我喜歡創建一個模板,因為它也復制了目標 CRS。 但是,您實際上可以跳過創建模板柵格並直接在projectRaster
設置分辨率和 CRS。 請檢查幫助文件,因為有計算方法等選項,這可能很重要。
這些天我們可以使用terra
,它是raster
包的替代品
示例數據
library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
聚合柵格單元格。 從 30 到 120m 是 4 倍
a1 <- aggregate(r, 4)
您可以對行和列使用不同的因子,也可以使用不同的聚合函數(默認為“均值”)
a2 <- aggregate(r, c(2,3), fun=sum)
你也可以走另一條路並分解:
d <- disagg(a2, 2)
聚合只能組合整個單元格。 但是要合並來自不同來源的柵格數據,您可能需要匹配未對齊的柵格幾何。 在這種情況下,您可以使用resample
未對齊的SpatRaster
:
x <- rast(r)
res(x) <- 0.01
解決方案
x <- resample(r, x)
也可能是您想將柵格數據轉換為具有另一個坐標參考系統(“地圖投影”)的幾何圖形。 你可以這樣做:
u1 <- project(r, "+proj=utm +zone=32")
但是,與矢量數據不同,柵格數據的轉換沒有明確定義。 因此,首選方法是提供您希望輸出與之對齊的模板。 這通常是您已經從另一個數據源獲得的SpatRaster
。 但在這里我為示例創建了一個:
temp <- rast(xmin=264000, xmax=324000, ymin=5479000, ymax=5565000, res=100, crs="+proj=utm +zone=32")
現在使用它
u2 <- project(r, temp)
進一步閱讀
我最近要求降低ggmap對象的分辨率。 這包括提取和轉換ggmap柵格(使用Robin Lovelace的ggmap_rast()),聚合此線程中討論的柵格,然后用下面較低分辨率的柵格替換ggmap高分辨率柵格。 希望這很有用:
original_map <- get_map("New York", scale = 1) #download a map at lowest resolution
#ggmap_rast function courtesy of Robin Lovelace
original_map.rast1 <- ggmap_rast(original_map) #extract RasterStack from ggmap object
original_map.rast2 <- aggregate(original_map.rast, 2) #compress raster
rast2_length <- sqrt(length(original_map.rast2)/3) ## find the number of cells in compressed raster
map <- ggmap::ggmap(original_map)
# from https://stackoverflow.com/questions/44225063/plot-ggmap-image-over-raster
r <- map$layers[[2]]$geom_params$raster #pull hex raster pixel values from ggmap object
#x <- r[,] #assign values to a variable (probably unnecessary)
xx <- r[1:rast2_length,1:rast2_length] #filler values for raster to be created
rgb_map <- original_map.rast2
for(i in 1:rast2_length){
for(j in 1:rast2_length){
k=i*rast2_length+j
#many rgv values are non-integers; rgb2hex requires integer
red = as.integer(round(rgb_map$layer.1[k],0))
green = as.integer(round(rgb_map$layer.2[k],0))
blue = as.integer(round(rgb_map$layer.3[k],0))
#rgb2hex from ggtern package
xx[i,j] <- rgb2hex(red,green,blue)
#rgb2hex is slow; export, calculate in excel, import is faster, sadly
#xx[i,j] <- as.character(rgb_from_excel$V1[k])
}
}
map$layers[[2]]$geom_params$raster <- xx
map
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.