简体   繁体   English

R 光栅 function 将不接受 WKT 格式的 crs

[英]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.

相关问题 当我将其投影到新的CRS(R中的projectRaster函数)时,为什么我的栅格地图的值会发生变化? - Why the values of my raster map change when I project it to a new CRS (projectRaster function in R)? R中栅格和shapefile的对齐范围:使用相同的CRS从QGIS导入 - Aligning extents for raster and shapefile in R: import from QGIS with same CRS 将 70 多个光栅文件堆栈的坐标参考系统 (CRS) 转换为 stack::raster () 格式 - Converting the Coordinate Reference System (CRS) of a Stack of 70+ Raster Files in stack::raster () Format 识别栅格文件中的CRS - Identify CRS in raster file 投影(光栅)= NA 但光栅有一组 crs - projection(raster) = NA but raster has a set crs 在 R 中从 .netCDF 文件创建光栅砖时出现 crs 投影信息的问题 - Problems with crs projection information when creating raster brick from netCDF file in R 尝试在R中将CRS分配给栅格时,为什么会出现错误? - Why am I getting an error when trying to assign a CRS to a raster in R? 在R中的栅格中计算对数函数 - Calculate logarithm function in a raster in R 是否有 R 函数来更改栅格的投影? - Is there an R function to change the projection of raster? R function 将多边形(sf,wkt)转换为掩码(矩阵,数组) - R function to convert polygon (sf, wkt) into mask (matrix, array)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM