[英]R raster function will not accept crs in WKT format
I'm trying to generate a raster and assign it a CRS projection.我正在尝试生成一个栅格并为其分配一个 CRS 投影。 However, my CRS is in the new WKT format, and the raster() function is requiring me to provide a proj4string.
但是,我的 CRS 采用新的 WKT 格式,并且 raster() function 要求我提供 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.上面的代码生成一个 crs = NA 的栅格。 I've also tried assigning it with crs(grid)<- and projection(grid)<- with no luck.
我也试过用 crs(grid)<- 和 projection(grid)<- 分配它,但没有运气。 How can I get my specific CRS file to associate with this raster??
如何让我的特定 CRS 文件与此栅格相关联?
@slamballais's answer did the trick, I also found another (slightly less clean) method through trial and error last night. @slamballais 的回答成功了,昨晚我还通过反复试验找到了另一种(稍微不太干净)的方法。 so here are both solutions.
所以这里有两种解决方案。
Option 1:选项1:
test <- sp::CRS(crs_dem$input)
grid <- raster(ext, resolution = c(40,40), crs = test)
Option 2:选项 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 expects text, so if you have a wkt format crs, you can use that directly. raster 需要文本,所以如果你有 wkt 格式的 crs,你可以直接使用它。 There is no need to create a more complex object.
无需创建更复杂的 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或者,如果你从
sf
object
r <- raster(crs=crs_dem$input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.