Skip to content

Commit

Permalink
Merge pull request #9 from DPIRD-FSI/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
adamhsparks authored Apr 10, 2023
2 parents cd8d196 + afd9177 commit 69d620c
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 99 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: extractOz
Title: Extract Australian Soil, Weather and GRDC Agro-ecological Zone
Information from GPS Points
Version: 0.1.3.9000
Version: 1.0.0
Authors@R: c(person(given = "Adam H.", family = "Sparks",
email = "[email protected]",
role = c("aut", "cre"),
Expand All @@ -21,7 +21,7 @@ RoxygenNote: 7.2.3
Imports:
curl,
data.table,
httr2,
httr,
methods,
nasapower,
purrr,
Expand All @@ -45,3 +45,6 @@ Language: en-US
X-schema.org-applicationCategory: Tools
X-schema.org-keywords: agriculture, Australia, agroecology, soils, weather, weather-data, soils-data, agriculture-research
X-schema.org-isPartOf: https://www.dpird.wa.gov.au/
URL: https://github.com/DPIRD-FSI/extractOz
BugReports: https://github.com/DPIRD-FSI/extractOz/issues
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(extract_area)
export(extract_daas_soil_order)
export(extract_power)
export(extract_silo)
export(remove_cache)
importFrom(data.table,":=")
importFrom(data.table,.BY)
importFrom(data.table,.EACHI)
Expand Down
14 changes: 13 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# extractOz 0.1.3.9000
# extractOz 1.0.0

## Major changes

- All functions related to retrieving or extracting data are now prefixed as `extract_noun()`.

- Revert using internal data as geopackage, use in examples but use .Rda internally.

- Add support for extracting weather data from the NASA POWER database, `extract_power()`.

- Move from {cropgrowdays} to {weatherOz}, this allows for more flexibility in the queries that can be performed in SILO.
Expand All @@ -14,6 +18,14 @@ To help users, a new function, `df_to_list()` has been added so that the workflo

- Enhanced tests.

- Add ability to manage DAAS soil order cache.

- Add ability to download DAAS soil order data but not cache locally (default behaviour now).

## Bug fixes

- Remove several `Warning` messages when performing {sf} operations.

# extractOz 0.1.3

## Bug fixes
Expand Down
14 changes: 14 additions & 0 deletions R/aez.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' GRDC Australian Grains Agroecological Zones
#'
#' An [sf::sf()] object for displaying GRDC Australian Grains Agroecological
#' Zones
#'
#' @section Coordinate Reference System:
#' Geodetic CRS: WGS 84
#'
#' @format An [sf::sf()] polygon object
#'
#' @source The Australian Grains Research and Development Corporation,
#' \acronym{GRDC}, <https://grdc.com.au/> under CC0 licence.
#'
"aez"
2 changes: 0 additions & 2 deletions R/extract_ae_zone.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ extract_ae_zone <- function(x) {
crs = sf::st_crs(aez)
)



intersection <- as.integer(sf::st_intersects(points_sf, aez))
zone <- data.table::data.table(ifelse(is.na(intersection), "",
as.character(aez$AEZ[intersection])))
Expand Down
2 changes: 1 addition & 1 deletion R/extract_power.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' not for use with \dQuote{global} or with a regional request. If this
#' parameter is provided, the `wind-surface` parameter is required with the
#' request, see
#' \url{https://power.larc.nasa.gov/docs/methodology/meteorology/wind/}.
#' <https://power.larc.nasa.gov/docs/methodology/meteorology/wind/>.
#' @param wind_surface A user-supplied wind surface for which the corrected
#' wind-speed is to be supplied. See `wind-surface` section for more detail.
#' @param temporal_average Deprecated. This argument has been superseded by
Expand Down
128 changes: 79 additions & 49 deletions R/extract_soil_order.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#' Extracts the major soil order at the GPS points provided.
#'
#' @inheritParams extract_ae_zone
#' @param cache `Boolean`. Store soil data locally for later use? If `FALSE`,
#' the downloaded files are removed when \R session is closed. To take advantage
#' of cached files in future sessions, use `cache = TRUE`. Defaults to `FALSE`.
#' Value is optional. All future requests will use the cached data unless
#' `remove_cache()` is used to remove the cached file.
#'
#' @note The first run will take additional time to download and extract the
#' soils data and cache it locally for future use. Any use after this will be
#' much faster due to the locally cached geospatial soils data.
#' soils data. If `cache = TRUE`, any use after this will be much faster due to
#' the locally cached geospatial soils data.
#'
#' @return A `data.table` with the provided \acronym{GPS} coordinates and the
#' respective Digital Atlas of Australian Soils (\acronym{DAAS} soil order),
Expand All @@ -23,17 +28,25 @@
#' "Tamworth" = c(x = 150.84, y = -31.07)
#' )
#'
#' extract_daas_soil_order(x = locs)
#' extract_daas_soil_order(x = locs, cache = FALSE)
#' @export

extract_daas_soil_order <- function(x) {
extract_daas_soil_order <- function(x, cache = FALSE) {
# check if the DAAS data exists in the local cache and if not, download and
# mask it before saving in the user cache

.check_for_cache()
.check_lonlat(x)

load(.get_cache_file())
if (file.exists(.get_cache_file())) {
load(.get_cache_file())
} else {
if (isTRUE(cache)) {
message("This is the first time using caching, a cache will be created.")
daas <- .get_daas_data(.cache = cache)
} else {
daas <- .get_daas_data(.cache = cache)
}
}

x <- .create_dt(x)

Expand All @@ -45,7 +58,7 @@ extract_daas_soil_order <- function(x) {

intersection <- as.integer(sf::st_intersects(points_sf, daas))
soil <- data.table::data.table(ifelse(is.na(intersection), "",
as.character(daas$SOIL[intersection])))
as.character(daas$SOIL[intersection])))

out <- cbind(x, soil)
data.table::setnames(out, old = "V1", new = "daas_soil_order")
Expand All @@ -71,48 +84,65 @@ extract_daas_soil_order <- function(x) {

#' @noRd
# nocov start
.check_for_cache <- function() {
if (!file.exists(.get_cache_file())) {
if (!dir.exists(.get_cache_dir())) {
dir.create(path = .get_cache_dir(), recursive = TRUE)
.get_daas_data <- function(.cache) {
u_remote <-
"https://data.gov.au/data/"
d_remote <-
"dataset/5ccb44bf-93f2-4f94-8ae2-4c3f699ea4e7/resource/56ba5f25-2324-43b5-8df8-b9c69ae2ea0b/download/"
filename <- "6f804e8b-2de9-4c88-adfa-918ec327c32f.zip"

url <- paste0(u_remote, d_remote, filename)
tryCatch(
# check for an http error b4 proceeding
if (!httr::http_error(url)) {
h <- curl::new_handle()
curl::handle_setopt(h, CONNECTTIMEOUT = 120L)
curl::curl_download(
url = url,
destfile = file.path(tempdir(), filename),
quiet = FALSE,
handle = h,
mode = "wb"
)

utils::unzip(file.path(tempdir(), filename), exdir = tempdir())

x <-
sf::st_read(dsn = file.path(tempdir(), "SoilAtlas2M_ASC_Conversion_v01"),
layer = "soilAtlas2M_ASC_Conversion")
x <- x[with(x, SOIL != "Nodata" | SOIL != "Lake"),]
x <- sf::st_transform(x, crs = sf::st_crs(aez))
sf::st_agr(x) = "constant"
sf::st_agr(aez) = "constant"
daas <- sf::st_intersection(x, aez)
},
error = function(x)
stop(
call. = FALSE,
"\nThe file download for DAAS has failed. Please try again.\n"
)
)

if (isTRUE(.cache)) {
if (!file.exists(.get_cache_file())) {
if (!dir.exists(.get_cache_dir())) {
dir.create(path = .get_cache_dir(), recursive = TRUE)
}
save(daas, file = .get_cache_file())
}
u_remote <-
"https://data.gov.au/data/"
d_remote <-
"dataset/5ccb44bf-93f2-4f94-8ae2-4c3f699ea4e7/resource/56ba5f25-2324-43b5-8df8-b9c69ae2ea0b/download/"
filename <- "6f804e8b-2de9-4c88-adfa-918ec327c32f.zip"

url <- paste0(u_remote, d_remote, filename)
tryCatch(
# check for an http error b4 proceeding
if (!httr2::req_error(url)) {
h <- curl::new_handle()
curl::handle_setopt(h, CONNECTTIMEOUT = 120L)
curl::curl_download(
url = url,
destfile = file.path(tempdir(), filename),
quiet = FALSE,
handle = h,
mode = "wb"
)

utils::unzip(file.path(tempdir(), filename), exdir = tempdir())

x <- sf::st_read(
dsn = file.path(tempdir(), "SoilAtlas2M_ASC_Conversion_v01"),
layer = "soilAtlas2M_ASC_Conversion"
)
x <- x[with(x, SOIL != "Nodata" | SOIL != "Lake"), ]
x <- sf::st_transform(x, crs = sf::st_crs(aez))
daas <- sf::st_intersection(x = x, y = aez)
save(daas, file = .get_cache_file())
},
error = function(x)
stop(
call. = FALSE,
"\nThe file download for DAAS has failed. Please try again.\n"
)
)
}
return(invisible(NULL))
return(daas)
} #nocov end

#' Remove DAAS Soil Order Cache
#'
#' The cache is located at `tools::R_user_dir("extractOz", which = "cache")`
#'
#' @return `NULL`, called for its side-effects
#' @export
remove_cache = function() {
path = .get_cache_file()
if (file.exists(path))
file.remove(path)
return(invisible(NULL))
}
5 changes: 0 additions & 5 deletions R/zzz.R

This file was deleted.

59 changes: 52 additions & 7 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"identifier": "extractOz",
"description": "Extracts point data for a given GPS coordinate that includes major soil order, weather and GRDC agro-ecological zone information at that point.",
"name": "extractOz: Extract Australian Soil, Weather and GRDC Agro-ecological Zone\n Information from GPS Points",
"codeRepository": "https://github.com/DPIRD-FSI/extractOz",
"issueTracker": "https://github.com/DPIRD-FSI/extractOz/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.3.9000",
"version": "1.0.0",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -59,6 +61,18 @@
},
"sameAs": "https://CRAN.R-project.org/package=knitr"
},
{
"@type": "SoftwareApplication",
"identifier": "readr",
"name": "readr",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=readr"
},
{
"@type": "SoftwareApplication",
"identifier": "rmarkdown",
Expand Down Expand Up @@ -107,6 +121,19 @@
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=tidyverse"
},
{
"@type": "SoftwareApplication",
"identifier": "vcr",
"name": "vcr",
"version": ">= 0.6.0",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=vcr"
}
],
"softwareRequirements": {
Expand Down Expand Up @@ -147,6 +174,23 @@
"sameAs": "https://CRAN.R-project.org/package=httr"
},
"4": {
"@type": "SoftwareApplication",
"identifier": "methods",
"name": "methods"
},
"5": {
"@type": "SoftwareApplication",
"identifier": "nasapower",
"name": "nasapower",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=nasapower"
},
"6": {
"@type": "SoftwareApplication",
"identifier": "purrr",
"name": "purrr",
Expand All @@ -158,7 +202,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=purrr"
},
"5": {
"7": {
"@type": "SoftwareApplication",
"identifier": "sf",
"name": "sf",
Expand All @@ -170,17 +214,17 @@
},
"sameAs": "https://CRAN.R-project.org/package=sf"
},
"6": {
"8": {
"@type": "SoftwareApplication",
"identifier": "utils",
"name": "utils"
},
"7": {
"9": {
"@type": "SoftwareApplication",
"identifier": "weatherOz",
"name": "weatherOz"
},
"8": {
"10": {
"@type": "SoftwareApplication",
"identifier": "R",
"name": "R",
Expand All @@ -190,8 +234,9 @@
},
"applicationCategory": "Tools",
"isPartOf": "https://www.dpird.wa.gov.au/",
"keywords": ["agriculture", "Australia", "agroecology", "soils", "weather", "weather-data", "soils-data", "agriculture-research"],
"fileSize": "3323.622KB",
"keywords": ["agriculture", "Australia", "agroecology", "soils", "weather", "weather-data", "soils-data", "agriculture-research", "r-package", "agriculture-data", "australia"],
"fileSize": "4562.105KB",
"readme": "https://github.com/DPIRD-FSI/extractOz/blob/main/README.md",
"contIntegration": ["https://github.com/DPIRD-FSI/extractOz/actions", "https://codecov.io/gh/DPIRD-FSI/extractOz"],
"developmentStatus": "https://www.repostatus.org/#active"
}
Binary file added data/aez.rda
Binary file not shown.
Loading

0 comments on commit 69d620c

Please sign in to comment.