使用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()只返回list类型的NULL对象。我假设这个问题是由坐标引用的显式强制引起的。
此外,以下是对每个数据集使用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
我在这些论坛上搜索了太多类似的问题,但都没有解决。有人能帮我(虚拟)一把吗?
非常感谢你,提前。
发布于 2015-06-05 08:34:58
这(数据似乎在空间上没有重叠)是一个常见的问题,源于对坐标参考系的混淆。发生这种情况有几个可能的原因。
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)

显然,这两个数据源不会重叠。只有你知道这两个中哪一个可能是正确的,你现在可以开始研究为什么另一个在错误的地方。
https://stackoverflow.com/questions/30513797
复制相似问题