Skip to content

Commit

Permalink
feat: updated to latest geodist, MazamaSpatialUtils
Browse files Browse the repository at this point in the history
This revision also fixes issues with not being able to run
R CMD check --run-donttest by having mazama_initialize() install
the required datasets.
  • Loading branch information
jonathancallahan committed Jan 20, 2021
1 parent 9a1ff73 commit 15e8a6d
Show file tree
Hide file tree
Showing 77 changed files with 2,036 additions and 1,603 deletions.
12 changes: 5 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
Type: Package
Package: MazamaLocationUtils
Version: 0.1.11
Version: 0.1.12
Title: Manage Spatial Metadata for Known Locations
Authors@R: c(
person("Jonathan", "Callahan", email="[email protected]", role=c("aut","cre")),
person("Eli", "Grosman", email="[email protected]", role=c("aut"))
)
Author: Jonathan Callahan [aut, cre],
Eli Grosman [aut],
Oliver Fogelin [aut]
Eli Grosman [aut]
Maintainer: Jonathan Callahan <[email protected]>
Description: A suite of utility functions for discovering and managing metadata
associated with sets of spatially unique "known locations".
License: GPL-3
URL: https://github.com/MazamaScience/MazamaLocationUtils
BugReports: https://github.com/MazamaScience/MazamaLocationUtils/issues
Depends:
R (>= 3.1.0)
R (>= 3.5)
Imports:
digest,
dplyr,
geodist,
geodist (>= 0.0.6.007),
httr,
jsonlite,
lubridate,
methods,
magrittr,
MazamaCoreUtils,
MazamaSpatialUtils,
MazamaSpatialUtils (>= 0.7),
readr,
revgeo,
rlang,
stringr
Suggests:
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# MazamaLocationUtils 0.1.12

