2013-03-14 73 views
-1

我正在尝试从列中包含物种名称,经度和纬度的矩阵创建栅格图层。创建栅格图层

的数据名称是“基地”,当我尝试使用的base用命令的范围内创建rasterlayer

r<-raster(base) 

我得到一个错误:

Error en .local(x, ...) : list has no "x" 

你愿意帮我?谢谢

+0

有机会的话,你的“基地”是一个数据帧。使用'as.matrix(基地)'可能会解决问题。 – Azim 2018-02-04 17:19:54

回答

2

你不会给我们太多的东西,但你可能想尝试一下(下面的例子ASSUMES你的坐标数据是使用WGS84数据的lat-long格式的。如果它不是在2-d空间的点表示会出错,所以请尽量找你的投影信息...

require(sp) 
require(raster) 
require(maptools) 


#Some dummy data in the format you have described 
base <- data.frame(longitude = sample(c(-20:20) , 10) , latitude = sample(c(-30:30) , 10) , species = sample(letters[1:3] , 10 , repl = TRUE)) 
head(base) 
#  longitude latitude species 
# 1  -10  7  a 
# 2   3  14  b 
# 3  -9  29  b 
# 4  -8  -25  a 
# 5   0  -19  b 
# 6  -19  19  c 

#Make spatial points dataframe, which will have a spatial extent 
spdf <- SpatialPointsDataFrame(base[ c("longitude" , "latitude") ] , data = data.frame(base$species) , proj4string = CRS("+proj=longlat +datum=WGS84")) 

#make raster based on the extent of your data 
r <- raster(extent(spdf)) 
r 
# class  : RasterLayer 
# dimensions : 10, 10, 100 (nrow, ncol, ncell) 
# resolution : 3.3, 5.8 (x, y) 
# extent  : -20, 13, -29, 29 (xmin, xmax, ymin, ymax) 
# coord. ref. : NA 
# values  : none