假设我有一个DatabPRI格栅和一个具有两个不同cr的简单特征(sf
)对象(点).我需要转换格栅的crs以匹配sf
个数据的crs(反之亦然,但我更希望摆脱Databis sin系统).各种try 都没有让我找到解决方案,即,在同一CRS中具有该格栅和sf
以进行进一步可视化、处理等.请帮助我如何做到这一点?以下是我try 过的几个 Select :
library(terra)
library(sf)
lai <- terra::rast("lai.tif")
terra::crs(lai) #check crs of lai
plots <- readRDS("plots.rds")
sf::st_crs(plots) #check crs of sf
#Trial 1
plots_tr <- sf::as_Spatial(plots) #transform the sf and get the crs
lai_pr1 <- terra::project(lai, "+proj=utm +zone=31 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs") #project the raster using the crs of the transformed sf
terra::plot(lai_pr1) #plot raster
plot(plots_tr, add=T, pch=19, col="black", cex=0.1) #overlay points
#Trial 2
lai_pr2 <- lai
terra::crs(lai_pr2) <- "+proj=utm +zone=31 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs" # replace lai crs
terra::plot(lai_pr2) #plot raster
plot(plots_tr, add=T, pch=19, col="black", cex=0.1) #overlay points
#Trial 3
lai_pr3 <- terra::project(lai, "epsg:25831") #project the raster using the EPSG code of the sf data (i.e., ETRS89 / UTM zone 31N)
terra::plot(lai_pr3) #plot raster
plot(plots_tr, add=T, pch=19, col="black", cex=0.1) #overlay points
链接到数据以运行此示例:
莱 -https://github.com/frandadamo/frandadamo/blob/main/lai.tif<
地块-https://github.com/frandadamo/frandadamo/blob/main/plots.rds
也许问题是sf
的crs是WKT,而格栅的crs不是?此外,使用sf
而不使用sf::as_Spatial()
也不会改变结果.我还try 用st_transform()
更改sf的crs.我读了上面已经发布的许多问题,但无法得到解决方案.Maybe spTransform()
?
希望这不会太傻.非常感谢.