-
Notifications
You must be signed in to change notification settings - Fork 0
/
eucd_effort_subset.R
192 lines (153 loc) · 6.35 KB
/
eucd_effort_subset.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
library(data.table)
library(magrittr)
library(plyr)
library(dplyr)
library(rgdal)
library(raster)
library(rgeos)
### Define helper functions
no_s <- function(x){
require(magrittr)
sub("S","",x) %>% as.numeric
}
collapse_shared <- function(x){
require(magrittr)
require(plyr)
require(dplyr)
rbind(
x %>% filter(group_identifier == "")
,
x %>% filter(group_identifier != "") %>% group_by(group_identifier) %>% filter(no_s(sampling_event_identifier)==sort(no_s(sampling_event_identifier))[1]) %>% ungroup
)
}
get_month <- function(x){
require(magrittr)
substr(x,6,7) %>% as.numeric
}
get_year <- function(x){
require(magrittr)
substr(x,1,4) %>% as.numeric
}
### READ IN EUCD DATA
fread('ebd_sampling_relOct-2018/ebd_sampling_relOct-2018.txt',drop="TRIP COMMENTS") -> effort
names(effort) <- names(effort) %>% tolower %>% gsub("[/ ]","_",.)
effort <- effort %>%
filter(state_code %in% c("US-AK","CA-BC","US-WA","US-OR","US-CA")) %>%
filter(state != "Washington" | county %in% c("San Juan","Island","Whatcom","Skagit","Snohomish","King","Pierce","Thurston","Mason","Kitsap","Clallam","Jefferson","Grays Harbor","Pacific","Wahkiakum")) %>%
filter(state != "Oregon" | county %in% c("Columbia","Clatsop","Tillamook","Lincoln","Lane","Douglas","Coos","Curry")) %>%
filter(state != "California" | county %in% c("Del Norte","Humboldt","Mendocino","Sonoma","Marin","Napa","Solano","Contra Costa","Alameda","Santa Clara","San Mateo","San Francisco","Santa Cruz","Monterey","San Luis Obispo","Santa Barbara","Ventura","Los Angeles","Orange","San Diego"))
effort <- effort %>% collapse_shared
effort <-
effort %>% select(last_edited_date,country_code,state_code,county,locality_id,latitude,longitude,observation_date,observer_id,sampling_event_identifier,protocol_type,protocol_code,all_species_reported,group_identifier)
write.csv(effort,"effort_stateprovcty_column_filtered.csv")
### On MacBook ###
load_effort_stateprovcty_column_filtered <- function(){
fread("effort/effort_stateprovcty_column_filtered.csv") %>%
filter(get_year(observation_date) != 2018 | get_month(observation_date) < 9) %>% # Omit observations after August 2018, to match effort dataset to EUCD coded dataset
filter(get_year(observation_date) > 2006) # Use records 2007 and newer, since species comments were introduced in June 2006
}
latlong_proj <- "+proj=longlat +datum=WGS84 +no_defs"
utm10_proj <- "+proj=utm +zone=10 +datum=WGS84 +units=m +no_defs"
### READ IN OCEAN DATA
ocean_shp <- readOGR("ne_10m_ocean",layer="ne_10m_ocean")
crs(ocean_shp)
extent(ocean_shp)
## Make table of distinct localities
load_effort_stateprovcty_column_filtered() -> effort
nrow(effort)
#effort %>% filter(get_year(observation_date) == 2018) %>% use_series(observation_date) %>% get_month %>% range
#effort %>% filter(get_year(observation_date) != 2018) %>% use_series(observation_date) %>% get_month %>% range
loctable <- effort %>% distinct(locality_id,.keep_all=T)
rm(effort)
xy <- loctable[,c('longitude','latitude')]
#spdf <- SpatialPointsDataFrame(coords = xy,
# data = eucd,
# proj4string = crs(latlong_proj))
sps <- SpatialPoints(coords = xy,proj4string = crs(latlong_proj))
rm(xy)
#spdf <- spTransform(spdf,crs(ocean_shp))
#spdf <- spTransform(spdf,utm10_proj)
#sps <- as(spdf,'SpatialPoints')
crs(sps)
extent(sps)
sps <- spTransform(sps,utm10_proj)
crs(sps)
extent(sps)
nrow(loctable) # Number of distinct LocIDs
## Uncomment this section if making new ocean raster, otherwise readRDS, at bottom:
#Sys.time()
#r <- raster(extent(sps),
# nrows=(extent(sps)@ymax-extent(sps)@ymin)/2000,
# ncols=(extent(sps)@xmax-extent(sps)@xmin)/2000,
# crs=crs(sps),
# vals=NA)
#r <- projectRaster(from=r,crs=crs(ocean_shp))
## Now I have a raster of NAs, at the extent of EUCD, with Ocean projection
#Sys.time()
#r <- rasterize(ocean_shp, r) #Takes 4 minutes
#rm(ocean_shp)
#saveRDS(r,"raster_2000m_latlong.rds")
readRDS("raster_2000m_latlong.rds") -> r
Sys.time()
## Extract whether point is <5 miles from Ocean, 1000 at a time, takes ~ 30 min
## Uncomment to use this by-1000-at-a-time version, otherwise use all-at-once version below
#dest <- rep(NA,length(sps))
#sps.index <- 1:length(sps)
#split.factor.index <- ceiling(seq_along(sps.index)/1000)
#d_by_1000 <- split(sps.index, split.factor.index)
#for (i in seq_along(d_by_1000)){
# dest[d_by_1000[[i]]] <- extract(r, spTransform(sps[d_by_1000[[i]]], latlong_proj), buffer=8047, fun=function(x){1 %in% x}, na.rm=F)
# print(i*1000/length(sps)*100)
#}
#loctable$near_ocean <- dest
#rm(dest,i,split.factor.index,sps.index)
## Extract whether point is <5 miles from Ocean
## All at once version (takes ~30 min on effort dataset)
loctable$near_ocean <-
extract(r,spTransform(sps,latlong_proj),
buffer=8047,fun=function(x){1 %in% x},na.rm=F)
Sys.time()
#saveRDS(loctable,"loctable.rds")
loctable %>% nrow # 466,064 unique localities for effort
#readRDS("loctable.rds")
load_effort_stateprovcty_column_filtered() -> effort
## Add "near_ocean" field to main effort data frame
effort$near_ocean <-
left_join(effort,loctable,by="locality_id") %>% use_series(near_ocean)
effort_coastal <-
effort %>% filter(near_ocean==TRUE)
#saveRDS(effort_coastal,file="effort_coastal.rds")
#readRDS("eucd_coastal.rds") -> eucd_coastal
nrow(effort_coastal)
###### BEGIN PELAGIC EFFORT FILTERING ######
# Read land shapefile
land_shp <- readOGR("ne_10m_land",layer="ne_10m_land")
crs(land_shp)
extent(land_shp)
Sys.time()
## Make raster of NAs, with Land_shp projection
r <- raster(extent(sps),
nrows=(extent(sps)@ymax-extent(sps)@ymin)/2000,
ncols=(extent(sps)@xmax-extent(sps)@xmin)/2000,
crs=crs(sps),
vals=NA)
r <- projectRaster(from=r,crs=crs(land_shp))
Sys.time()
r <- rasterize(land_shp, r) #Takes a while
Sys.time()
## Efficiently extract whether point is >5 miles from land, Takes 25 minutes
loctable$pelagic <-
extract(r,spTransform(sps,latlong_proj),
buffer=8047,fun=function(x){all(is.na(x))},na.rm=F)
#rm(land_shp,sps)
#rm(r)
#rm(latlong_proj,utm10_proj)
Sys.time()
## Add "pelagic" field to effort data frame
load_effort_stateprovcty_column_filtered() -> effort
effort$pelagic <-
left_join(effort,loctable,by="locality_id") %>% use_series(pelagic)
#rm(loctable)
effort_pelagic <-
effort %>% filter(pelagic==TRUE)
#saveRDS(effort_pelagic,file="effort_pelagic.rds")