Skip to content

Commit 017667f

Browse files
committed
feat: modify exploration page 1
test merged geometries for case studies #67 display name of the geometry #153
1 parent 1bea84e commit 017667f

File tree

2 files changed

+9096
-1869
lines changed

2 files changed

+9096
-1869
lines changed

data-raw/aa-data-exploration-and-preparation.Rmd

+58-38
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ library(diades.atlas)
2626
library(dplyr)
2727
library(leaflet)
2828
library(ggplot2)
29+
library(sf)
2930
```
3031

3132
## Data exploration
@@ -34,23 +35,12 @@ library(ggplot2)
3435

3536
Do not forget to set environment variables in .Renviron
3637
```{r}
37-
postgis_host <- Sys.getenv("POSTGRES_HOST")
38-
postgis_user <- Sys.getenv("POSTGRES_USER")
39-
postgis_password <- Sys.getenv("POSTGRES_PASSWORD")
40-
41-
con <- DBI::dbConnect(
42-
RPostgres::Postgres(),
43-
host = Sys.getenv("POSTGRES_HOST", diades.atlas:::get_golem_config("POSTGRES_HOST")),
44-
dbname = Sys.getenv("POSTGRES_DBNAME", diades.atlas:::get_golem_config("POSTGRES_DBNAME")),
45-
port = Sys.getenv("POSTGRES_PORT", diades.atlas:::get_golem_config("POSTGRES_PORT")),
46-
user = Sys.getenv("POSTGRES_USER", "diadesatlas_r"),
47-
password = Sys.getenv("POSTGRES_PASS", "diadesPassword"),
48-
options = "-c search_path=diadesatlas"
49-
)
50-
# Listtables
51-
# DBI::dbListTables(con)
52-
# DBI::dbListObjects(con)
38+
session <- shiny::MockShinySession$new() #new.env()
39+
connect(session)
40+
con <- get_con(session)
41+
```
5342

43+
```{r, eval=FALSE}
5444
# Get the names of all the tables in the schema
5545
all_schemas <- DBI::dbGetQuery(con, "SELECT table_name FROM information_schema.tables WHERE table_schema='diadesatlas'")
5646
all_schemas
@@ -64,21 +54,13 @@ casestudy <- tbl(con, dbplyr::in_schema("diadesatlas", "casestudy"))
6454
casestudy
6555
```
6656

67-
### Create connection
68-
69-
```{r, eval=FALSE}
70-
my_dm <- dm_from_src(con, schema = "diadesatlas", learn_keys = TRUE)
71-
my_dm
72-
```
73-
7457
### Function to collect data
7558
```{r, eval=FALSE}
7659
# empty
7760
# dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "cices51")) %>% collect()
7861
# services ?
7962
# dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "cices")) %>% collect()
80-
# services new
81-
dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "es_diades")) %>% collect()
63+
8264
# geometry of all basin
8365
dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "basin_location")) %>% collect()
8466
# Model results over the years
@@ -105,24 +87,41 @@ dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "species")) %>% collect()
10587
### Prepare json files
10688
Create all json data used on the home page of the app
10789
```{r, eval=FALSE}
108-
# List of case studies
109-
casestudy <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy")) %>% collect()
110-
# List of the corresponding basins
111-
casestudy_basin <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy_basin")) %>% collect()
112-
# Put them two together
113-
cstd <- full_join(casestudy, casestudy_basin) %>% filter(publishable)
90+
# # List of case studies
91+
# casestudy <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy")) %>% collect()
92+
# # List of the corresponding basins
93+
# casestudy_basin <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy_basin")) %>% collect()
94+
# # Put them two together
95+
# cstd <- full_join(casestudy, casestudy_basin) %>% filter(publishable) %>% collect()
11496
115-
# Get the features for drawing the polygons on the map
116-
qry <- "SELECT ST_Transform(diadesatlas.basin_outlet.simplified_geom, 4326) as geom, basin_id as basin_id FROM diadesatlas.basin_outlet"
11797
118-
# i then read as an sf object
119-
pols <- sf::st_read(con, query = qry, geom = "geom") %>%
120-
filter(basin_id %in% casestudy_basin$basin_id) %>%
121-
left_join(cstd)
122-
pols %>%
98+
casestudy_new <- sf::st_read(con,
99+
query = "SELECT casestudy_id, casestudy_name, geom FROM diadesatlas.casestudy", geom = "geom") %>%
100+
# fake basin_id to be compatabible with mod_b_second.R
101+
mutate(basin_id = 1:n()) %>%
102+
st_transform(crs = 4326)
103+
104+
casestudy_new %>%
123105
geojson::as.geojson() %>%
124106
geojson::geo_write(here::here("inst/casestudy.json"))
125107
108+
109+
# Get the features for drawing the polygons on the map
110+
# qry <- "SELECT ST_Transform(diadesatlas.basin_outlet.simplified_geom, 4326) as geom, basin_id as basin_id FROM diadesatlas.basin_outlet"
111+
112+
# qry <- "SELECT diadesatlas.basin_outlet.simplified_geom as geom, basin_id as basin_id FROM diadesatlas.basin_outlet"
113+
#
114+
# pols <- sf::st_read(con, query = qry, geom = "geom") %>%
115+
# st_transform(crs = 4326)
116+
#
117+
# # i then read as an sf object
118+
# pols %>%
119+
# filter(basin_id %in% casestudy_basin$basin_id) %>%
120+
# left_join(cstd)
121+
# pols %>%
122+
# geojson::as.geojson() %>%
123+
# geojson::geo_write(here::here("inst/casestudy.json"))
124+
126125
# List of ecosystem services
127126
ecosystems <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "ecosystem_service")) %>% collect()
128127
ecosystems %>%
@@ -153,6 +152,27 @@ species %>%
153152
jsonlite::write_json(here::here("inst/species.json"))
154153
```
155154

155+
### Explore ecosystem services table
156+
```{r}
157+
r <- list(lg = 'en')
158+
datasets <- generate_datasets(con = con)
159+
.globals <- getFromNamespace('.globals', 'shiny')
160+
161+
# simulate a species_list
162+
session$options$golem_options$species_list <- datasets$species_list
163+
.globals$domain <- session
164+
165+
golem::get_golem_options("species_list")
166+
167+
# debugonce(ecosystem_table)
168+
ecosystem_table(species = "Alosa alosa",
169+
case_study = 5,
170+
ecosystem = '1_4',
171+
r = r,
172+
session = session) %>%
173+
DT::datatable()
174+
```
175+
156176

157177
### Stop connection
158178

0 commit comments

Comments
 (0)