【问题标题】:R - 'raster' package - RasterLayer ("raster") does not extract to SpatialPolygonsDataFrame ("sp"); Simply returns NULL objectR - 'raster' 包 - RasterLayer(“raster”)不提取到 SpatialPolygonsDataFrame(“sp”);只返回 NULL 对象
【发布时间】:2015-05-28 17:55:23
【问题描述】:

使用raster 包,我读入了两个数据集,一个ASCII 栅格和一个ESRI shapefile。我想将栅格(水温)数据提取到 shapefile 的全部范围,即湖岸线。

使用shapefile() 函数读入时,ESRI shapefile 将被视为SpatialPolygonsDataFrame

shapefile <- shapefile("shore.shp",verbose=TRUE)

我使用raster()函数读入了ASCII光栅。

raster <- raster("1995_001.asc",native=TRUE,crs="+proj=merc +datum=WGS84 +ellps=WGS84 +units=m")

shapefile的坐标参考信息为:

+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0

光栅的(即使用raster() 函数中的crs 参数强制转换为以下内容):

+proj=merc +datum=WGS84 +ellps=WGS84 +units=m +towgs84=0,0,0

然后我使用 rgdal 包中的 spTransform() 函数将 shapefile 的空间参考强制转换为栅格的空间参考。

spTransform(shapefile, CRS(projection(raster)))

最后,我提交了以下内容:

extract(raster,shapefile,method="simple",fun=mean,small=TRUE,na.rm=TRUE,df=FALSE)

但是,extract() 只返回 NULL 类型为 list 的对象。我认为这个问题是由坐标引用的显式强制引起的。

另外,下面是对每个数据集使用show()函数的结果:

> show(raster) class : RasterLayer dimensions : 1024, 1024, 1048576 (nrow, ncol, ncell) resolution : 1800, 1800 (x, y) extent : -10288022, -8444822, 4675974, 6519174 (xmin, xmax, ymin, ymax) coord. ref. : NA data source : in memory names : layer values : -9999, 8.97 (min, max)

> show(shapefile) class : SpatialPolygonsDataFrame features : 1 extent : 597568.5, 998261.6, 278635.3, 668182.2 (xmin, xmax, ymin, ymax) coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0 variables : 3 names : AREA, PERIMETER, HECTARES min values : 59682523455.695, 5543510.075, 5968252.346 max values : 59682523455.695, 5543510.075, 5968252.346

我在这些论坛上搜索了大量类似的问题,但没有得到解决。有人可以借我一个(虚拟的)手吗?

非常感谢,提前。

【问题讨论】:

    标签: r shapefile coordinate-systems r-raster sp


    【解决方案1】:

    这(数据似乎在空间上没有重叠)是一个常见问题,源于对坐标参考系统的混淆。发生这种情况可能有几个原因。

    1) 区域实际上不重叠

    2)也许您将 crs 分配给 RasterLayer 是错误的(或多边形的分配)。请出示对象(show(raster));在询问有关 raster 包的问题时,这几乎总是有帮助的。

    3) 您没有分配 spTransform 的结果。请记住,R 通常不会进行“就地”修改。确保做shapefile <- spTransform(shapefile, crs(raster))

    始终进行健全性测试,然后按原路返回,直到一切正常。从这里开始的地方是做

    plot(raster)
    plot(shapefile, add=TRUE)
    

    查看是否有任何多边形绘制在栅格数据之上。

    如果这不起作用,请查看范围。例如像这样(顺便说一句,这也显示了如何创建一个独立的可重复的示例/问题,其他人实际上可以回答):

    library(raster)
    # the spatial parameters of your raster
    r <- raster(ncol=100, nrow=100, xmn=-10288022, xmx=-8444822, ymn=4675974, ymx=6519174, crs="+proj=merc +datum=WGS84")
    values(r) <- 1:ncell(r)
    
    # the extent of your SpatialPolygons
    ep <- extent(597568.5, 998261.6, 278635.3, 668182.2)
    p <- as(ep, 'SpatialPolygons')
    crs(p) <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83"
    
    # tranform both data set to longlat
    rgeo <- projectRaster(r, crs='+proj=longlat +datum=WGS84')
    pgeo <- spTransform(p, CRS('+proj=longlat +datum=WGS84'))
    
    # plot on top of Google map
    library(dismo)
    e <- union(extent(rgeo), extent(pgeo))
    g <- gmap(e,lonlat=TRUE)
    plot(g, inter=TRUE)
    plot(extent(rgeo), add=TRUE, col='red', lwd=2)
    plot(pgeo, add=TRUE, col='blue', lwd=2)
    

    显然这两个数据源不重叠。只有你知道这两者中哪一个可能是正确的,你现在可以开始研究为什么另一个在错误的地方。

    【讨论】:

    • 感谢您的回复。您是正确的,因为这两个数据集在空间上没有重叠。我已经应用了你的每一个建议,但结果和以前一样。事实证明,ASCII 光栅没有在读入和检查“显示(光栅)”时定义的坐标系。我该如何为 R 中的 'raster' 和 'shapefile' 定义坐标系和投影,以便它们在空间上重叠?
    • 我知道 ASCII 文件不存储任何 CRS 信息(在许多方面它是一种非常糟糕的文件格式)。如果您可以向我们展示show(raster),那么我们至少可以评估crs="+proj=merc +datum=WGS84 是否合理(除非您确定 知道这一点)。我建议您编辑您的问题,并包括show(shapefile)
    猜你喜欢
    • 2015-05-08
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    相关资源
    最近更新 更多