forked from HeritageNetwork/Regional_SDM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1_pointsInPolys_cleanBkgPts.R
203 lines (165 loc) · 7.38 KB
/
1_pointsInPolys_cleanBkgPts.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File: 1_pointsInPolys_cleanBkgPts.r
# Purpose:
# 1. Sampling of EDM polygons to create random points within the polygons.
# these are the random presence points being created here, from polygon presence data.
# 2. Removing any points from the background points dataset that overlap or are near
# the input presence polygon dataset.
library(RSQLite)
library(sf)
library(dplyr)
####
# Assumptions
# - the shapefile is named with the species code that is used in the lookup table
# e.g. glypmuhl.shp
# - There is lookup data in the sqlite database to link to other element information (full name, common name, etc.)
# - the polygon shapefile has at least these fields EO_ID_ST, SNAME, SCOMNAME, RA
####
#### load input polys ----
setwd(loc_model)
# set up folder system for inputs
dir.create(paste0(model_species,"/inputs/presence"), recursive = T, showWarnings = F)
dir.create(paste0(model_species,"/inputs/model_input"), showWarnings = F)
# setwd(paste0(loc_model,"/",model_species,"/inputs/presence"))
# changing to this WD temporarily allows for presence file to be either in presence folder or specified with full path name
# load data, QC ----
presPolys <- st_zm(st_read(nm_presFile, quiet = T))
#check for proper column names. If no error from next code block, then good to go
#presPolys$RA <- presPolys$SFRACalc
shpColNms <- names(presPolys)
desiredCols <- c("EO_ID_ST", "SNAME", "SCOMNAME", "RA", "OBSDATE")
if("FALSE" %in% c(desiredCols %in% shpColNms)) {
stop(paste0("Column(s) are missing or incorrectly named: ", paste(desiredCols[!desiredCols %in% shpColNms], collapse = ", ")))
} else {
print("Required columns are present")
}
if(any(is.na(presPolys[,c("EO_ID_ST", "SNAME", "SCOMNAME", "RA")]))) {
stop("The columns 'EO_ID_ST','SNAME','SCOMNAME', and 'RA' (SFRACalc) cannot have NA values.")
}
#pare down columns
presPolys <- presPolys[,desiredCols]
# set date/year column to [nearest] year, rounding when day is given
presPolys$OBSDATE <- as.character(presPolys$OBSDATE)
presPolys$date <- NA
for (d in 1:length(presPolys$OBSDATE)) {
dt <- NA
do <- presPolys$OBSDATE[d]
if (grepl("^[0-9]{4}.{0,3}$", do)) {
# year only formats
dt <- as.numeric(substring(do,1,4))
} else {
if (grepl("^[0-9]{4}[-|/][0-9]{1,2}[-|/][0-9]{1,2}", do)) {
# ymd formats
try(dt <- as.Date(do), silent = TRUE)
} else if (grepl("^[0-9]{1,2}[-|/][0-9]{1,2}[-|/][0-9]{4}", do)) {
# mdy formats
try(dt <- as.Date(do, format = "%m/%d/%Y"), silent = TRUE)
}
# if still no match, or if failed
if (is.na(dt)) {
if (grepl("[0-9]{4}", do)) {
# use first 4-digit sequence as year
dt <- regmatches(do, regexpr("[0-9]{4}", do))
if (as.integer(dt) < 1900 | as.integer(dt) > format(Sys.Date(), "%Y")) {
# years before 1900 and after current year get discarded
dt <- Sys.Date()
} else {
dt <- as.Date(paste0(dt,"-01-01"))
}
}
# put additional date formats here
}
# give up and assign current date
if (is.na(dt)) {
dt <- Sys.Date()
}
dt <- round(as.numeric(format(dt, "%Y")) + (as.numeric(format(dt,"%j"))/365.25))
}
presPolys$date[d] <- dt
}
desiredCols <- c(desiredCols, "date")
# explode multi-part polys ----
suppressWarnings(shp_expl <- st_cast(presPolys, "POLYGON"))
#add some columns (explode id and area)
shp_expl <- cbind(shp_expl,
EXPL_ID = 1:length(shp_expl$geometry),
AREAM2 = st_area(shp_expl))
names(shp_expl$geometry) <- NULL # temp fix until sf patches error
#write out the exploded polygon set
nm.PyFile <- paste(loc_model, model_species,"inputs/presence",paste0(baseName, "_expl.shp"), sep = "/")
st_write(shp_expl, nm.PyFile, driver="ESRI Shapefile", delete_layer = TRUE)
####
#### Placing random points within each sample unit (polygon/EO) ----
####
# just in case convert to lower
names(shp_expl) <- tolower(names(shp_expl))
#calculate Number of points for each poly, stick into new field
shp_expl$PolySampNum <- round(400*((2/(1+exp(-(as.numeric(shp_expl$aream2)/900+1)*0.004)))-1))
#make a new field for the design, providing a stratum name
shp_expl <- cbind(shp_expl, "stratum" = paste("poly_",shp_expl$expl_id, sep=""))
# sample must be equal or larger than the RA sample size in the random forest model
shp_expl$ra <- factor(tolower(as.character(shp_expl$ra)))
# QC step: are any records attributed with values other than these?
raLevels <- c("very high", "high", "medium", "low", "very low")
if("FALSE" %in% c(shp_expl$ra %in% raLevels)) {
stop("at least one record is not attributed with RA appropriately")
} else {
print("RA levels attributed correctly")
}
#EObyRA <- unique(shp_expl[,c("expl_id", "eo_id_st","ra")])
shp_expl$minSamps[shp_expl$ra == "very high"] <- 5
shp_expl$minSamps[shp_expl$ra == "high"] <- 4
shp_expl$minSamps[shp_expl$ra == "medium"] <- 3
shp_expl$minSamps[shp_expl$ra == "low"] <- 2
shp_expl$minSamps[shp_expl$ra == "very low"] <- 1
shp_expl$finalSampNum <- ifelse(shp_expl$PolySampNum < shp_expl$minSamps,
shp_expl$minSamps,
shp_expl$PolySampNum)
ranPts <- st_sample(shp_expl, size = shp_expl$finalSampNum * 2)
ranPts.sf <- st_sf(ranPts)
names(ranPts.sf) <- "geometry"
st_geometry(ranPts.sf) <- "geometry"
ranPts.joined <- st_join(ranPts.sf, shp_expl)
# check for polys that didn't get any points
polysWithNoPoints <- shp_expl[!shp_expl$expl_id %in% ranPts.joined$expl_id,]
if(nrow(polysWithNoPoints) > 0){
stop("One or more polygons didn't get any points placed in them.")
}
# get actual finalSampNum
ranPts.joined2 <- ranPts.joined[0,]
for (ex in 1:length(shp_expl$geometry)) {
s1 <- shp_expl[ex,]
samps <- row.names(ranPts.joined[ranPts.joined$expl_id==s1$expl_id,])
if (length(samps) > s1$finalSampNum) samps <- sample(samps, size = s1$finalSampNum) # samples to remove
ranPts.joined2 <- rbind(ranPts.joined2, ranPts.joined[samps,])
}
ranPts.joined <- ranPts.joined2
rm(ex, s1, samps, ranPts.joined2)
#check for cases where sample smaller than requested
# how many points actually generated?
ptCount <- table(ranPts.joined$expl_id)
overUnderSampled <- ptCount - shp_expl$finalSampNum
#positive vals are oversamples, negative vals are undersamples
print(table(overUnderSampled))
# If you get large negative values then there are many undersamples and
# exploration might be worthwhile
names(ranPts.joined) <- tolower(names(ranPts.joined))
colsToKeep <- c("stratum", tolower(desiredCols))
ranPts.joined <- ranPts.joined[,colsToKeep]
# name of random points output shapefile
nm.RanPtFile <- paste(loc_model, model_species,"inputs/presence",paste(baseName, "_RanPts.shp", sep = ""), sep = "/")
# write it out
st_write(ranPts.joined, nm.RanPtFile, driver="ESRI Shapefile", delete_layer = TRUE)
###
### remove Coincident Background points ----
###
# get the background shapefile
backgShapef <- st_read(nm_bkgPts, quiet = T)
# find coincident points ----
polybuff <- st_transform(shp_expl, st_crs(backgShapef))
polybuff <- st_buffer(polybuff, dist = 30)
coincidentPts <- unlist(st_contains(polybuff, backgShapef, sparse = TRUE))
# remove them (if any)
if (length(coincidentPts) > 0) backgSubset <- backgShapef[-coincidentPts,] else backgSubset <- backgShapef
# write background points
outFileName <- paste0(loc_model,"/",model_species,"/inputs/model_input/", paste0(baseName, "_bkg_clean.shp"))
st_write(backgSubset, outFileName, driver="ESRI Shapefile", delete_layer = TRUE)