-
Notifications
You must be signed in to change notification settings - Fork 1
/
FindNextLight2.R
71 lines (65 loc) · 2.85 KB
/
FindNextLight2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
FindNextLight2<-function(gdm.rast, occ.table, env.vars, aoi, add.site=NULL){
#Finds the next most complementary or disimilar site to sampled sites
#within a set of target sites.
#Args:
# occ.table: data frame with species occurrence information.
# Must contain lon,lat,species and site columns.
# env.vars: RasterStack or RasterBrick of environmental
# variables that define gradients of interest
# add.site: data frame with x and y coordinates of sites
# that should be added to the sampled site set
# for iterative selection of survey sites. See
# example.
#
#Returns:
# A list object containing:
# dist.raster: Raster of ED complementarity values
# dist.table: data frame of x,y and ED complementarity
# values for all target sites.
#Example:
# Pending.
require(gdm)
require(reshape2)
#Create a cell id raster
t.raster <- env.vars[[1]]
t.raster[1:ncell(t.raster)]<-1:ncell(t.raster)
t.raster[is.na(prod(env.vars))]<-NA
#Prepare table for gdm prediction in unsampled sites
sampled <- na.omit(unique(cellFromXY(t.raster, occ.table[,c("lon","lat")])))
sampled <- na.omit(t.raster[sampled])
if(!is.null(add.site)){
cell2add <- cellFromXY(t.raster,add.site)
sampled <- c(sampled, cell2add)
}
#sampled<-sample(sampled,100)
t.raster[sampled] <- NA
t.raster<-mask(t.raster, aoi)
usmpled <- na.omit(unique(getValues(t.raster)))
site.grid <- expand.grid(sampled, usmpled)
gdm.pred.table <- cbind(distance=0, weights=1,
xyFromCell(t.raster, site.grid$Var1),
xyFromCell(t.raster, site.grid$Var2),
env.vars[site.grid$Var1],
env.vars[site.grid$Var2])
# colnames(gdm.pred.table)<-colnames(gdm.table)
gdm.pred.table<-na.omit(gdm.pred.table)
#Compute GDM to new table
gdm.pred <- predict(gdm.rast, gdm.pred.table)
res <- data.frame(c.sampled = site.grid$Var1, c.unsampl = site.grid$Var2, dis=gdm.pred)
res2 <- dcast(res, c.unsampl~c.sampled, value.var="dis")
min.dis.vals <- apply(res2[, 2:ncol(res2)],1,min) #For all candidate sites,
#find the minimum distance
#to a surveyed site
#Plot distances from sampled to unsampled sites
dist2sampled <- t.raster
dist2sampled[t.raster] <- 0
dist2sampled[res2$c.unsampl] <- min.dis.vals
# plot(dist2sampled,col=rev(heat.colors(10)))
#Order sites in decreasing order of predicted biological distance
#First site on list is the most disimilar site to surveyed sites
dists <- data.frame(xyFromCell(t.raster,res2$c.unsampl), dist=min.dis.vals)
dists <- dists[order(-dists$dist),]
# points(dists[1,1:2],col="blue",pch=18)
# points(occ.table[,c("lon","lat")],pch=18,cex=0.6)
return(list(dist.raster=dist2sampled, dist.table=dists))
}