1

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

使用该函数SpatialPolygonsDataFrame读入时,ESRI shapefile 被视为一个。shapefile()

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

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

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

然后我使用包中的spTransform()函数rgdal将 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

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

非常感谢,提前。

4

1 回答 1

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)

在此处输入图像描述

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

于 2015-06-05T00:34:58.400 回答