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

4 new function download media #6

Merged
merged 47 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
835e523
Copy download_media.R from fis-projecten
SanderDevisscher Apr 26, 2024
67366db
make a proper function
SanderDevisscher Apr 26, 2024
4bd9232
Update download_media.R
SanderDevisscher Apr 26, 2024
7b25947
Create download_media.Rd
SanderDevisscher Apr 26, 2024
b4257b4
fix small issue with help
SanderDevisscher Apr 26, 2024
ff34fc1
add imports
SanderDevisscher Apr 26, 2024
7d11298
Update download_media.Rd
SanderDevisscher Apr 26, 2024
63b0a17
Update NAMESPACE
SanderDevisscher Apr 26, 2024
9023da5
Update download_media.R
SanderDevisscher Apr 26, 2024
9b98724
Delete scafold.txt
SanderDevisscher Apr 26, 2024
91ea0ad
Create print_hello_world.R
SanderDevisscher Apr 26, 2024
613bf82
add magrittr
SanderDevisscher Apr 26, 2024
9d9ada0
delete src folder
SanderDevisscher Apr 26, 2024
f70233e
Update DESCRIPTION
SanderDevisscher Apr 26, 2024
0f4f42a
add don't run
SanderDevisscher Apr 26, 2024
1973253
Update download_media.R
SanderDevisscher Apr 26, 2024
37babd6
Delete scafold.txt
SanderDevisscher Apr 26, 2024
62415e9
clean up
SanderDevisscher Apr 26, 2024
9b62202
remove version limits
SanderDevisscher Apr 29, 2024
dff7329
remove camtraptor suggests
SanderDevisscher Apr 29, 2024
acb9588
Delete scafold.txt
SanderDevisscher Apr 29, 2024
33f887e
Merge branch 'main' into 4-new-function-download_media
SanderDevisscher May 2, 2024
f926d57
add @export
SanderDevisscher May 3, 2024
ab57978
Update NAMESPACE
SanderDevisscher May 3, 2024
3e2f7e4
Merge branch 'main' into 4-new-function-download_media
SanderDevisscher May 3, 2024
41b1f53
Aanzet tot functie
LynnPallemaerts May 8, 2024
fc5de6c
Add test zip to data_raw
LynnPallemaerts May 8, 2024
34f9eab
Revert "Add test zip to data_raw"
LynnPallemaerts May 8, 2024
d0e5c10
Revert "Aanzet tot functie"
LynnPallemaerts May 8, 2024
60ddf88
Merge pull request #20 from inbo/19-new-function-download_favorites
SanderDevisscher May 14, 2024
253b972
preprocess drg agouti export
LynnPallemaerts May 27, 2024
52ae09c
Create drg_example.Rda
LynnPallemaerts May 27, 2024
9712906
Roxygenise
LynnPallemaerts May 27, 2024
5a9f9ec
Compress rda file
LynnPallemaerts May 27, 2024
488b25b
set LazyDataCompression to true
LynnPallemaerts May 27, 2024
7a5ef3c
fix LazyDataCompression
LynnPallemaerts May 27, 2024
c413bb7
Update .Rbuildignore
LynnPallemaerts May 27, 2024
f1174b4
Update DESCRIPTION
LynnPallemaerts May 27, 2024
69d73f3
Rename and split download_media
LynnPallemaerts May 27, 2024
1624a69
rerun check
LynnPallemaerts May 27, 2024
0b4aa12
update author list
LynnPallemaerts May 27, 2024
1b78a54
Merge branch 'main' into 4-new-function-download_media
SanderDevisscher May 27, 2024
afa1069
add middle names
LynnPallemaerts May 27, 2024
c1972fa
checks
LynnPallemaerts May 27, 2024
c2c53fd
improve indentation
SanderDevisscher May 27, 2024
38d8ff2
remove lubridate from imports
SanderDevisscher May 27, 2024
fe1fb29
Rename drg_example.Rda to drg_example.rda
SanderDevisscher May 27, 2024
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
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
^\.Rproj\.user$
^LICENSE\.md$
^\.github$
^data_raw$
^preprocessing$
^CODE_OF_CONDUCT.md$
^codemeta.json$
21 changes: 16 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
Package: fistools
Title: Tools & data used for wildlife management & invasive species in Flanders
Version: 0.0.0.9000
Authors@R:
person("Sander", "Devisscher", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2015-5731"))
Version: 0.0.1
Authors@R: c(
person(given = "Sander", middle = "", family = "Devisscher", "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2015-5731")),
person(given = "Lynn", middle = "", family = "Pallemaerts", "[email protected]",
role = "aut", comment = c(ORCID = "0000-0002-5034-5416")),
person(given = "Soria", middle = "", family = "Delva", "[email protected]",
role = "aut", comment = c(ORCID = "0000-0002-7164-6031")),
person(given = "Emma", middle = "", family = "Cartuyvels", "[email protected]",
role = "aut", comment = c(ORCID = "0000-0001-7856-6360"))
)
Description: This package contains functions & data that are widely used within the wildlife management & invasive species research group (FIS) of the research institute forest and nature (INBO).
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
LazyDataCompression: xz
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
dplyr (>= 1.1.4),
googledrive (>= 2.1.1),
httr (>= 1.4.7),
magrittr (>= 2.0.3),
stringr (>= 1.5.1),
rlang (>= 1.1.3),
progress (>= 1.2.3),
googledrive (>= 2.1.1),
svDialogs (>= 1.1.0),
utils (>= 4.3.2)
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

