This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pseudo.R
88 lines (70 loc) · 1.84 KB
/
Pseudo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/Rscript
## Script to generate pseudoabsence points from record data
## each new file will contain 1000 pseudoabsence records
## Guillermo Huerta Ramos
# start with a new environment
rm(list = ls())
# load libraries
library("dismo")
library("maptools")
# get file names from input data to use for the loop
file.names <- dir("./data/data_out/rarf")
# this directory will contain csv files with final data
dir.create("./data/data_out/pseudo")
# this directory will contain png files with presence and pseudoabsence points
dir.create("./data/data_out/pseudomaps")
setwd("./data/data_out/pseudo")
for (i in file.names){
setwd("../rarf")
print(paste0("reading ", i, " records"))
subs<-read.table(i, header = T, sep = ",",stringsAsFactors = F, row.names = "X")
attach(subs)
# define circles with a radius of 50 km around the subsampled points
x = circles(subs[,c("lon","lat")], d=50000, lonlat=T)
# draw random points that must fall within the circles in object x
bg = spsample(x@polygons, 1000, type='random', iter=1000)
mg=data.frame(bg)
##Draw custom map
setwd("../pseudomaps")
maxx<-max(lon,na.rm = T)
if (maxx<0) {
maxx<-(maxx+5)
} else{
maxx<-(maxx-5)
}
minx<-min(lon,na.rm = T)
if (minx<0) {
minx<-(minx-2)
} else{
minx<-(minx+2)
}
maxy<-max(lat,na.rm = T)
if (maxy<0) {
maxy<-(maxy-1)
} else{
maxy<-(maxy+1)
}
miny<-min(lat,na.rm = T)
if (minx<0) {
miny<-(miny-1)
} else{
miny<-(miny+1)
}
data(wrld_simpl)
xlim=c(minx, maxx)
ylim=c(miny, maxy)
print(paste0("writing plot file for ", i))
png(i)
plot(wrld_simpl,xlim=xlim, ylim=ylim, axes=T)
box()
# plot the presence points
points(bg, col='black', pch=20, cex=0.75)
#plot pseudoabsence points
points(lon,lat, col='orange', pch=20, cex=0.75)
dev.off()
setwd("../pseudo")
print(paste0("writing peudoabscence file for ", i))
write.csv(bg,i)
detach(subs)
}
print(paste0("Job finished"))