Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified geometries #156

Merged
merged 16 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ data-raw/translation.Rmd
dev/translation.html
dev/data-docker/
bdd_connect.sh
/data-raw/*.html
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Imports:
pkgload (>= 1.2.2),
promises,
purrr (>= 0.3.4),
rmapshaper (>= 0.4.5),
RPostgres (>= 1.3.3),
sf (>= 1.0.2),
shinipsum (>= 0.1.0),
Expand Down
2 changes: 1 addition & 1 deletion R/app_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ app_ui <- function(request) {
welcomemodal = modal(
inputId = "welcome",
title = tagList(
tags$h2("Welcome on DiadES Atlas!") %>% with_i18("welcome")
tags$h2("Welcome to DiadES Atlas!") %>% with_i18("welcome")
),
body = tagList(
tags$div(
Expand Down
5 changes: 3 additions & 2 deletions R/mod_a_first_fct_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ tm_ocean <- function(dataOcean,
"[7, 9]",
"[10, 12]",
"[13, 15]"
)
),
popup.vars = c("prevalence" = "nb_occurence")
)
}

Expand Down Expand Up @@ -152,7 +153,7 @@ tm_draw <- function(species_latin_name,
# ------------------------------------------ display the map
tm_graticules() +
tm_ocean +
tm_frontiers +
# tm_frontiers +
tm_catchmment +
tm_layout(
main.title.fontface = 3,
Expand Down
9 changes: 5 additions & 4 deletions R/utils_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ generate_datasets <- function(con) {
catchment_geom <- sf::st_read(
con,
query = "SELECT * FROM diadesatlas.v_basin vb"
) %>%
rmapshaper::ms_simplify()
) #%>%
#mutate(geom = st_make_valid(geom))
# rmapshaper::ms_simplify(keep = .05)

dataALL <- DBI::dbGetQuery(
con,
Expand All @@ -223,8 +224,8 @@ generate_datasets <- function(con) {
query = "SELECT * FROM diadesatlas.v_ices_geom;"
) %>%
# sf::st_transform("+proj=eqearth +wktext") %>%
sf::st_transform("+proj=wintri") %>%
rmapshaper::ms_simplify()
sf::st_transform("+proj=wintri") # %>%
# rmapshaper::ms_simplify()

species_order <- c(
"Alosa alosa",
Expand Down
2 changes: 2 additions & 0 deletions data-raw/aa-a-exploration_data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

TUTORIAL: The aims of this vignette is to learn how to request tables with dplyr package.

## Installation

Install the present package and use its core functions
Expand Down
161 changes: 61 additions & 100 deletions data-raw/aa-data-exploration-and-preparation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,16 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

This vignette explores the tables in the database and ensures that R has access to the tables.
It also transforms the data in json.

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = TRUE # Set to TRUE when data OK for test, but not on GitHub
)
# To compile manually (run in the console directly)
if (FALSE) {
vignette_name <- "aa-data-exploration-and-preparation"
vignette_file <- paste0(vignette_name, ".Rmd")

rmarkdown::render(
input = here::here(file.path("data-raw", vignette_file)),
output_format = "rmarkdown::html_vignette",
output_options = list(toc = TRUE),
output_file = here::here(file.path("vignettes", vignette_file))
)

# Add header for title
lines <- readLines(here::here(file.path("vignettes", vignette_file)))

cat(
glue::glue('---
title: ".{vignette_name}."
output: rmarkdown::html_vignette
vignette: >
%\\VignetteIndexEntry{.{vignette_name}.}
%\\VignetteEngine{knitr::rmarkdown}
%\\VignetteEncoding{UTF-8}
---
', .open = ".{", .close = "}."),
lines,
sep = "\n",
file = here::here(file.path("vignettes", vignette_file))
)
}

```

```{r setup}
Expand All @@ -52,6 +26,7 @@ library(diades.atlas)
library(dplyr)
library(leaflet)
library(ggplot2)
library(sf)
```

## Data exploration
Expand All @@ -60,23 +35,12 @@ library(ggplot2)

Do not forget to set environment variables in .Renviron
```{r}
postgis_host <- Sys.getenv("POSTGRES_HOST")
postgis_user <- Sys.getenv("POSTGRES_USER")
postgis_password <- Sys.getenv("POSTGRES_PASSWORD")

con <- DBI::dbConnect(
RPostgres::Postgres(),
host = Sys.getenv("POSTGRES_HOST", diades.atlas:::get_golem_config("POSTGRES_HOST")),
dbname = Sys.getenv("POSTGRES_DBNAME", diades.atlas:::get_golem_config("POSTGRES_DBNAME")),
port = Sys.getenv("POSTGRES_PORT", diades.atlas:::get_golem_config("POSTGRES_PORT")),
user = Sys.getenv("POSTGRES_USER", "diadesatlas_r"),
password = Sys.getenv("POSTGRES_PASS", "diadesPassword"),
options = "-c search_path=diadesatlas"
)
# Listtables
# DBI::dbListTables(con)
# DBI::dbListObjects(con)
session <- shiny::MockShinySession$new() #new.env()
connect(session)
con <- get_con(session)
```

```{r, eval=FALSE}
# Get the names of all the tables in the schema
all_schemas <- DBI::dbGetQuery(con, "SELECT table_name FROM information_schema.tables WHERE table_schema='diadesatlas'")
all_schemas
Expand All @@ -90,23 +54,15 @@ casestudy <- tbl(con, dbplyr::in_schema("diadesatlas", "casestudy"))
casestudy
```

### Create connection

```{r, eval=FALSE}
my_dm <- dm_from_src(con, schema = "diadesatlas", learn_keys = TRUE)
my_dm
```

### Function to collect data
```{r, eval=FALSE}
# empty
# dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "cices51")) %>% collect()
# services ?
# dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "cices")) %>% collect()
# services new
dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "es_diades")) %>% collect()

# geometry of all basin
dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "basin_outlet")) %>% collect()
dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "basin_location")) %>% collect()
# Model results over the years
dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "hybrid_model_result")) %>% collect()
# Abundance of species in a given basin ove the years
Expand All @@ -131,24 +87,41 @@ dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "species")) %>% collect()
### Prepare json files
Create all json data used on the home page of the app
```{r, eval=FALSE}
# List of case studies
casestudy <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy")) %>% collect()
# List of the corresponding basins
casestudy_basin <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy_basin")) %>% collect()
# Put them two together
cstd <- full_join(casestudy, casestudy_basin) %>% filter(publishable)
# # List of case studies
# casestudy <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy")) %>% collect()
# # List of the corresponding basins
# casestudy_basin <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "casestudy_basin")) %>% collect()
# # Put them two together
# cstd <- full_join(casestudy, casestudy_basin) %>% filter(publishable) %>% collect()

# Get the features for drawing the polygons on the map
qry <- "SELECT ST_Transform(diadesatlas.basin_outlet.simplified_geom, 4326) as geom, basin_id as basin_id FROM diadesatlas.basin_outlet"

# i then read as an sf object
pols <- sf::st_read(con, query = qry, geom = "geom") %>%
filter(basin_id %in% casestudy_basin$basin_id) %>%
left_join(cstd)
pols %>%
casestudy_new <- sf::st_read(con,
query = "SELECT casestudy_id, casestudy_name, geom FROM diadesatlas.casestudy", geom = "geom") %>%
# fake basin_id to be compatabible with mod_b_second.R
mutate(basin_id = 1:n()) %>%
st_transform(crs = 4326)

casestudy_new %>%
geojson::as.geojson() %>%
geojson::geo_write(here::here("inst/casestudy.json"))


# Get the features for drawing the polygons on the map
# qry <- "SELECT ST_Transform(diadesatlas.basin_outlet.simplified_geom, 4326) as geom, basin_id as basin_id FROM diadesatlas.basin_outlet"

# qry <- "SELECT diadesatlas.basin_outlet.simplified_geom as geom, basin_id as basin_id FROM diadesatlas.basin_outlet"
#
# pols <- sf::st_read(con, query = qry, geom = "geom") %>%
# st_transform(crs = 4326)
#
# # i then read as an sf object
# pols %>%
# filter(basin_id %in% casestudy_basin$basin_id) %>%
# left_join(cstd)
# pols %>%
# geojson::as.geojson() %>%
# geojson::geo_write(here::here("inst/casestudy.json"))

# List of ecosystem services
ecosystems <- dplyr::tbl(con, dbplyr::in_schema("diadesatlas", "ecosystem_service")) %>% collect()
ecosystems %>%
Expand Down Expand Up @@ -179,37 +152,25 @@ species %>%
jsonlite::write_json(here::here("inst/species.json"))
```

### Prepare and test leaflet map
```{r, eval=FALSE}
# set query and reproject to EPSG:4326 for GeoJSON creation
# (the real query is more involved with a bounding box etc)
qry <- "SELECT ST_Transform(diadesatlas.basin_outlet.simplified_geom, 4326) as geom FROM diadesatlas.basin_outlet"

# i then read as an sf object
pols <- sf::st_read(con, query = qry, geom = "geom")

# and convert to GeoJSON
pols.js <- geojsonio::geojson_json(pols)
# pols %>% geojson::as.geojson() %>% geojson::geo_write("plop.json")

# options(readr.default_locale=readr::locale(tz="Europe/Berlin"))
# df60 <- get_eurostat_geospatial(resolution = 60)
#
# CE.sf <- df60 %>%
# dplyr::filter(LEVL_CODE == 2 & CNTR_CODE %in% c("AT","CZ","DE","HU","PL","SK")) %>%
# dplyr::select(NUTS_ID)

plot(pols)

pols %>%
ggplot() +
geom_sf(color = "black", size = 0.4)

# CE = sf::as_Spatial(pols)

leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = pols, color = "green")
### Explore ecosystem services table
```{r}
r <- list(lg = 'en')
datasets <- generate_datasets(con = con)
.globals <- getFromNamespace('.globals', 'shiny')

# simulate a species_list
session$options$golem_options$species_list <- datasets$species_list
.globals$domain <- session

golem::get_golem_options("species_list")

# debugonce(ecosystem_table)
ecosystem_table(species = "Alosa alosa",
case_study = 5,
ecosystem = '1_4',
r = r,
session = session) %>%
DT::datatable()
```


Expand Down
Loading