R raster function will not accept crs in WKT format

335 Views Asked by At

I'm trying to generate a raster and assign it a CRS projection. However, my CRS is in the new WKT format, and the raster() function is requiring me to provide a proj4string. Here's my code so far:

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))

The code above generates a raster with crs = NA. I've also tried assigning it with crs(grid)<- and projection(grid)<- with no luck. How can I get my specific CRS file to associate with this raster??

2

There are 2 best solutions below

0
On BEST ANSWER

@slamballais's answer did the trick! I also found another (slightly less clean) method through trial and error last night, so here are both solutions.

Option 1:

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

Option 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))
0
On

raster expects text, so if you have a wkt format crs, you can use that directly. There is no need to create a more complex object.

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)

or, if you start with an sf object

r <- raster(crs=crs_dem$input)