* Updated `location_getSingleAddress_Photon()` to remove **revgeo** dependency.
* Updated requirements to **geodist** 0.0.6.007 to handle errors finding longitude
and latitude columns in the passed in tibble.
* `geodist::geodist()` is now always called with `measure = "geodesic"` to avoid
warning messages from `geodist()` about inaccuracies with `measure = "cheap"
(the default).
* `mazama_initialize()` now installs required datasets if they are missing.

# MazamaLocationUtils 0.1.11

* Added unit test for `table_findOverlappingLocations()`.
Expand Down
8 changes: 4 additions & 4 deletions R/MazamaLocationUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ coreMetadataNames <- c(
"stateCode", # from MazamaSpatialUtils::getStateCode()
"county", # from MazamaSpatialUtils::getUSCounty()
"timezone", # from MazamaSpatialUtils::getTimezone()
"houseNumber", # from http://photon.komoot.de/
"street", # from http://photon.komoot.de/
"city", # from http://photon.komoot.de/
"zip" # from http://photon.komoot.de/
"houseNumber", # from http://photon.komoot.io/
"street", # from http://photon.komoot.io/
"city", # from http://photon.komoot.io/
"zip" # from http://photon.komoot.io/
)

# ----- Internal Package State -------------------------------------------------
Expand Down
33 changes: 20 additions & 13 deletions R/location_getSingleAddress_Photon.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@

#' @rdname location_getSingleAddress_Photon
#' @export
#' @importFrom utils capture.output
#' @importFrom jsonlite fromJSON
#' @importFrom stringr str_detect str_sub str_subset
#'
#' @title Get address data from the Photon API to OpenStreetMap
#'
#' @param longitude Single longitude in decimal degrees E, Default: NULL
#' @param latitude Single latitude in decimal degrees N, Default: NULL
#' @param baseUrl Base URL for data queries.
#' @param verbose Logical controlling the generation of progress messages.
#'
#' @return List of address components.
#'
#' @description The Photon API is used get address data associated with
#' the \code{longitude} and \code{latitude}. The following list of data
#' is returned:
#'
#' \itemize{
#' \item{\code{houseNumber}}
#' \item{\code{street}}
Expand All @@ -13,16 +27,13 @@
#' \item{\code{countryCode}}
#' \item{\code{countryName}}
#' }
#'
#' The function makes an effort to convert both \code{state} and \code{country}
#' \code{Name} into \code{Code} with codes defaulting to \code{NA}. Both
#' \code{Name} and \code{Code} are returned so that improvements can be made in
#' the conversion algorithm.
#' @param longitude Single longitude in decimal degrees E, Default: NULL
#' @param latitude Single latitude in decimal degrees N, Default: NULL
#' @param baseUrl Base URL for data queries.
#' @param verbose Logical controlling the generation of progress messages.
#' @return List of address components.
#' @examples
#'
#' @examples
#' \donttest{
#' library(MazamaLocationUtils)
#'
Expand All @@ -36,12 +47,8 @@
#' addressList <- location_getSingleAddress_Photon(lon, lat)
#' str(addressList)
#' }
#' @references \url{http://photon.komoot.de}
#' @rdname location_getSingleAddress_Photon
#' @export
#' @importFrom utils capture.output
#' @importFrom jsonlite fromJSON
#' @importFrom stringr str_detect str_sub str_subset
#'
#' @references \url{http://photon.komoot.io}
#'
location_getSingleAddress_Photon <- function(
longitude = NULL,
Expand Down
19 changes: 12 additions & 7 deletions R/location_getSingleAddress_TexasAM.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@

#' @rdname location_getSingleAddress_TexasAM
#' @export
#'
#' @title Get an address from a Texas A&M web service
#' @description Texas A&M APIs are used to determine the address associated with
#' the \code{longitude} and \code{latitude}.
#'
#' @param longitude Single longitude in decimal degrees E, Default: NULL
#' @param latitude Single latitude in decimal degrees N, Default: NULL
#' @param verbose Logical controlling the generation of progress messages.
#' @param apiKey Texas A&M Geocoding requires an API key. The first 2500 requests
#' are free. Default: NULL
#'
#' @return Numeric elevation value.
#' @examples
#' \donttest{
#'
#' @description Texas A&M APIs are used to determine the address associated with
#' the \code{longitude} and \code{latitude}.
#'
#' @examples
#' \dontrun{
#' library(MazamaLocationUtils)
#'
#' # Set up standard directories and spatial data
Expand All @@ -23,9 +29,8 @@
#'
#' location_getSingleAddress_TexasAM(longitude, latitude, apiKey)
#' }
#'
#' @references \url{https://geoservices.tamu.edu/Services/ReverseGeocoding/WebService/v04_01/HTTP.aspx}
#' @rdname location_getSingleAddress_TexasAM
#' @export
#'
location_getSingleAddress_TexasAM <- function(
longitude = NULL,
Expand Down
17 changes: 11 additions & 6 deletions R/location_getSingleElevation_USGS.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@

#' @rdname location_getSingleElevation_USGS
#' @export
#'
#' @title Get elevation data from a USGS web service
#' @description USGS APIs are used to determine the elevation associated with
#' the \code{longitude} and \code{latitude}.
#'
#' @param longitude Single longitude in decimal degrees E, Default: NULL
#' @param latitude Single latitude in decimal degrees N, Default: NULL
#' @param verbose Logical controlling the generation of progress messages.
#'
#' @return Numeric elevation value.
#' @examples
#'
#' @description USGS APIs are used to determine the elevation associated with
#' the \code{longitude} and \code{latitude}.
#'
#' @examples
#' \donttest{
#' library(MazamaLocationUtils)
#'
Expand All @@ -15,9 +21,8 @@
#' lat <- 47.423333
#' location_getSingleElevation_USGS(lon, lat)
#' }
#'
#' @references \url{https://nationalmap.gov/epqs/}
#' @rdname location_getSingleElevation_USGS
#' @export
#'
location_getSingleElevation_USGS <- function(
longitude = NULL,
Expand Down
38 changes: 19 additions & 19 deletions R/location_initialize.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@

#' @rdname location_initialize
#' @export
#' @importFrom MazamaCoreUtils stopIfNull
#' @importFrom MazamaSpatialUtils getCountryCode getStateCode getTimezone
#' @importFrom dplyr tibble
#' @importFrom stringr str_sub
#'
#' @title Create known location record with core metadata
#'
#' @param longitude Single longitude in decimal degrees E, Default: NULL
#' @param latitude Single latitude in decimal degrees N, Default: NULL
#' @param stateDataset Name of spatial dataset to use for determining state
#' @param elevationService Name of the elevation service to use for determining
#' the elevation. Default: NULL. Accepted values: "usgs".
#' @param addressService Name of the address service to use for determining
#' the street address. Default: NULL Accepted values: "photon".
#' @param verbose Logical controlling the generation of progress messages.
#'
#' @return Tibble with a single new known location.
#'
#' @description Creates a known location record with the following columns
#' of core metadata:
#' \itemize{
Expand All @@ -19,19 +36,6 @@
#' \item{zip}
#' }
#'
#' @param longitude Single longitude in decimal degrees E, Default: NULL
#' @param latitude Single latitude in decimal degrees N, Default: NULL
#' @param stateDataset Name of spatial dataset to use for determining state
#' @param elevationService Name of the elevation service to use for determining
#' the elevation. Default: NULL. Accepted values: "usgs".
#' @param addressService Name of the address service to use for determining
#' the street address. Default: NULL Accepted values: "photon".
#' @param verbose Logical controlling the generation of progress messages.
#'
#' @return Tibble with a single new known location.
#'
#' @rdname location_initialize
#'
#' @examples
#' \donttest{
#' library(MazamaLocationUtils)
Expand All @@ -45,11 +49,7 @@
#' lat <- 47.423333
#' locationRecord <- location_initialize(lon, lat)
#' }
#' @export
#' @importFrom MazamaCoreUtils stopIfNull
#' @importFrom MazamaSpatialUtils getCountryCode getStateCode getTimezone
#' @importFrom dplyr tibble
#' @importFrom stringr str_sub

location_initialize <- function(
longitude = NULL,
latitude = NULL,
Expand Down
29 changes: 18 additions & 11 deletions R/mazama_initialize.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@

#' @rdname mazama_initialize
#'
#' @export
#' @importFrom MazamaSpatialUtils setSpatialDataDir loadSpatialData
#'
#' @title Initialize with MazamaScience standard directories
#'
#' @param spatialDataDir Directory where spatial datasets are found,
#' Default: "~/Data/Spatial"
#'
#' @return No return value.
#'
#' @description Convenience function to initialize spatial data. Wraps the
#' following setup lines:
#'
Expand All @@ -11,10 +21,7 @@
#' MazamaSpatialUtils::loadSpatialData("NaturalEarthAdm1")
#' MazamaSpatialUtils::loadSpatialData("USCensusCounties")
#' }
#' @param spatialDataDir Directory where spatial datasets are found,
#' Default: "~/Data/Spatial"
#' @return No return value.
#' @rdname mazama_initialize
#'
#' @examples
#' \donttest{
#' library(MazamaLocationUtils)
Expand All @@ -23,17 +30,11 @@
#' spatialDataDir <- tempdir() # typically "~/Data/Spatial"
#' MazamaSpatialUtils::setSpatialDataDir(spatialDataDir)
#'
#' # Install core spatial datasets (168 MB download)
#' MazamaSpatialUtils::installSpatialData()
#'
#' exists("NaturalEarthAdm1")
#' mazama_initialize(spatialDataDir)
#' exists("NaturalEarthAdm1")
#' class(NaturalEarthAdm1)
#' }
#' @export
#' @importFrom MazamaSpatialUtils setSpatialDataDir loadSpatialData
#'
mazama_initialize <- function(
spatialDataDir = "~/Data/Spatial"
) {
Expand All @@ -48,6 +49,12 @@ mazama_initialize <- function(
# Not initialized, so try to initialize
result <- try({
MazamaSpatialUtils::setSpatialDataDir(spatialDataDir)
# Install if not found
MazamaSpatialUtils::installSpatialData("EEZCountries")
MazamaSpatialUtils::installSpatialData("OSMTimezones")
MazamaSpatialUtils::installSpatialData("NaturalEarthAdm1")
MazamaSpatialUtils::installSpatialData("USCensusCounties")
# Now load
MazamaSpatialUtils::loadSpatialData("EEZCountries")
MazamaSpatialUtils::loadSpatialData("OSMTimezones")
MazamaSpatialUtils::loadSpatialData("NaturalEarthAdm1")
Expand Down
2 changes: 2 additions & 0 deletions R/table_addLocation.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#' locationTbl <-
#' locationTbl %>%
#' table_addLocation(lon, lat, radius = 500)
#'
#' dplyr::glimpse(locationTbl)
#' }
#' @seealso \link{table_addSingleLocation}
#' @seealso \link{table_removeRecord}
Expand Down
2 changes: 1 addition & 1 deletion R/table_addSingleLocation.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#'
#' # Set up standard directories and spatial data
#' spatialDataDir <- tempdir() # typically "~/Data/Spatial"
#' MazamaSpatialUtils::setSpatialDataDir(spatialDataDir)
#' mazama_initialize(spatialDataDir)
#'
#' locationTbl <- get(data("wa_monitors_500"))
#'
Expand Down
2 changes: 1 addition & 1 deletion R/table_findOverlappingLocations.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ table_findOverlappingLocations <- function(
# ----- Check for locations that are too close -------------------------------

# Calculate distances between each location
distances <- geodist::geodist(tbl)
distances <- geodist::geodist(tbl, measure = "geodesic")

# Get distances that are less than the given diameter
# NOTE: the distance between a location and itself is always zero
Expand Down
21 changes: 12 additions & 9 deletions R/table_getNearestLocation.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@

#' @rdname table_getNearestLocation
#' @export
#' @importFrom MazamaCoreUtils stopIfNull
#'
#' @title Return known locations
#' @description Returns a tibble of known locations from \code{locationTbl}, one
#' for each incoming location. If no known location is found for a particular
#' incoming location, that record in the tibble will contain all \code{NA}.
#'
#' @param locationTbl Tibble of known locations, Default: NULL
#' @param longitude Vector of longitudes in decimal degrees E, Default: NULL
#' @param latitude Vector of latitudes in decimal degrees N, Default: NULL
#' @param radius Radius in meters, Default: NULL
#'
#' @return Tibble of known locations.
#'
#' @description Returns a tibble of known locations from \code{locationTbl}, one
#' for each incoming location. If no known location is found for a particular
#' incoming location, that record in the tibble will contain all \code{NA}.
#'
#' @examples
#' library(MazamaLocationUtils)
#'
Expand All @@ -22,11 +29,7 @@
#'
#' # Expanding the radius will find one
#' table_getNearestLocation(locationTbl, lon, lat, radius = 5000) %>% str()
#' @rdname table_getNearestLocation
#' @export
#' @importFrom MazamaCoreUtils stopIfNull
#' @importFrom geodist geodist
#' @importFrom rlang .data

table_getNearestLocation <- function(
locationTbl = NULL,
longitude = NULL,
Expand Down
2 changes: 1 addition & 1 deletion R/table_initializeExisting.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ table_initializeExisting <- function(
# ----- Check for locations that are too close -------------------------------

# Calculate distances between each location
distances <- geodist::geodist(locationTbl)
distances <- geodist::geodist(locationTbl, measure = "geodesic")

# Get distances that are less than the given diameter
# NOTE: the distance between a location and itself is always zero
Expand Down
Loading

0 comments on commit 15e8a6d

Please sign in to comment.