Skip to content

Commit

Permalink
functions to query PEP pg database closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlondon committed Jul 26, 2024
1 parent e21a011 commit c74b5d9
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions R/get_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
library(DBI)
library(dbplyr)
library(dplyr)
library(RPostgres)
library(sf)
library(arrow)
library(geoarrow)

get_deployments <- function() {
tryCatch(
{
con <- dbConnect(
RPostgres::Postgres(),
dbname = "pep",
host = Sys.getenv("PEP_PG_IP"),
user = keyringr::get_kc_account("pgpep_londonj"),
password = keyringr::decrypt_kc_pw("pgpep_londonj")
)
},
error = function(cond) {
print("Unable to connect to Database.")
}
)

tbl_speno <- dplyr::tbl(con, in_schema("capture", "for_telem"))

tbl_deploy <- dplyr::tbl(con, in_schema("telem", "tbl_tag_deployments")) |>
dplyr::filter(species %in% c("Pv")) |>
dplyr::mutate(species = case_when(
species == "Pv" ~ "Harbor seal",
.default = NA
)) |>
dplyr::left_join(tbl_speno, by = c("speno", "species")) |>
dplyr::collect()

dbDisconnect(con)

return(tbl_deploy)
}


get_locations <- function(tbl_deploy = NULL) {
tryCatch(
{
con <- dbConnect(
RPostgres::Postgres(),
dbname = "pep",
host = Sys.getenv("PEP_PG_IP"),
user = keyringr::get_kc_account("pgpep_londonj"),
password = keyringr::decrypt_kc_pw("pgpep_londonj")
)
},
error = function(cond) {
print("Unable to connect to Database.")
}
)

locs_obs <- sf::st_read(con, Id("telem", "geo_wc_locs_qa")) |>
dplyr::collect() |>
dplyr::left_join(tbl_deploy) |>
dplyr::select(
speno, deployid, ptt, instr, tag_family, type, quality, locs_dt, latitude, longitude,
error_radius, error_semi_major_axis, error_semi_minor_axis,
error_ellipse_orientation, project, species, age, sex, qa_status, deploy_dt, end_dt,
deploy_lat, deploy_long, capture_lat, capture_long, geom
) |>
dplyr::filter(species == "Harbor seal")

dbDisconnect(con)

return(locs_obs)
}


get_timelines <- function(tbl_deploy = NULL) {
tryCatch(
{
con <- dbConnect(
RPostgres::Postgres(),
dbname = "pep",
host = Sys.getenv("PEP_PG_IP"),
user = keyringr::get_kc_account("pgpep_londonj"),
password = keyringr::decrypt_kc_pw("pgpep_londonj")
)
},
error = function(cond) {
print("Unable to connect to Database.")
}
)

tbl_timelines <- dplyr::tbl(con, in_schema("telem", "tbl_wc_histos_timeline_qa")) |>
dplyr::collect() |>
dplyr::left_join(tbl_deploy, by = c("deployid")) |>
dplyr::filter(species %in% c("Pv")) |>
dplyr::mutate(species = case_when(
species == "Pv" ~ "Harbor seal",
.default = NA
))

dbDisconnect(con)

return(tbl_timelines)
}

0 comments on commit c74b5d9

Please sign in to comment.