diff --git a/R/get_example_grid_germany.R b/R/get_example_grid_germany.R index ddd784f3..9fa1d622 100644 --- a/R/get_example_grid_germany.R +++ b/R/get_example_grid_germany.R @@ -8,6 +8,15 @@ #' * [get_shapes_of_germany]. get_example_grid_germany <- function() { + #kwb.utils::assignPackageObjects("kwb.dwd") + path <- "evapo_p/grids_germany_monthly_evapo_p_202201.asc.gz" - read_asc_gz_file(url = ftp_path_grids_germany("monthly", path)) + url <- ftp_path_grids_germany("monthly", path) + + #epsg <- get_epsg_from_url(url) + epsg <- 31467L + + crs <- sf::st_crs(epsg) + + read_asc_gz_file(url = url, crs = crs) } diff --git a/R/read_asc_gz_file.R b/R/read_asc_gz_file.R index 1db4e7ec..9a7f1ec6 100644 --- a/R/read_asc_gz_file.R +++ b/R/read_asc_gz_file.R @@ -11,7 +11,11 @@ #' @seealso #' * [read_binary_radolan_file], #' * [unzip_asc_gz_file]. -read_asc_gz_file <- function(file, url = NULL) +read_asc_gz_file <- function( + file, + url = NULL, + crs = if (!is.null(url)) get_epsg_from_url(url) +) { target_dir <- if (is.null(url)) { dirname(file) @@ -26,9 +30,10 @@ read_asc_gz_file <- function(file, url = NULL) ) #dir(target_dir) + #kwb.utils::hsOpenWindowsExplorer(dirname(grid_file)) # Read the .asc file into a raster object (with appropriate projection) - read_asc_file(grid_file) + read_asc_file(file = grid_file, crs = crs) } # read_asc_file ---------------------------------------------------------------- @@ -36,15 +41,12 @@ read_asc_gz_file <- function(file, url = NULL) #' Read Raster Data from .ASC File #' #' @param file path to .asc file -#' @param projection projection string used in Radolan data. Currently not used! +#' @param crs coordinate reference system to be applied to the data. An object +#' of class "crs" (as returned by \code{\link[sf]{st_crs}} is expected here. #' @param dbg logical indicating whether to show debug messages #' @importFrom kwb.utils catAndRun #' @importFrom raster `crs<-` raster -read_asc_file <- function( - file, - projection = get_radolan_projection_string(), - dbg = TRUE -) +read_asc_file <- function(file, crs = NULL, dbg = TRUE) { kwb.utils::catAndRun( sprintf("Reading %s using raster::raster()", basename(file)), @@ -52,13 +54,8 @@ read_asc_file <- function( expr = { # Provide a copy of the projection file in the same folder - provide_projection_file(file) - result <- raster::raster(file) - - #result <- raster::raster(file, values = TRUE) - #raster::crs(result) <- projection - - result + #prj_file <- provide_projection_file(file) + raster::raster(file, native = TRUE, crs = crs) } ) } diff --git a/man/get_epsg_from_url.Rd b/man/get_epsg_from_url.Rd index 56dad34f..4d325d52 100644 --- a/man/get_epsg_from_url.Rd +++ b/man/get_epsg_from_url.Rd @@ -17,8 +17,8 @@ in the given URL. } \description{ The URL is looked up in column \code{dir} of \code{\link{epsg_info}}. If the -URL was found and results in a unique EPSG code (in column \code{epsg}), -that EPSG code is returned. If the URL was not found or if the corresponding +URL is found and results in a unique EPSG code (in column \code{epsg}), +that EPSG code is returned. If the URL is not found or if the corresponding EPSG codes are not unique, an error is raised. } \examples{ diff --git a/man/read_asc_file.Rd b/man/read_asc_file.Rd index ff1f2a95..b729de13 100644 --- a/man/read_asc_file.Rd +++ b/man/read_asc_file.Rd @@ -4,12 +4,13 @@ \alias{read_asc_file} \title{Read Raster Data from .ASC File} \usage{ -read_asc_file(file, projection = get_radolan_projection_string(), dbg = TRUE) +read_asc_file(file, crs = NULL, dbg = TRUE) } \arguments{ \item{file}{path to .asc file} -\item{projection}{projection string used in Radolan data. Currently not used!} +\item{crs}{coordinate reference system to be applied to the data. An object +of class "crs" (as returned by \code{\link[sf]{st_crs}} is expected here.} \item{dbg}{logical indicating whether to show debug messages} } diff --git a/man/read_asc_gz_file.Rd b/man/read_asc_gz_file.Rd index 82a70402..579875db 100644 --- a/man/read_asc_gz_file.Rd +++ b/man/read_asc_gz_file.Rd @@ -4,7 +4,11 @@ \alias{read_asc_gz_file} \title{Read Zipped ESRI-Ascii-Grid File (from URL)} \usage{ -read_asc_gz_file(file, url = NULL) +read_asc_gz_file( + file, + url = NULL, + crs = if (!is.null(url)) get_epsg_from_url(url) +) } \arguments{ \item{file}{path to zipped file in ESRI-ascii-grid format (.asc.gz)} diff --git a/tests/testthat/test-function-get_epsg_from_url.R b/tests/testthat/test-function-get_epsg_from_url.R index b841826b..d3c51e48 100644 --- a/tests/testthat/test-function-get_epsg_from_url.R +++ b/tests/testthat/test-function-get_epsg_from_url.R @@ -12,7 +12,7 @@ test_that("get_epsg_from_url() works", { url_2 <- paste0(start_url, "precipitation/01_Jan/grids_germany_monthly_precipitation_188101.asc.gz") url_3 <- kwb.dwd:::get_radolan_url("hourly", year_month = "202001") - expect_error(f(url_1)) + capture.output(expect_error(f(url_1))) expect_identical(f(url_2), 31467L) expect_identical(f(url_3), 3034L) }) diff --git a/tests/testthat/test-function-read_asc_file.R b/tests/testthat/test-function-read_asc_file.R index d2352a7c..f3308ecf 100644 --- a/tests/testthat/test-function-read_asc_file.R +++ b/tests/testthat/test-function-read_asc_file.R @@ -6,5 +6,4 @@ test_that("read_asc_file() works", { expect_error(f()) expect_error(expect_warning(f("no-such-file", dbg = FALSE))) - }) diff --git a/tests/testthat/test-function-read_asc_gz_file.R b/tests/testthat/test-function-read_asc_gz_file.R index 92a5f2c2..dc81bf0e 100644 --- a/tests/testthat/test-function-read_asc_gz_file.R +++ b/tests/testthat/test-function-read_asc_gz_file.R @@ -1,17 +1,23 @@ -# -# This test file has been generated by kwb.test::create_test_files() -# launched by user hsonne on 2022-09-16 16:43:24. -# Your are strongly encouraged to modify the dummy functions -# so that real cases are tested. You should then delete this comment. -# +#library(testthat) test_that("read_asc_gz_file() works", { f <- kwb.dwd:::read_asc_gz_file - expect_error( - kwb.dwd:::read_asc_gz_file() - # argument "file" is missing, with no default - ) + expect_error(f()) + start_url <- "ftp://opendata.dwd.de/climate_environment/CDC/grids_germany/monthly/" + + #head(grep("\\.asc.gz$", kwb.dwd::dwd_files$file, value = TRUE)) + + url_1 <- paste0(start_url, "evapo_p/grids_germany_monthly_evapo_p_202201.asc.gz") + url_2 <- paste0(start_url, "precipitation/01_Jan/grids_germany_monthly_precipitation_188101.asc.gz") + + crs <- sf::st_crs(31467L) + + expect_output(result_1 <- f(url = url_1, crs = crs)) + expect_output(result_2 <- f(url = url_2, crs = crs)) + + expect_s4_class(result_1, "RasterLayer") + expect_s4_class(result_2, "RasterLayer") })