export(check)
export(colcompare)
export(download_dep_media)
export(download_gdrive_if_missing)
export(download_seq_media)
importFrom(magrittr,"%>%")
126 changes: 126 additions & 0 deletions R/download_dep_media.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#' Download deployment media
#'
#' @author Lynn Pallemaerts
#' @author Emma Cartuyvels
#' @author Sander Devisscher
#' @author Soria Delva
#'
#' @description
#' This function allows the user to download all media related to a Agouti -
#' dataset which matches the given parameters.
#'
#' @param dataset character string, path to the folder where a camptraptor datapackage has been unzipped.
#' @param depID character string, ID of the deployment to download media from.
#' @param favorite boolean, do you only want the pretty pictures?
#' @param species character string, latin name of the species to download
#' @param outputfolder character string, path where the function should download the media into
#'
#' @details
#' If you are getting an Authorization Error (#403), this probably means your Agouti project has Restrict Images on. This needs to be turned off.
#' If depID = "all" and favorite = TRUE, the function will download all favorited pictures in the whole dataset.
#'
#' @returns Downloads the specified media files into the outputfolder
#'
#' @examples
#' \dontrun{
#' drg <- fistools::drg_example
#'
#' # Situation 1: download whole deployment
#' download_dep_media(dataset = drg,
#' depID = "96413aa6-5f1f-4dfb-8fab-8f06decc179f")
#'
#' # Situation 2: download only wanted species
#' download_dep_media(dataset = drg,
#' depID = "96413aa6-5f1f-4dfb-8fab-8f06decc179f",
#' species = "Dama dama")
#'
#' # Situation 3: download only favorited species media
#' download_dep_media(dataset = drg,
#' depID = "96413aa6-5f1f-4dfb-8fab-8f06decc179f",
#' species = "Dama dama",
#' favorite = TRUE)
#'
#' # Situation 4: download only favorited species media
#' download_dep_media(dataset = drg,
#' depID = "all",
#' favorite = TRUE)

#' }
#'
#' @export
#' @importFrom magrittr %>%

