-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgt_measures_ltree.Rmd
247 lines (192 loc) · 6.43 KB
/
mgt_measures_ltree.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
---
title: "Management Measures using ltree"
output:
html_document:
toc: true
# number_sections: true
editor_options:
chunk_output_type: console
---
```{r setup, include=F}
if (!require(librarian)){
remotes::install_github("DesiQuintans/librarian")
library(librarian)
}
shelf(
# database
DBI, RPostgres,
# tidyverse
dplyr, purrr, readr, stringr, tibble, tidyr,
# report
DT, gt, htmltools, htmlwidgets, kableExtra, knitr, markdown, rmarkdown,
# utility
fs, glue, here, png, scales, stringr, urltools, rlist
)
here <- here::here
# CAREFUL - THIS SCRIPT WILL DROP AND RECREATE TABLES
not_linux <- Sys.info()[["sysname"]] != "Linux"
if (not_linux){
# presumably Caleb's machine
db_params <- list(
dbname = "dev",
host = "localhost",
user = "cgrant",
pwd_txt = "../../pwd.txt")
} else {
# presumably server
db_params <- list(
dbname = "gis",
host = "postgis",
user = "admin",
pwd_txt = "/share/.password_mhk-env.us")
}
# Ben's laptop
if (Sys.info()[["user"]] == "bbest" & Sys.info()[["user"]] == "Darwin"){
db_params <- list(
dbname = "gis",
host = "marineenergy.app",
user = "admin",
pwd_txt = "~/private/dbpass_marineenergy.app.txt")
}
db = dbConnect(
RPostgres::Postgres(),
dbname = db_params$dbname,
host = db_params$host,
port = 5432,
user = db_params$user,
password = readLines(db_params$pwd_txt))
con <- db
knitr::opts_chunk$set(echo = T, warning = F, message = F, connection = "db")
```
## redo
```{r}
d_mgt <- read_csv("data/tethys_mgt.csv")
dbWriteTable(con, "tethys_mgt", d_mgt)
```
```{r}
d_mgt_tags <- d_mgt %>%
select(
Technology, Stressor,
Management = `Management Measure Category`,
Phase = `Phase of Project`) %>%
pivot_longer(everything(), names_to="category", values_to="tag_mgt") %>%
group_by(category, tag_mgt) %>%
summarise(.groups="drop") %>%
bind_rows(
d_mgt %>%
select(
Receptor, ReceptorSpecifics = `Specific Receptor`) %>%
group_by(Receptor, ReceptorSpecifics) %>%
summarise(.groups="drop") %>%
mutate(
category = "Receptor") %>%
select(category, tag_mgt = Receptor, ReceptorSpecifics)) %>%
arrange(category, tag_mgt, ReceptorSpecifics)
write_csv(d_mgt_tags, "data/tethys_mgt_tags.csv")
View(d_mgt_tags)
```
```{r}
tbl(con, "tethys_mgt") %>%
arrange(id)
dbSendStatement(con, "ALTER TABLE tethys_mgt ADD COLUMN tags LTREE[];")
dbSendStatement(con, "ALTER TABLE tethys_mgt DROP COLUMN tags;")
dbSendStatement(con, '
UPDATE tethys_mgt
SET tags = \'{"Technology.Tidal", "Management.Mitigation", "Receptor.Habitat"}\'
WHERE id = 1;')
dbSendStatement(con, '
UPDATE tethys_mgt
SET tags = \'{"Technology.Tidal", "Management.DesignFeature", "Receptor.Benthic"}\'
WHERE id = 2;')
dbGetQuery(con, "SELECT * from tethys_mgt WHERE 'Technology.Tidal' = ANY(tags);")
dbGetQuery(con, "SELECT * from tethys_mgt WHERE tags @> {\"Technology.Tidal\", \"Receptor.Habitat\"};")
x <- tbl(con, "tethys_mgt") %>%
filter(id==1) %>%
collect()
as.character(x$tags)
tbl(con, "mgt")
DBI::dbListTables(con) %>% sort()
```
# Caleb's original...
## Initialize ltree extension
```{r initialize_ltree}
dbExecute(db, glue("create extension if not exists ltree"))
```
## Notes
[PostgreSQL ltree documentation](https://www.postgresql.org/docs/current/ltree.html)
Each element in the ltree path is called a _label_.
> A label is a sequence of alphanumeric characters and underscores (for example, in C locale the characters A-Za-z0-9_ are allowed). Labels must be less than 256 characters long.
Examples: `42`, `Personal_Services`
> A _label path_ is a sequence of zero or more labels separated by dots, for example L1.L2.L3, representing a path from the root of a hierarchical tree to a particular node. The length of a label path cannot exceed 65535 labels.
Example: `Top.Countries.Europe.Russia`
---
## Caleb's hack way of ingesting a CSV and doing string replacement
Cant use the following characters for ltree data type:
- " "
- "-"
- "&"
- ":"
```{r}
# List of source CSVs that contain management measure categories
data <- list(
"receptor" = "https://raw.githubusercontent.com/marineenergy/apps/master/data/ferc_lookup_receptor.csv",
"stressor" = "https://raw.githubusercontent.com/marineenergy/apps/master/data/ferc_lookup_stressor.csv",
"technology" = "https://raw.githubusercontent.com/marineenergy/apps/master/data/ferc_lookup_technology.csv")
# ,"phase" = "https://raw.githubusercontent.com/marineenergy/apps/master/data/ferc_tags_phase_lut.csv"
d <- tibble(
category = names(data),
csv = unlist(data)) %>%
mutate(
data = purrr::map(
csv,
function(x){
read_csv(x, col_types = cols()) %>%
select(tag0 = 1)})) %>%
unnest(data) %>%
mutate(
tag = glue("{str_to_title(category)}.{tag0}") %>%
str_replace_all("[^A-Za-z0-9_.]", ""),
label = tag0 %>%
str_replace(".*\\.(.+)$", "\\1"),
level = tag %>%
str_count("\\.")) %>%
select(
tag, label, level)
# x_cat = category, x_tag = tag0, x_csv = csv)
# Create database table
dbExecute(db, glue("DROP TABLE IF EXISTS tags CASCADE"))
dbExecute(db, glue("CREATE TABLE tags (tag LTREE PRIMARY KEY, label VARCHAR, level INT4)"))
dbWriteTable(db, "tags", d, overwrite = F, append = T, row.names = F)
tbl(db, "tags") %>%
collect() %>%
datatable()
```
## Query ltree
`@ Match case-insensitively, for example a@ matches A`
`* Match any label with this prefix, for example foo* matches foobar`
`% Match initial underscore-separated words`
### Inheritance
#### Example 1
```{r ltree_inheritance1}
dbGetQuery(db, glue("SELECT * FROM tags WHERE tag ~ '*.Birds.*'")) %>%
# dbGetQuery(db, glue("SELECT * FROM tags WHERE tag @ '*Birds*'")) %>%
datatable()
```
#### Example 2
```{r ltree_inheritance2}
dbGetQuery(db, glue("SELECT * FROM tags WHERE tag <@ 'Receptor.Birds'")) %>%
datatable()
```
#### Example 3
```{r ltree_inheritance3}
dbGetQuery(db, glue("SELECT * FROM tags WHERE tag @ 'GroundNestingBirds'")) %>%
datatable()
```
#### Example 4
```{r ltree_inheritance4}
dbGetQuery(db, glue("SELECT * FROM tags WHERE tag = 'Receptor.Birds'")) %>%
datatable()
```
## Conclusion
Unfortunately, we cant query ltree without doing string replacement of the selected management measure.
For example, if we are looking for receptor `Ground-Nesting Birds`, we will need to do something like this: `... where receptor @ Ground_Nesting_Birds`