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

Update functions florabank #61

Merged
merged 36 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a77135d
Change source database
fredericpiesschaert Feb 10, 2025
9e83979
replace sql-script for 1*1
fredericpiesschaert Feb 11, 2025
0b47c1e
replace sql-script for 4*4 and synchronize group by
fredericpiesschaert Feb 13, 2025
2be3b2f
adapt function description
fredericpiesschaert Feb 13, 2025
718dbfa
set assert_that to D0152_00_Flora
fredericpiesschaert Feb 13, 2025
ad8712a
replace cte by subquery to avoid issues with glue_sql
fredericpiesschaert Feb 13, 2025
c0aaccf
Checklist issues
fredericpiesschaert Feb 13, 2025
c1aeb18
roxygen2 updates
fredericpiesschaert Feb 13, 2025
2cff90a
replace sql-string
fredericpiesschaert Feb 14, 2025
4140412
update function description
fredericpiesschaert Feb 14, 2025
98ea741
add closing bracket in sql
fredericpiesschaert Feb 14, 2025
3a70f77
wrong table reference in sql
fredericpiesschaert Feb 14, 2025
91a641d
update roxygen2
fredericpiesschaert Feb 14, 2025
7a6f369
update citation
fredericpiesschaert Feb 14, 2025
d48433c
insert new sql-script
fredericpiesschaert Feb 17, 2025
7b9da5a
update function description and documentation
fredericpiesschaert Feb 17, 2025
2928c48
Roxygen update
fredericpiesschaert Feb 17, 2025
d66f41e
update NAMESPACE
fredericpiesschaert Feb 17, 2025
7251706
wrong table reference in sql-string
fredericpiesschaert Feb 18, 2025
c0221a9
wrong fieldname in sql-string
fredericpiesschaert Feb 18, 2025
a721c81
add dependency in example
ElsLommelen Feb 18, 2025
b68d7b3
add release notes
fredericpiesschaert Feb 18, 2025
597de5d
update version number
fredericpiesschaert Feb 18, 2025
5665885
Merge pull request #62 from inbo/fix-example-error
fredericpiesschaert Feb 18, 2025
5d919c3
update version
fredericpiesschaert Feb 18, 2025
fbdb1d3
update version
fredericpiesschaert Feb 18, 2025
dffbac5
update version
fredericpiesschaert Feb 18, 2025
bfd6451
update rda-files
fredericpiesschaert Feb 18, 2025
418a800
add userFlora to dictionary
ElsLommelen Feb 18, 2025
e586ace
update url
ElsLommelen Feb 18, 2025
603967f
replace outdated url
ElsLommelen Feb 18, 2025
1485c5a
update url (vervang door doi wegens niet toegankelijk)
ElsLommelen Feb 18, 2025
89124e7
respect naming convention
fredericpiesschaert Feb 19, 2025
f66083f
respect naming convention
fredericpiesschaert Feb 19, 2025
1b463b8
drop concatenation from 4x4 output
fredericpiesschaert Feb 19, 2025
bd3d0bd
roxygen2
fredericpiesschaert Feb 19, 2025
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
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "inbodb: Connect to and Retrieve Data from Databases on the INBO Server",
"version": "0.0.6",
"version": "0.0.7",
"license": "GPL-3.0",
"upload_type": "software",
"description": "<p>A bundle of functions to connect to and retrieve data from databases on the INBO server, with dedicated functions to query some of these databases.<\/p>",
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ abstract: "A bundle of functions to connect to and retrieve data from databases
identifiers:
- type: url
value: https://inbo.github.io/inbodb/
version: 0.0.6
version: 0.0.7
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: inbodb
Title: Connect to and Retrieve Data from Databases on the INBO Server
Version: 0.0.6
Version: 0.0.7
Authors@R: c(
person("Els", "Lommelen", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-3481-5684", affiliation = "Research Institute for Nature and Forest (INBO)")),
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ importFrom(dplyr,collect)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# inbodb 0.0.7

* update functions `get_florabank_observations()`,
`get_florabank_taxon_ifbl_year()` and
`get_florabank_traits()` (database 'D0021_00_userFlora' is deprecated and
replaced with 'D0152_00_Flora')

# inbodb 0.0.6

* new functions `get_taxonlijsten_lists()`, `get_taxonlijsten_features()` and
Expand Down
2 changes: 1 addition & 1 deletion R/connect_inbo_dbase.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' The function can only be used from within the INBO network.
#'
#' For more information, refer to
#' \href{https://inbo.github.io/tutorials/tutorials/r_database_access/}{this
#' \href{https://tutorials.inbo.be/tutorials/r_database_access/}{this
#' tutorial}.
#'
#' @param database_name char Name of the INBO database you want to connect
Expand Down
105 changes: 65 additions & 40 deletions R/get_florabank_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@
#' @return A dataframe with the following variables:
#' `NaamNederlands`,
#' `NaamWetenschappelijk`,
#' `AcceptedNaamWetenschappelijk`,
#' `Bron`,
#' `BeginDatum`,
#' `EindDatum`,
#' `hok`,
#' `Hok`,
#' `Toponiem`,
#' `CommentaarTaxon`,
#' `CommentaarHabitat`,
#' `WaarnemingID`,
#' `CommentaarEvent`,
#' `CommentaarWaarneming`,
#' `EventID`,
#' `X_event`,
#' `Y_event`,
#' `X_waarneming`,
#' `Y_waarneming`,
#' `X_meting`,
#' `Y_meting`
#' `Y_waarneming`
#'
#' @importFrom glue glue_sql
#' @importFrom assertthat assert_that
Expand All @@ -55,7 +56,7 @@
#' # code can only be run if a connection to the database is possible
#' library(inbodb)
#' # connect to florabank
#' db_connectie <- connect_inbo_dbase("D0021_00_userFlora")
#' db_connectie <- connect_inbo_dbase("D0152_00_Flora")
#'
#' # query and collect the data using scientific name
#' succprat1 <- get_florabank_observations(db_connectie,
Expand Down Expand Up @@ -100,67 +101,91 @@
#' }

get_florabank_observations <- function(connection, names, fixed = FALSE,
collect = FALSE) {
collect = FALSE) {

assert_that(inherits(connection, what = "Microsoft SQL Server"),
msg = "Not a connection object to database.")
assert_that(connection@info$dbname == "D0021_00_userFlora")
assert_that(connection@info$dbname == "D0152_00_Flora")

Check warning on line 108 in R/get_florabank_observations.R

View check run for this annotation

Codecov / codecov/patch

R/get_florabank_observations.R#L108

Added line #L108 was not covered by tests

if (missing(names)) {
stop("Please provide names.")
}

sql_statement <- "SELECT DISTINCT
tblTaxon.NaamNederlands
, tblTaxon.NaamWetenschappelijk
, cdeBron.Omschrijving AS Bron
, tblWaarneming.BeginDatum
, tblWaarneming.EindDatum
, tblIFBLHok.Code AS hok
, tblWaarneming.Opmerking AS Toponiem
, tblMeting.CommentaarTaxon
, tblMeting.CommentaarHabitat
, tblWaarneming.ID AS WaarnemingID
, tblWaarneming.Cor_X AS X_waarneming
, tblWaarneming.Cor_Y AS Y_waarneming
, tblMeting.Cor_X AS X_meting
, tblMeting.Cor_Y AS Y_meting
FROM dbo.tblWaarneming
INNER JOIN dbo.tblMeting ON tblWaarneming.ID = tblMeting.WaarnemingID
INNER JOIN dbo.relTaxonTaxon ON relTaxonTaxon.TaxonIDChild = tblMeting.TaxonID
INNER JOIN dbo.tblTaxon ON tblTaxon.ID = relTaxonTaxon.TaxonIDParent
LEFT JOIN dbo.tblIFBLHok ON tblIFBLHok.ID = tblWaarneming.IFBLHokID
INNER JOIN dbo.cdeBron ON cdeBron.Code = tblWaarneming.BronCode
WHERE 1=1
AND (tblMeting.MetingStatusCode='GDGA' OR tblMeting.MetingStatusCode='GDGK')
sql_statement <- "SELECT DISTINCT COALESCE(cte.ParentNaamNederlands
, cte.NaamNederlands) as NaamNederlands
, cte.NaamWetenschappelijk
, cte.ParentNaamWetenschappelijk AS AcceptedNaamWetenschappelijk
, b.Beschrijving AS Bron
, e.BeginDatum
, e.EindDatum
, h.Code AS Hok
, e.Toponiem
, e.Opmerking AS CommentaarEvent
, w.Commentaar AS CommentaarWaarneming
, e.id AS EventID
, e.CorX AS X_event
, e.CorY AS Y_event
, w.CorX AS X_waarneming
, w.CorY AS Y_waarneming
FROM [event] e
INNER JOIN Bron b ON b.ID = e.BronID
INNER JOIN Waarneming w ON w.EventID = e.ID
INNER JOIN waarnemingstatus ws ON ws.id = w.WaarnemingStatusID
INNER JOIN Hok h ON h.ID = e.HokID
INNER JOIN (SELECT t.id AS taxonid
, t.code AS taxoncode
, t.NaamNederlands
, t.NaamWetenschappelijk
, CASE WHEN t.ParentTaxonID IS NULL OR t.TaxonRelatieTypeID = 1
THEN t.id ELSE t.ParentTaxonID END AS ParentTaxonID
, CASE WHEN t.ParentTaxonID IS NULL OR t.TaxonRelatieTypeID = 1
THEN t.code ELSE tp.code END AS ParentTaxoncode
, CASE WHEN t.ParentTaxonID IS NULL OR t.TaxonRelatieTypeID = 1
THEN t.NaamNederlands ELSE tp.NaamNederlands
END AS ParentNaamNederlands
, CASE WHEN t.ParentTaxonID IS NULL OR t.TaxonRelatieTypeID = 1
THEN t.NaamWetenschappelijk ELSE tp.NaamWetenschappelijk
END AS ParentNaamWetenschappelijk
FROM Taxon t
LEFT JOIN Taxon tp ON tp.id = t.ParentTaxonID)cte
ON cte.taxonid = w.TaxonID
WHERE 1=1
AND ws.Code in ('GDGA','GDGK')

Check warning on line 153 in R/get_florabank_observations.R

View check run for this annotation

Codecov / codecov/patch

R/get_florabank_observations.R#L114-L153

Added lines #L114 - L153 were not covered by tests
"

if (!fixed) {
like_string <-
paste0("AND (",
paste0("AND cte.ParentTaxonID in
(SELECT DISTINCT CASE WHEN t.ParentTaxonID IS NULL
OR t.TaxonRelatieTypeID = 1 THEN t.id ELSE t.ParentTaxonID
END AS ParentTaxonID
FROM Taxon t
LEFT JOIN Taxon tp ON tp.id = t.ParentTaxonID
WHERE 1=1
AND (",

Check warning on line 165 in R/get_florabank_observations.R

View check run for this annotation

Codecov / codecov/patch

R/get_florabank_observations.R#L158-L165

Added lines #L158 - L165 were not covered by tests
paste0(
c(paste0("tblTaxon.NaamNederlands", " LIKE ", "'%", names, "%'"),
paste0("tblTaxon.NaamWetenschappelijk", " LIKE ", "'%", names,
c(paste0("cte.NaamNederlands", " LIKE ", "'%", names, "%'"),
paste0("cte.NaamWetenschappelijk", " LIKE ", "'%", names,

Check warning on line 168 in R/get_florabank_observations.R

View check run for this annotation

Codecov / codecov/patch

R/get_florabank_observations.R#L167-L168

Added lines #L167 - L168 were not covered by tests
"%'")),
collapse = " OR "),
")")
"))")
sql_statement <- glue_sql(
sql_statement,
like_string,
.con = connection)
} else {
sql_statement <- glue_sql(
sql_statement,
"AND (tblTaxon.NaamWetenschappelijk IN ({names*}) OR
tblTaxon.NaamNederlands IN ({names*}))
"AND (cte.NaamWetenschappelijk IN ({names*}) OR
cte.NaamNederlands IN ({names*}))

Check warning on line 180 in R/get_florabank_observations.R

View check run for this annotation

Codecov / codecov/patch

R/get_florabank_observations.R#L179-L180

Added lines #L179 - L180 were not covered by tests
",
names = names,
.con = connection)
}

sql_statement <- glue_sql(
sql_statement,
"ORDER BY tblWaarneming.BeginDatum DESC OFFSET 0 ROWS",
"ORDER BY e.BeginDatum DESC OFFSET 0 ROWS",

Check warning on line 188 in R/get_florabank_observations.R

View check run for this annotation

Codecov / codecov/patch

R/get_florabank_observations.R#L188

Added line #L188 was not covered by tests
.con = connection)

sql_statement <- iconv(sql_statement, from = "UTF-8", to = "latin1")
Expand Down
Loading
Loading