-
Notifications
You must be signed in to change notification settings - Fork 1
/
2_assign_grid.Rmd
445 lines (342 loc) 路 10.9 KB
/
2_assign_grid.Rmd
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
---
title: "Assign grid to occurrences"
author:
- Damiano Oldoni
- Peter Desmet
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
number_sections: true
---
In this document we transform the text file containing the occurrences of (alien) species for Europe into a sqlite database. Then, we filter on issues and occurrence status. This is a way to handle the critical huge amount of occurrences. Note: some of these steps could take long.
# Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```
Load libraries:
```{r load_libraries_assign_grid_eu}
library(tidyverse) # To do datascience
library(here) # To find files
library(rgbif) # To use GBIF services
library(glue) # To write queries
library(RSQLite) # To interact with SQlite databases
library(sp) # To work with geospatial data
```
# Get occurrence data
## Get CSV file from GBIF
We download the occurrences from GBIF, based on the key got in `download.Rmd`:
```{r get_occ_file_eu}
key <- "0032154-190918142434337"
zip_filename <- paste0(key, ".zip")
if (!file.exists(here::here("data", "raw", zip_filename))) {
occ <- occ_download_get(
key = key,
path = here::here("data", "raw")
)
}
```
We unzip text file with occurrences as `key_number` + `occurrence.txt` in `./data/raw`:
```{r unzip_csv_occs_eu}
occ_file <- paste(key, "occurrence.txt", sep = "_")
occ_path <- here::here("data", "raw", occ_file)
if (!file.exists(here::here("data", "raw", occ_file))) {
unzip(zipfile = occ,
files = "occurrence.txt",
exdir = here::here("data", "raw"))
file.rename(from = here::here("data", "raw", "occurrence.txt"),
to = occ_path
)
}
```
Name of columns:
```{r get_cols_occsfile_eu}
cols_occ_file <- read_delim(
occ_path, "\t", n_max = 1,
quote = ""
)
cols_occ_file <- names(cols_occ_file)
```
Number of columns present:
```{r n_cols_occ_file}
length(cols_occ_file)
```
## Define columns to select
We define a subset of columns we are interested to:
```{r columns_to_use_eu}
cols_to_use <- c("gbifID", "scientificName", "kingdom", "phylum", "class", "order",
"family", "genus", "specificEpithet", "infraspecificEpithet",
"taxonRank", "taxonomicStatus", "datasetKey", "basisOfRecord",
"occurrenceStatus", "lastInterpreted", "hasCoordinate",
"hasGeospatialIssues", "decimalLatitude", "decimalLongitude",
"coordinateUncertaintyInMeters", "coordinatePrecision",
"pointRadiusSpatialFit", "verbatimCoordinateSystem", "verbatimSRS",
"eventDate", "startDayOfYear", "endDayOfYear", "year", "month",
"day", "verbatimEventDate", "samplingProtocol", "samplingEffort",
"issue", "taxonKey", "acceptedTaxonKey", "kingdomKey", "phylumKey",
"classKey", "orderKey", "familyKey", "genusKey", "subgenusKey",
"speciesKey", "species")
```
Columns in occurrence file not present in the subset:
```{r cols_in_cols_to_use_not_present_in_cols_occ_db_eu}
cols_to_use[which(!cols_to_use %in% cols_occ_file)]
```
will be removed from the selection:
```{r remove_cols_not_in_cols_occ_db_eu}
cols_to_use <- cols_to_use[which(cols_to_use %in% cols_occ_file)]
```
Final number of columns to select:
```{r n_cols_to_use_eu}
length(cols_to_use)
```
## Read occurrence data
### Define column type specifications
The following columns should contain integers:
1. `*Key`, e.g. `taxonKey`, `speciesKey`
2. `*DayOfYear`: `startDayOfYear` and `endDayOfYear`
3. `year`
4. `month`
5. `day`
```{r define_col_integer_for_specific_columns_eu}
int_colnames <-
cols_to_use[str_detect(cols_to_use, "Key") &
!str_detect(cols_to_use, "datasetKey")]
int_colnames <- c(
int_colnames,
cols_to_use[str_detect(cols_to_use, "DayOfYear")],
cols_to_use[cols_to_use == "year"],
cols_to_use[cols_to_use == "month"],
cols_to_use[cols_to_use == "day"]
)
int_cols <-
map(int_colnames, ~ col_integer()) %>%
setNames(int_colnames)
```
The following columns should contain real numbers:
1. `decimal*`: `decimalLatitude` and `decimalLongitude`
2. `coordinate*`: `coordinateUncertaintyInMeters` and `coordinatePrecision`
3. `pointRadiusSpatialFit`
```{r define_col_double_for_specific_columns_eu}
real_colnames <- cols_to_use[str_detect(cols_to_use, "decimal")]
real_colnames <- c(
real_colnames,
cols_to_use[str_detect(cols_to_use, "coordinate")],
cols_to_use[cols_to_use == "pointRadiusSpatialFit"]
)
real_cols <-
map(real_colnames, ~ col_double()) %>%
setNames(real_colnames)
```
The other columns to select contain text:
```{r define_col_text_for_other_columns_eu}
char_colnames <- cols_to_use[!cols_to_use %in% real_colnames &
!cols_to_use %in% int_colnames]
char_cols <-
map(char_colnames, ~ col_character()) %>%
setNames(char_colnames)
```
Final column specification:
```{r cols_type_to_use}
col_specs <- cols_only()
col_specs$cols <- c(char_cols, int_cols, real_cols)
col_specs
```
### Read data
Import occurrence data:
```{r read_occs_in_occ_eu}
occ_eu <- read_tsv(
here::here("data", "raw", paste0(key, "_occurrence.txt")),
na = "",
quote = "",
col_types = col_specs)
```
Number of occurrences
```{r n_occs_eu}
nrow(occ_eu)
```
Number of columns:
```{r n_cols_occ_eu}
ncol(occ_eu)
```
Preview:
```{r preview_occ_eu}
occ_eu %>% head()
```
# Filter data
## Filter on issues
Occurrences containing the following issues should be filtered out:
```{r issues_to_discard_occs_eu}
issues_to_discard <- c(
"ZERO_COORDINATE",
"COORDINATE_OUT_OF_RANGE",
"COORDINATE_INVALID"
)
names(issues_to_discard) <- issues_to_discard
issues_to_discard
```
Issues present in `occ_eu`:
```{r issues_in_data_occs_eu}
issues <-
occ_eu %>%
distinct(issue) %>%
separate(issue, into = "issues", sep = ";") %>%
distinct() %>%
arrange()
issues
```
Are there some `issues_to_discard` in `issues`?
```{r any_issue_to_discard_in_data_occs_eu}
any(issues_to_discard %in% issues$issues)
```
If yes, remove occurrences containing `issues_to_discard`:
```{r remove_occs_issues_to_discard_occs_eu}
if (any(issues_to_discard %in% issues$issues)) {
occ_eu <-
map_dfc(issues_to_discard,
function(x) {
str_detect(occ_eu$issue, x)
}) %>%
mutate_all(funs(replace(., is.na(.), FALSE))) %>%
bind_cols(occ_eu) %>%
filter_at(issues_to_discard,
all_vars(. == FALSE)) %>%
select(-one_of(issues_to_discard))
}
```
## Filter on occurrence status
Occurrences with the following occurrence status should be filtered out:
```{r occurrenceStatus_to_discard_occs_eu}
occurrenceStatus_to_discard <- c(
"absent",
"excluded"
)
```
Occurrence status present in `occ_eu`:
```{r occ_status_in_data_occs_eu}
occurrenceStatus <-
occ_eu %>%
distinct(occurrenceStatus) %>%
distinct()
occurrenceStatus
```
Are there some `occurrenceStatus_to_discard` in `occurrenceStatus`?
```{r any_occurrenceStatus_to_discard_in_data_occs_eu}
any(occurrenceStatus_to_discard %in% occurrenceStatus$occurrenceStatus)
```
If yes, remove occurrences with `occurrenceStatus` equal to one of `occurrenceStatus_to_discard`:
```{r remove_occurrenceStatus_to_discard_occs_eu}
if (any(
occurrenceStatus_to_discard %in% occurrenceStatus$occurrenceStatus)) {
occ_eu <-
occ_eu %>%
filter(!occurrenceStatus %in% occurrenceStatus_to_discard)
}
```
## Overview and control filtered data table
Number of occurrences left:
```{r nrow_occ_eu_after_filter}
nrow(occ_eu)
```
Preview:
```{r preview_occ_eu_after_filtering}
occ_eu %>% head()
```
# Assign grid
We assign grid to occurrences.
## Get geographic coordinates and coordinate uncertainty
Number of occurrences per each value of `coordinateUncertaintyInMeters`:
```{r n_occ_per_uncertainty_occs_eu}
occ_eu %>%
group_by(coordinateUncertaintyInMeters) %>%
count() %>%
arrange(desc(n))
```
We assign 1000 meters to occurrences without uncertainty:
```{r assign_fix_uncertainty_occs_eu}
occ_eu <-
occ_eu %>%
mutate(
coordinateUncertaintyInMeters =
if_else(is.na(coordinateUncertaintyInMeters),
1000.0,
coordinateUncertaintyInMeters)
)
```
We save geographic coordinates, `decimalLatitude` and `decimalLongitude` and coordinate uncertainty, `coordinateUncertaintyInMeters` as a new data.frame, `geodata_df`:
```{r geodata_df_occs_eu}
geodata_df <-
occ_eu %>%
select(decimalLatitude,
decimalLongitude,
coordinateUncertaintyInMeters)
nrow_geodata_df <- nrow(geodata_df)
```
## Project geographic coordinates
We project latitude and longitude by using the projection of the grid. We transform GBIF data which have coordinate reference system equal to EPSG code 4326 to Lambert projection with EPSG code 3035:
```{r transform_to_3035_occs_eu}
coordinates(geodata_df) <- ~decimalLongitude+decimalLatitude
proj4string(geodata_df) <- CRS("+init=epsg:4326")
geodata_df <- spTransform(geodata_df, CRS("+init=epsg:3035"))
colnames(geodata_df@coords) <- c("x", "y")
```
## Assign occurrence within uncertainty circle
Assign the occurrence randomly within the circle with radius equal to `coordinateUncertaintyInMeters`:
```{r assign_pts_in_circle_occs_eu}
geodata_df@data <-
geodata_df@data %>%
mutate(random_angle = runif(nrow_geodata_df, 0, 2*pi))
geodata_df@data <-
geodata_df@data %>%
mutate(random_r = sqrt(runif(
nrow_geodata_df, 0, 1)) * coordinateUncertaintyInMeters)
geodata_df@data <-
geodata_df@data %>%
mutate(x = geodata_df@coords[, "x"],
y = geodata_df@coords[, "y"])
geodata_df@data <-
geodata_df@data %>%
mutate(x = x + random_r * cos(random_angle),
y = y + random_r * sin(random_angle))
geodata_df@data <-
geodata_df@data %>%
select(-c(random_angle, random_r)) %>%
select(x, y, coordinateUncertaintyInMeters)
```
Preview:
```{r preview_geodata_df_data_occs_eu}
geodata_df@data %>% head(n = 10)
```
Where `x` and `y` are the new coordinates while in `@coords` we keep track of the original coordinates:
```{r preview_geodata_df_coords_occs_eu}
geodata_df@coords[1:10,]
```
## Assign occurrences to grid cells
We assign each occurrence to a grid cell.
```{r assign_grid_occs_eu}
geodata_df@data <-
geodata_df@data %>%
mutate(eea_cell_code = paste0("1km",
"E", floor(x/1000),
"N", floor(y/1000)))
```
Preview:
```{r preview_gridcell_occs_eu}
geodata_df@data %>% head(n = 10)
```
We can now add the column `eea_cell_code` to `occ_eu`:
```{r add_eaa_cell_code_to_occ_eu}
occ_eu$eea_cell_code <- geodata_df@data$eea_cell_code
```
Preview:
```{r preview_with_eaa_cell_code_occs_eu}
occ_eu %>% head()
```
# Save data
Save occurrences with added EAA cell code in `/data/interim`:
```{r save_occ_eu_after_filtering}
write_tsv(occ_eu,
here::here("data", "interim", paste0(key, "_occurrence.tsv")),
na = "")
```