R 栅格函数不接受 WKT 格式的 crs

R raster function will not accept crs in WKT format

我正在尝试生成栅格并为其分配 CRS 投影。但是,我的 CRS 是新的 WKT 格式,并且 raster() 函数要求我提供 proj4string。到目前为止,这是我的代码:

library(sf)
library(raster)

crs_dem <- st_crs(
  'PROJCS["NAD_1983_2011_StatePlane_California_II_FIPS_0402",
    GEOGCS["GCS_NAD_1983_2011",
      DATUM["D_NAD_1983_2011",
         SPHEROID["GRS_1980",6378137.0,298.257222101]],
       PRIMEM["Greenwich",0.0],
       UNIT["Degree",0.0174532925199433]],
     PROJECTION["Lambert_Conformal_Conic"],
     PARAMETER["False_Easting",2000000.0],
     PARAMETER["False_Northing",500000.0],
     PARAMETER["Central_Meridian",-122.0],
     PARAMETER["Standard_Parallel_1",38.33333333333334],
     PARAMETER["Standard_Parallel_2",39.83333333333334],
     PARAMETER["Latitude_Of_Origin",37.66666666666666],
     UNIT["Meter",1.0]]')

ext <- extent(1895000, 1935000, 579500, 616500)
grid <- raster(ext, resolution = c(40,40), crs = crs(dem))

上面的代码生成了一个 crs = NA 的栅格。我也试过用 crs(grid)<- 和 projection(grid)<- 分配它,但没有成功。如何让我的特定 CRS 文件与此栅格关联?

@slamballais 的回答成功了!昨晚我也通过反复试验发现了另一种(不太干净)的方法,所以这两种解决方案都在这里。

选项 1:

test <- sp::CRS(crs_dem$input)
grid <- raster(ext, resolution = c(40,40), crs = test)

选项 2:

library(dplyr)
aoi <- ext %>% 
  as('SpatialPolygons') %>% 
  st_as_sf %>% 
  st_set_crs(crs_dem)
grid <- raster(ext, resolution = c(40,40), crs = projection(aoi))

raster 需要文本,所以如果你有 wkt 格式的 crs,你可以直接使用它。无需创建更复杂的对象。

crs_dem <- 'PROJCS["NAD_1983_2011_StatePlane_California_II_FIPS_0402",
    GEOGCS["GCS_NAD_1983_2011", DATUM["D_NAD_1983_2011", SPHEROID["GRS_1980",6378137.0,298.257222101]],
     PRIMEM["Greenwich",0.0], UNIT["Degree",0.0174532925199433]],
     PROJECTION["Lambert_Conformal_Conic"], PARAMETER["False_Easting",2000000.0],
     PARAMETER["False_Northing",500000.0], PARAMETER["Central_Meridian",-122.0],
     PARAMETER["Standard_Parallel_1",38.33333333333334], PARAMETER["Standard_Parallel_2",39.83333333333334],
     PARAMETER["Latitude_Of_Origin",37.66666666666666], UNIT["Meter",1.0]]'
     
library(raster)
r <- raster(crs=crs_dem)

或者,如果您从 sf 对象开始

r <- raster(crs=crs_dem$input)