Skip to content

Commit

Permalink
feat: validating uniqueness of locationID
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathancallahan committed Nov 14, 2023
1 parent c0d5777 commit f21b66b
Show file tree
Hide file tree
Showing 67 changed files with 162 additions and 119 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Type: Package
Package: MazamaLocationUtils
Version: 0.4.1
Version: 0.4.2
Title: Manage Spatial Metadata for Known Locations
Authors@R: c(
person("Jonathan", "Callahan", email="[email protected]", role=c("aut","cre")),
Expand All @@ -27,7 +27,7 @@ Imports:
lubridate,
magrittr,
methods,
MazamaCoreUtils (>= 0.5.0),
MazamaCoreUtils (>= 0.5.1),
MazamaSpatialUtils (>= 0.8.6),
readr,
rlang,
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# MazamaLocationUtils 0.4.2

* Add uniqueness of `locationID` to `validateLocationTable()`.
* `table_save()` now supports a vector of `outputType` formats and will save
the location table in multiple formats.

# MazamaLocationUtils 0.4.1

* Documentation fixes for CRAN submission.
Expand Down
14 changes: 8 additions & 6 deletions R/location_createID.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ location_createID <- function(
) {

algorithm <- match.arg(algorithm)

# TODO: use precision here when MazamaCoreUtils provides it
returnVal <- MazamaCoreUtils::createLocationID(longitude, latitude, algorithm)

if ( algorithm == "geohash" ) {
returnVal <- returnVal %>% stringr::str_sub(1, precision)
}
returnVal <-
MazamaCoreUtils::createLocationID(
longitude = longitude,
latitude = latitude,
algorithm = algorithm,
precision = precision,
invalidID = as.character(NA)
)

return(returnVal)

Expand Down
10 changes: 8 additions & 2 deletions R/table_getNearestLocation.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ table_getNearestLocation <- function(

# ----- Subset ---------------------------------------------------------------

incomingIDTbl <- dplyr::tibble(
targetLocationIDsTbl <- dplyr::tibble(
locationID = table_getLocationID(locationTbl, longitude, latitude, distanceThreshold)
)

subsetTbl <- dplyr::left_join(incomingIDTbl, locationTbl, by = "locationID")
# NOTE: dplyr::left_join() guarantees *at least* one record per X depending on
# NOTE: how many matches are found in Y. Because validateLocationTbl() ensures
# NOTE: that locationID is never duplicated in a locationTbl, this ends up
# NOTE: handling the situation of multiple target locations that refer to the
# NOTE: same record in locationTbl.

subsetTbl <- dplyr::left_join(targetLocationIDsTbl, locationTbl, by = "locationID")

# ----- Return ---------------------------------------------------------------

Expand Down
78 changes: 44 additions & 34 deletions R/table_save.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

#' @title Save a known location table
#' @description Save a tibble of known locations to the preferred directory.
#' @description Save a tibble of known locations to the preferred directory. If
#' \code{outputType} is a vector, the known locations table will be saved to the
#' preferred directory in multiple formats.
#'
#' @param locationTbl Tibble of known locations.
#' @param collectionName Character identifier for this table.
#' @param backup Logical specifying whether to save a backup version of any
#' existing tables sharing \code{collectionName}.
#' @param outputType Output format. One of "rda" or "csv".
#' @param outputType Vecctor of output formats. (Currently only "rda" or "csv" are supported.)
#' @return File path of saved file.
#' @examples
#' library(MazamaLocationUtils)
Expand All @@ -20,7 +23,7 @@
#' # Save it as "table_save_example"
#' table_save(locationTbl, "table_save_example")
#'
#' # Add a column and save again
#' # Add a column and save again
#' locationTbl %>%
#' table_addColumn("my_column") %>%
#' table_save("table_save_example")
Expand All @@ -34,55 +37,62 @@
#' @importFrom MazamaCoreUtils stopIfNull
#' @importFrom lubridate now
table_save <- function(
locationTbl = NULL,
collectionName = NULL,
backup = TRUE,
outputType = c("rda", "csv")
locationTbl = NULL,
collectionName = NULL,
backup = TRUE,
outputType = "rda"
) {

# ----- Validate parameters --------------------------------------------------

MazamaLocationUtils::validateLocationTbl(locationTbl, locationOnly = FALSE)
MazamaCoreUtils::stopIfNull(collectionName)
outputType <- match.arg(outputType)

for ( type in outputType ) {
if ( !type %in% c("rda", "csv") )
stop("outputType '%s' is not supported.", type)
}

dataDir <- getLocationDataDir()

# ----- Save data ------------------------------------------------------------

result <- try({

fileName <- paste0(collectionName, ".", outputType)
filePath <- file.path(dataDir, fileName)

# Save backups
if ( backup && file.exists(filePath) ) {
backupName <- paste0(
collectionName, ".",
strftime(lubridate::now(), "%Y-%m-%dT%H:%M:%S"),
".rda"
)
backupPath <- file.path(dataDir, backupName)
file.rename(filePath, backupPath)
}
for ( type in outputType ) {

if ( outputType == "rda" ) {
result <- try({

# Assign a name and save
assign(collectionName, locationTbl)
save(list = c(collectionName), file = filePath)
fileName <- paste0(collectionName, ".", type)
filePath <- file.path(dataDir, fileName)

} else if ( outputType == "csv" ) {
# Save backups
if ( backup && file.exists(filePath) ) {
backupName <- paste0(
collectionName, ".",
strftime(lubridate::now(), "%Y-%m-%dT%H:%M:%S"),
".rda"
)
backupPath <- file.path(dataDir, backupName)
file.rename(filePath, backupPath)
}

readr::write_csv(locationTbl, file = filePath)

if ( type == "rda" ) {
# Assign a name and save
assign(collectionName, locationTbl)
save(list = c(collectionName), file = filePath)
}

if ( type == "csv" ) {
readr::write_csv(locationTbl, file = filePath)
}

}, silent = TRUE)

if ( "try-error" %in% class(result) ) {
warning(sprintf("Could not write %s", filePath))
}

}, silent = TRUE)
} # END of type loop

if ( "try-error" %in% class(result) ) {
stop(sprintf("Could not write %s", filePath))
}

# ----- Return ---------------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ validateLocationTbl <- function(
stop("'locationTbl$latitude' is not numeric. Please ensure that decimal latitudes are used.")

if ( !locationOnly ) {

missingNames <-
setdiff(MazamaLocationUtils::coreMetadataNames, names(locationTbl))

if ( length(missingNames) > 0 ) {
missingNamesString <- paste0(missingNames, collapse = ", ")
stop(sprintf("'locationTbl' is missing '%s'", missingNamesString))
}

if ( any(duplicated(locationTbl$locationID)) )
stop("'locationTbl$locationID' is not unique.")

}

return(invisible(TRUE))
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

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

2 changes: 1 addition & 1 deletion docs/articles/Developer_Style_Guide.html

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

8 changes: 4 additions & 4 deletions docs/articles/MazamaLocationUtils.html

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

2 changes: 1 addition & 1 deletion docs/articles/index.html

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

6 changes: 3 additions & 3 deletions docs/authors.html

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

2 changes: 1 addition & 1 deletion docs/index.html

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

10 changes: 8 additions & 2 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pkgdown_sha: ~
articles:
Developer_Style_Guide: Developer_Style_Guide.html
MazamaLocationUtils: MazamaLocationUtils.html
last_built: 2023-10-30T18:04Z
last_built: 2023-11-14T00:52Z

2 changes: 1 addition & 1 deletion docs/reference/LocationDataDir.html

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

2 changes: 1 addition & 1 deletion docs/reference/MazamaLocationUtils.html

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

2 changes: 1 addition & 1 deletion docs/reference/apiKeys.html

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

Binary file modified docs/reference/clusterByDistance-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/clusterByDistance.html

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

2 changes: 1 addition & 1 deletion docs/reference/coreMetadataNames.html

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

2 changes: 1 addition & 1 deletion docs/reference/getLocationDataDir.html

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

2 changes: 1 addition & 1 deletion docs/reference/id_monitors_500.html

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

Loading

0 comments on commit f21b66b

Please sign in to comment.