【问题标题】:Why am I getting an error when trying to assign a CRS to a raster in R?为什么在尝试将 CRS 分配给 R 中的栅格时出现错误?
【发布时间】:2013-10-29 18:45:03
【问题描述】:

我正在使用 R 中的 raster 包创建栅格,我想明确指定栅格的坐标参考系 (CRS),以便在我使用 writeRaster() 将对象保存到时将其编码到 geotif一份文件。我试图按照 raster() 函数的帮助文件中的指示指定 CRS,但它返回了一个未使用的参数错误,如下面的最小工作示例所示。

为什么会失败?如何为栅格设置 CRS?

library(raster)
set.seed(2013)
df <- data.frame( x = rep( 0:1, each=2 ),
                  y = rep( 0:1,  2),
                  l = rnorm( 4 ))

spg <- df
coordinates(spg) <- ~ x + y
gridded(spg) <- TRUE

rasterDF <- raster(spg, crs="+proj=longlat +datum=WGS84")
# Error in .local(x, ...) : 
#   unused argument (crs = "+proj=longlat +datum=WGS84")

【问题讨论】:

  • @tim riffe,感谢您发现我遗漏了图书馆电话。
  • 它不起作用的原因是因为当您尝试从SpatialPointsDataFrame 创建栅格时没有crs 参数。如果您查看?raster 的帮助页面的使用列表,您将看到您正在转换的每种类型的对象都有哪些参数可用。相反,要么按照@ialm 的建议进行操作,要么制作没有投影信息的栅格,然后通过projection(rasterDF) &lt;- "+proj=longlat +datum=WGS84" 添加它,或者通过projection(rasterDF) &lt;- CRS("+proj=longlat +datum=WGS84") 更好地添加,这样CRS 函数可以检查您是否有有效的CRS 字符串。呸!

标签: r spatial raster


【解决方案1】:

您可以在创建栅格对象之前使用proj4string(spg) &lt;- "your CRS" 将投影设置为您的空间数据框。投影信息应该会传递到您新创建的栅格图层。

这对我有用:

library(raster)
set.seed(2013)
df <- data.frame( x = rep( 0:1, each=2 ),
                  y = rep( 0:1,  2),
                  l = rnorm( 4 ))

spg <- df
coordinates(spg) <- ~ x + y
gridded(spg) <- TRUE

# Add the projection information to spg
proj4string(spg) <- "+proj=longlat +datum=WGS84"

rasterDF <- raster(spg)

# Check that it worked
rasterDF
# class       : RasterLayer 
# dimensions  : 2, 2, 4  (nrow, ncol, ncell)
# resolution  : 1, 1  (x, y)
# extent      : -0.5, 1.5, -0.5, 1.5  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
# data source : in memory
# names       : l 
# values      : -0.6674423, 1.360611  (min, max)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 2016-09-01
    • 2015-02-04
    相关资源
    最近更新 更多