download_dep_media <- function(dataset,
depID,
species = NULL, # ONLY LATIN NAMES FOR NOW
favorite = FALSE,
outputfolder = NULL) {

if (length(depID) > 1) {
stop("function can only download one deployment at the time. if you want to download different deployments, use a for-loop")
}

if (is.null(outputfolder)) {
if (dir.exists("../../Mijn afbeeldingen")) {
output_folder <- paste0("../../Mijn afbeeldingen/", depID)
} else {
stop("output folder is not specified and default does not exist. please specify an output folder !!!")
}
} else {
output_folder <- paste0(outputfolder, "/", depID)
}
if (!dir.exists(output_folder)) {
dir.create(output_folder)
}

med <- dataset$data$media

if (depID == "all" & favorite == TRUE) {
med <- med %>%
dplyr::filter(favourite == TRUE)

depID <- unique(med$deploymentID)
}

if (!is.null(species)) {
obs <- dataset$data$observations %>%
dplyr::filter(scientificName == species)
med <- med %>%
dplyr::filter(sequenceID %in% obs$sequenceID)
}
if (favorite == TRUE) {
med <- med %>%
dplyr::filter(favourite == TRUE)
}

med <- med %>%
dplyr::filter(deploymentID %in% depID)

if (nrow(med) > 100 & nrow(med) < 1000) {
warning("you are trying to download between 100 and 1000 images. this may take a while")
}
if (nrow(med) > 1000) {
continue <- askYesNo("You are trying to download more than 1000 images. This might take a long time to complete. Are you sure you want to continue?")
if (continue == FALSE) {
stop()
}
}

print(paste0("you are downloading ", nrow(med), " images"))

pb <- progress::progress_bar$new(format = " [:bar] :percent ETA: :eta",
total = nrow(med),
clear = FALSE,
width = 60)

for (i in 1:nrow(med)) {
pb$tick()
filename <- paste0(stringr::str_sub(med$fileName[i], end = -5),
".jpeg")
httr::GET(url = med$filePath[i],
httr::write_disk(path = paste0(output_folder,
"/",
filename),
overwrite = TRUE))
}
}
98 changes: 98 additions & 0 deletions R/download_seq_media.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#' Download sequence media
#'
#' @author Lynn Pallemaerts
#' @author Emma Cartuyvels
#' @author Sander Devisscher
#' @author Soria Delva
#'
#' @description
#' This function allows the user to download all media related to a Agouti -
#' sequence which matches the given parameters.
#'
#' @param dataset character string, path to the folder where a camptraptor datapackage has been unzipped.
#' @param seqID character string, ID of the sequence to download media from
#' @param favorite boolean, do you only want the pretty pictures?
#' @param outputfolder character string, path where the function should download the media into
#'
#' @details
#' If you are getting an Authorization Error (#403), this probably means your Agouti project has Restrict Images on. This needs to be turned off.
#'
#' @returns Downloads the specified media files into the outputfolder
#'
#' @examples
#' \dontrun{
#' drg <- fistools::drg_example
#'
#' # Situation 1: download whole sequence
#' download_seq_media(dataset = drg,
#' seqID = "f4c049d2-d42f-4cd3-a951-fd485ed0279a")
#'
#' # Situation 2: download only favorited species media within sequence
#' download_seq_media(dataset = drg,
#' seqID = "f4c049d2-d42f-4cd3-a951-fd485ed0279a",
#' favorite = TRUE)
#' }
#'
#' @export
#' @importFrom magrittr %>%

download_seq_media <- function(dataset,
seqID,
favorite = FALSE,
outputfolder = NULL) {

if (length(seqID) > 1) {
stop("function can only download one sequence at the time. if you want to download different sequences, use a for-loop")
}

if (is.null(outputfolder)) {
if (dir.exists("../../Mijn afbeeldingen")) {
output_folder <- paste0("../../Mijn afbeeldingen/", seqID)
} else {
stop("output folder is not specified and default does not exist. please specify an output folder !!!")
}
} else {
output_folder <- paste0(outputfolder, "/", seqID)
}
if (!dir.exists(output_folder)) {
dir.create(output_folder)
}

med <- dataset$data$media

if (favorite == TRUE) {
med <- med %>%
dplyr::filter(favourite == TRUE)
}

med <- med %>%
dplyr::filter(sequenceID %in% seqID)

if (nrow(med) > 100 & nrow(med) < 1000) {
warning("you are trying to download between 100 and 1000 images. this may take a while")
}
if (nrow(med) > 1000) {
continue <- askYesNo("You are trying to download more than 1000 images. This might take a long time to complete. Are you sure you want to continue?")
if (continue == FALSE) {
stop()
}
}

print(paste0("you are downloading ", nrow(med), " images"))

pb <- progress::progress_bar$new(format = " [:bar] :percent ETA: :eta",
total = nrow(med),
clear = FALSE,
width = 60)

for (i in 1:nrow(med)) {
pb$tick()
filename <- paste0(stringr::str_sub(med$fileName[i], end = -5),
".jpeg")
httr::GET(url = med$filePath[i],
httr::write_disk(path = paste0(output_folder,
"/",
filename),
overwrite = TRUE))
}
}
12 changes: 12 additions & 0 deletions R/drg_example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#' drg_example
#'
#' Subset of Drongengoed Agouti export for testing of functions
#'
#' @format datapackage
#' \describe{
#' \item{deployments}{Lijst van geselecteerde deployments}
#' \item{observations}{Observaties van de geselecteerde deployments}
#' \item{media}{Lijst van media-urls van de geselecteerde deployments}
#' }
#' @source <https://www.agouti.eu>
"drg_example"
Binary file added data/drg_example.rda
Binary file not shown.
71 changes: 71 additions & 0 deletions man/download_dep_media.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/download_gdrive_if_missing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading