我在现有条件下以及未来的气候变化情景下为多个物种运行了Maxent。我使用nicheOverlap函数和Schoener的D统计量来量化当前和未来合适的栖息地之间的变化。我研究中的很多生物只是向上移动更远的山脉,所以有很多重叠,因为未来的分布在目前的分布范围内(在较高海拔处占据较少的区域)。通过查看QGIS中的ascii文件,我可以看到未来在面积方面不太合适的栖息地,所以我想量化这个。我已经在互联网上搜索了一个计算栅格区域的好方法,并且从未发现任何非常适合我喜欢的东西。因此,我写了一些东西,它是各种脚本的点点滴滴的融合。它贴在下面。
两个问题: 1)你们都同意这是我正在做的事情(以平方公里计算面积)吗? 2)有没有办法简化这个?具体来说,你会看到我从光栅到数据帧再回到光栅?也许我可以留在栅格?
感谢您的任何意见!
丽贝卡
####
library(raster)
#load rasters
m <- raster("SpeciesA_avg.asc")
mf <- raster("SpeciesA_future_layers_avg.asc")
#change to dataframe
m.df <- as.data.frame(m, xy=TRUE)
#get rid of NAs
m.df1 <- na.omit(m.df)
#keep only cells that that have a suitability score above 0.5 (scores range from 0 to 1)
m.df2 <- m.df1[m.df1$SpeciesA_avg> 0.5,]
#re-rasterize just the suitable area
m.raster <- rasterFromXYZ(m.df2)
##same as above but for future projection
mf.df <- as.data.frame(mf, xy=TRUE)
mf.df1 <- na.omit(mf.df)
mf.df2 <- mf.df1[mf.df1$SpeciesA_future_layers_avg>0.5,]
mf.raster <-rasterFromXYZ(mf.df2)
#get sizes of all cells in current distribution raster
#note my original layers were 30 seconds or 1 km2.
cell_size<-area(m.raster, na.rm=TRUE, weights=FALSE)
#delete NAs from all raster cells. It looks like these come back when switching from dataframe to raster
cell_size1<-cell_size[!is.na(cell_size)]
#compute area [km2] of all cells in raster
raster_area_present<-length(cell_size1)*median(cell_size1)
raster_area_present
#get sizes of all cells in future raster [km2]
cell_size<-area(mf.raster, na.rm=TRUE, weights=FALSE)
#delete NAs from vector of all raster cells
cell_size1<-cell_size[!is.na(cell_size)]
#compute area [km2] of all cells in geo_raster
raster_area_future<-length(cell_size1)*median(cell_size1)
raster_area_future
##calculate change in area
dif_area <- raster_area_present - raster_area_future
dif_area
答案 0 :(得分:1)
当您提出问题时,您应该提供一个简单的自包含示例。不只是转储指向我们没有的文件的脚本。写一个简单的例子教你的R,并经常帮助你自己解决问题。无论如何,我在这里是一些示例数据和解决方案,我认为:
library(raster)
#example data
m <- mf <- raster(ncol=10, nrow=10, vals=0)
m[,1] <- NA
m[,3:7] <- 1
mf[,6:9] <- 1
# get rid of NAs (the example has none); should not be needed
m <- reclassify(m, cbind(NA, NA, 0))
mf <- reclassify(mf, cbind(NA, NA, 0))
# keep cells > 0.5 (scores range from 0 to 1)
m <- round(m)
mf <- round(mf)
# now combine the two layers, for example:
x <- m + mf * 10
# area of each cell
a <- area(x)
# sum area by class
z <- zonal(a, x, sum)
# zone value
#[1,] 0 152327547
#[2,] 1 152327547
#[3,] 10 101551698
#[4,] 11 101551698
区域0是&#34;不是当前的,也不是未来的&#34;,1是&#34;仅当前&#34;,10是&#34;仅限未来&#34; 11是当前和未来&#34; 面积以m ^ 2为单位。
您可能希望查看有关maxent和其他空间分布模型的本教程:http://rspatial.org/sdm/