[英]How do I replace the XY coordinates of a raster in R?
我意識到這個問題的標題類似於另一個問題,但那里的解決方案對我不起作用。
我有一個光柵文件。 我有一個緯度矩陣和一個經度矩陣。 我想用兩個矩陣中的坐標替換光柵文件的 x 和 y 坐標。
我在這里上傳了示例文件。 有三個文件,一個是柵格 (GeoTIFF) 和兩個經緯度矩陣。
## read in the raster
raster <- raster("test.tif")
## Read in the coordinates
lat1 <- read.csv("lat1.csv")
lon1 <- read.csv("lon1.csv")
raster$lon <- lon1 ##??? no clue
raster$lat <- lat1 ##???
https://www.dropbox.com/sh/0lsdgo3mk95hua8/AAA9fMuCo2XZmu50U6QFq4cEa?dl=0
有什么簡單的方法可以做到這一點? 我可以在 RasterBrick 上使用相同的方法嗎?
我嘗試了很多東西,我找不到如何替換坐標。
任何幫助,將不勝感激。
像這樣的事情可能會有所幫助:
library(raster)
library(rgdal)
# starting raster:
r<- raster(matrix(1:100, nrow = 10))
# new coordinates:
x <- 51:60
y <- 21:30
xy <- expand.grid(x, y)
# create new raster via SpatialPixelsDataFrame, copying the values from the original raster but supplying the new coordinates
r2 <- raster(SpatialPixelsDataFrame(xy, data.frame(values(r))))
# plot to have a look:
par(mfrow = c(1,2))
plot(r)
plot(r2)
請注意,柵格以 xy 坐標為像素的中心,給出的范圍可能會讓您感到驚訝:
extent(r2)
但你可以輕松適應任何你想要的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.