Skip to content

Commit

Permalink
fix s3 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
geryan committed Dec 19, 2024
1 parent 2347c22 commit f963e6e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ S3method(ext_matrix,SpatExtent)
S3method(ext_matrix,SpatRaster)
S3method(ext_matrix,SpatVector)
S3method(ext_matrix,default)
S3method(ext_matrix,double)
S3method(ext_matrix,matrix)
S3method(ext_matrix,vector)
export(calculate_travel_time)
export(ext_from_terra)
export(ext_matrix)
Expand Down
45 changes: 21 additions & 24 deletions R/ext_matrix.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
#' @title Convert extent to matrix
#'
#' @description
#' This function converts from formats ...
#' TODO - explain why we need to do this, which is that we need a special esoteric format
#' This function allows `get_friction_surface()` to accept the argument `extent` as a `vector`, 2x2 `matrix`, `SpatVector`, or `SpatRaster` and extracts and converts into the annoyingly specific format necessary to download the friction surface. See details in `?get_friction_surface`
#'
#' @param x `numeric` length 4, consisting of `c(xmin, xmax, ymin, ymax)`
#' dimensions of extent
#' @param extent `vector` of length 4, 2x2 `matrix`, `SpatVector`, or `SpatRaster`
#'
#' @return 2x2 `matrix` - explain esoteric format of matrix
#' @return 2x2 `matrix` with column names "x" and "y" and row names "min"
#' and "max
#'
#' @examples
#' ext_matrix(c(111,112,0, 1))
#' # TODO
#' # add examples of all SpatRaster, SpatVector, etc.
#'
#' @export
ext_matrix <- function(x, ...){
ext_matrix <- function(extent){
UseMethod("ext_matrix")
}

#' @export
ext_matrix.SpatExtent <- function(x, ...){
extent <- ext_vect_to_matrix(x)
ext_matrix.SpatExtent <- function(extent){
extent <- ext_vect_to_matrix(extent)
extent
}

#' @export
ext_matrix.SpatRaster <- function(x, ...){
extent <- ext_from_terra(x)
ext_matrix.SpatRaster <- function(extent){
extent <- ext_from_terra(extent)
extent
}

#' @export
ext_matrix.SpatVector <- function(x, ...){
extent <- ext_from_terra(x)
ext_matrix.SpatVector <- function(extent){
extent <- ext_from_terra(extent)
extent
}

#' @export
ext_matrix.vector <- function(x){
ext_matrix.double <- function(extent){
if (length(extent) != 4){
cli::cli_abort(
message = c(
Expand All @@ -46,33 +44,32 @@ ext_matrix.vector <- function(x){
)
)
}
extent <- ext_vect_to_matrix(x)
extent <- ext_vect_to_matrix(extent)
extent
}

#' @export
ext_matrix.matrix <- function(x, ...){
is_2x2 <- identical(dim(x), c(2L,2L))
ext_matrix.matrix <- function(extent){
is_2x2 <- identical(dim(extent), c(2L,2L))
if(!is_2x2){
cli::cli_abort(
message = c(
"If {.arg x} is of class, {.cls matrix}, it must have dimensions: 2x2",
"If {.arg extent} is of class, {.cls matrix}, it must have dimensions: 2x2",
"However, we see that {.arg x} has dimensions: \\
{.val {paste0(dim(x), collapse = 'x')}}."
{.val {paste0(dim(extent), collapse = 'x')}}."
)
)
}
extent <- x
extent
}

#' @export
ext_matrix.default <- function(x, ...){
ext_matrix.default <- function(extent){
cli::cli_abort(
message = c(
"{.arg extent} must be of class {.cls numeric, matrix, \\
SpatExtent, SpatRaster}",
"But we see class: {.cls {class(x)}."
SpatExtent, SpatRaster, SpatVector}",
"But we see class: {.cls {class(extent)}}."
)
)
}
14 changes: 6 additions & 8 deletions man/ext_matrix.Rd

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

2 changes: 1 addition & 1 deletion man/traveltime-package.Rd

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

1 change: 1 addition & 0 deletions traveltime.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: d7cb03ec-3557-4217-8f1a-4913ea040d41

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down

0 comments on commit f963e6e

Please sign in to comment.