-
Notifications
You must be signed in to change notification settings - Fork 2
/
3_assign_grid.Rmd
311 lines (249 loc) · 7.98 KB
/
3_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
---
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 assign grid to occurrences.
# Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```
Load libraries:
```{r load_libraries}
library(tidyverse) # To do datascience
library(sp) # To work with geospatial data
library(here) # To find files
library(glue) # To write queries
library(RSQLite) # To interact with SQlite databases
```
# Get geographic coordinates and coordinate uncertainty
Name and path of `.sqlite` file:
```{r name_path}
key <- "0031758-231002084531237"
sqlite_file <- paste(key, "occurrence.sqlite", sep = "_")
sqlite_path <- here::here("data", "interim", sqlite_file)
```
Table name:
```{r define_table_name}
table_name <- "occ"
```
Open connection to database:
```{r open_connection_to_db}
sqlite_occ <- dbConnect(SQLite(), dbname = sqlite_path)
```
Retrieve geographic coordinates, `decimalLatitude` and `decimalLongitude` and coordinate uncertainty, `coordinateUncertaintyInMeters`:
```{r get_geodata}
query <- glue_sql("SELECT {`cols`*} FROM {table}",
cols = c("decimalLatitude",
"decimalLongitude",
"coordinateUncertaintyInMeters"),
table = table_name,
.con = sqlite_occ
)
geodata_df <-
dbGetQuery(sqlite_occ, query) %>%
dplyr::as_tibble() %>%
dplyr::mutate(coordinateUncertaintyInMeters = as.double(.data$coordinateUncertaintyInMeters))
```
Number of occurrences:
```{r n_occs}
nrow_geodata_df <- nrow(geodata_df)
nrow_geodata_df
```
Preview:
```{r preview_geodata}
geodata_df %>% head(10)
```
Number of occurrences per each value of `coordinateUncertaintyInMeters`:
```{r n_occ_per_uncertainty}
geodata_df %>%
group_by(coordinateUncertaintyInMeters) %>%
count() %>%
arrange(desc(n))
```
Number of occurrences without coordinate uncertainty or set to zero:
```{r n_occs_without_uncertainty}
geodata_df %>%
filter(is.na(coordinateUncertaintyInMeters) |
coordinateUncertaintyInMeters == 0) %>%
count()
```
We assign 1000 meters to these occurrences:
```{r assign_fix_uncertainty}
geodata_df <-
geodata_df %>%
mutate(coordinateUncertaintyInMeters = if_else(
is.na(coordinateUncertaintyInMeters) | coordinateUncertaintyInMeters == 0,
1000.0,
coordinateUncertaintyInMeters
))
```
We apply the same changes in SQLite table. To speed up the process we deactivate database synchronization and wrap the execution of the query in a transaction:
```{r assign_1000_as_uncertainty_on_db}
# speed up by deactivating db synchronization
query <- glue_sql(
"PRAGMA synchronous = OFF",
.con = sqlite_occ
)
dbExecute(sqlite_occ, query)
# Start transaction
dbBegin(sqlite_occ)
query <- glue_sql(
"UPDATE {table} SET {`column`} = 1000 WHERE
{`column`} = '' OR {`column`} = 0",
table = table_name,
column = "coordinateUncertaintyInMeters",
.con = sqlite_occ)
dbExecute(sqlite_occ, query)
# Commit transaction
dbCommit(sqlite_occ)
```
Get all possible values of coordinate uncertainty from SQLite database:
```{r get_unique_values_uncertainty}
query <- glue_sql("SELECT DISTINCT {`column`} FROM {table}",
table = table_name,
column = "coordinateUncertaintyInMeters",
.con = sqlite_occ
)
uncertainty_df <-
dbGetQuery(sqlite_occ, query) %>%
as_tibble()
uncertainty_df
```
Are there occurrences with 0 uncertainty left?
```{r zero_in_uncertainty_df}
0 %in% uncertainty_df
```
Are there occurrences without uncertainty?
```{r zero_in_uncertainty_df}
NA %in% uncertainty_df
```
# Assign grid to occurrences
We use the official grid of Belgium at 1x1km resolution as provided by EEA.
## Project geographic coordinates, assign occurrence within uncertainty circle and assign grid cell
In this section we do the following:
1. 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.
2. Assign the occurrences randomly within the circle with radius equal to `coordinateUncertaintyInMeters`
3. Define the grid cell the occurrence belongs to.
Due to memory issues, we perform this three operations in chunks on a temporary tsv file containing coordinates and coordinate uncertainties.
First, we create such file:
```{r create_temp_file}
temp_file_coords <- here::here(
"data",
"interim",
paste0(key, "_coordinates_and_uncertainty_epsg_4326.tsv")
)
col_names_geodata_df <- names(geodata_df)
write_tsv(geodata_df, temp_file_coords, na = "")
remove(geodata_df)
```
Set random number generator seed (this helps reproducibility). We use the unique identifier of the reserved [Zenodo dataset's DOI](https://doi.org/10.5281/zenodo.10058400) which the occurrence cube will be published to:
```{r set_seed}
set.seed(10058400)
```
We define the function to apply to each chunk:
```{r transform_to_3035_assign_pts_in_circle}
reproject_assign <- function(df, pos){
# Step 1: reprojection
nrow_df <- nrow(df)
coordinates(df) <- ~decimalLongitude+decimalLatitude
proj4string(df) <- CRS("+init=epsg:4326")
df <- spTransform(df, CRS("+init=epsg:3035"))
colnames(df@coords) <- c("x", "y")
# Step 2: assign occurrence within uncertainty circle
df@data <-
df@data %>%
mutate(random_angle = runif(nrow_df, 0, 2*pi))
df@data <-
df@data %>%
mutate(random_r = sqrt(runif(
nrow_df, 0, 1)) * coordinateUncertaintyInMeters)
df@data <-
df@data %>%
mutate(x = df@coords[, "x"],
y = df@coords[, "y"])
df@data <-
df@data %>%
mutate(x = x + random_r * cos(random_angle),
y = y + random_r * sin(random_angle)) %>%
select(-c(random_angle, random_r))
# Step 3: Find grid cell the occurrence belongs to
df@data <-
df@data %>%
mutate(eea_cell_code = paste0(
"1km",
"E", floor(x/1000),
"N", floor(y/1000))) %>%
select(x, y, coordinateUncertaintyInMeters, eea_cell_code)
return(df@data)
}
```
And finally we apply reprojection to all occurrences:
```{r reproject_assign_coords_by_chunk}
chunk_size <- 1000000
geodata_df <- read_tsv_chunked(
file = temp_file_coords,
callback = DataFrameCallback$new(reproject_assign),
chunk_size = chunk_size,
col_types = cols(.default = col_double()),
na = ""
)
```
Preview:
```{r preview_geodata_df_data}
geodata_df %>% head(n = 10)
```
Before proceeding, we can delete the temporary file created at the beginning of this section:
```{r delete_temp_file}
file.remove(temp_file_coords)
```
## Add grid cell code to sqlite file
We can now add the column `eea_cell_code` to the table `occ` of sqlite file. We first create the new column `eaa_cell_code` in the table:
```{r add_eaa_cellcode_to_sqlite}
new_col <- "eea_cell_code"
query <- glue_sql("ALTER TABLE {table} ADD COLUMN {colname} {type}",
table = table_name,
colname = new_col,
type = "CHARACTER",
.con = sqlite_occ
)
dbExecute(sqlite_occ, query)
```
And then we populate it with the values in `geodata_df$eaa_cell_code`. This step can take long:
```{r add_values_cellcode}
# Start transaction
dbBegin(sqlite_occ)
dbExecute(
sqlite_occ,
glue_sql(
"UPDATE {table} SET {`column`} = :eea_cell_code WHERE _ROWID_ = :id",
table = table_name,
column = new_col,
.con = sqlite_occ),
params = data.frame(
eea_cell_code = geodata_df$eea_cell_code,
id = rownames(geodata_df))
)
# Commit transaction
dbCommit(sqlite_occ)
```
Preview:
```{r preview}
query <- glue_sql("SELECT * FROM {table} WHERE _ROWID_ <= 10",
table = table_name,
.con = sqlite_occ)
dbGetQuery(sqlite_occ, query) %>%
select(eea_cell_code , everything())
```
Close connection:
```{r close_connection}
dbDisconnect(sqlite_occ)
```