-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToyExample.R
52 lines (47 loc) · 1.72 KB
/
ToyExample.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
#Toy implementation with Boyacá data
library(raster)
library(rgdal)
library(gdm)
sp.data<-read.csv("C:/Modelos/BirdRichness/Modelos/summary/ebdcol_v2.csv",as.is=T)
env.data<-stack(paste0("Z:/Capas/NewExtent/","bio_",c(1,2,3,4,12,15,18),".asc"))
col<-readOGR("D:/Datos/BaseLayers","COL_adm1")
##Perform analysis on Boyacá data only
#Filter to Boyaca
boy<-col[col@data$NAME_1=="Boyacá",]
env.boy<-crop(env.data,boy)
env.boy<-mask(env.boy,boy)
sp.boy<-sp.data[which(!is.na(extract(env.boy,sp.data[,c(3,2)]))),]
template<-env.boy[[1]]
template[1:ncell(template)]<-1:ncell(template)
template[is.na(env.boy[[1]])]<-NA
sp.boy$inCol<-extract(template,sp.boy[,c(3,2)])
sp.boy<-na.omit(sp.boy)
sp.env<-extract(env.boy,sp.boy[,c(3,2)])
sp.env<-cbind(sp.boy[,2:4],sp.env)
#occ.table: lon, lat, species and site data frame
occ.table<-sp.boy
colnames(occ.table)<-c("species","lat","lon","site")
rm(list=setdiff(ls(), c("occ.table","env.boy")))
#Iteratively find best locations
while(response!="S"){
if(exists(new.sites)){
iter<-FindNext(occ.table, env.vars, add.site=new.sites)
} else {
new.sites<-data.frame()
iter<-FindNext(occ.table, env.vars, add.site=NULL)
}
title(paste("ED = ",round(cellStats(iter$dist.raster,"sum"))))
response <- readline(prompt="Choose option: (I)gnore location, (A)dd this location, Add (C)ustom location, (S)top ")
if(response=="I"){
#Code to remove site from target areas
}
if(response=="A"){
new.sites <- rbind(new.sites, iter$dist.table[1, 1:2])
}
if(response=="C"){
coords <- readline(prompt="Enter comma separated coordinates(lon, lat): ")
coords <- as.numeric(strsplit(coords,",")[[1]])
new.sites <- rbind(new.sites, coords)
}
#If not any of the above set response to S
}