Skip to content

Commit

Permalink
Merge pull request #23 from loreabad6/main
Browse files Browse the repository at this point in the history
as_cubble method for stars and make_spatial_sf suggestions
  • Loading branch information
huizezhang-sherry committed Jan 19, 2024
2 parents 734e089 + b7aabc0 commit e63a8db
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions R/as-cubble.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#'# stars - take a few seconds to run
#' tif <- system.file("tif/L7_ETMs.tif", package = "stars")
#' x <- stars::read_stars(tif)
#' x |> as_cubble()
#' x |> as_cubble(index = band)
#'}
#'
#' # don't have to supply coords if create from a sftime
Expand Down Expand Up @@ -172,24 +172,25 @@ as_cubble.ncdf4 <- function(data, key, index, coords, vars,
#' @export
as_cubble.stars <- function(data, key, index, coords, ...){

# making the assumption that long/lat are the first two dimensions
# time is the third
if (is.na(stars::st_raster_type(data))) { # vector data cube
stopifnot(is.null(data$id),
inherits(stars::st_get_dimension_values(data, 1), "sfc"))
# Check if any of the dimensions is an sfc
any(sapply(
stars::st_dimensions(data),
function(i) inherits(i$values, "sfc"))))
data$id <- seq_len(dim(data)[1]) # recycles
data <- sf::st_as_sf(data, long = TRUE)
key <- enquo(key)
index <- enquo(index)
as_cubble(data, key=!!key, index=!!index)
} else { # raster data cube
# making the assumption that long/lat are the first two dimensions
longlat <- names(stars::st_dimensions(data))[1:2]
time <- names(stars::st_dimensions(data))[3]

index <- enquo(index)
as_tibble(data) |>
mutate(id = as.integer(interaction(!!sym(longlat[[1]]),
!!sym(longlat[[2]])))) |>
as_cubble(key = id, index = time, coords = longlat)
as_cubble(key = id, index = !!index, coords = longlat)
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ make_spatial_sf <- function(x, sfc = NULL, crs, silent = FALSE) {
stopifnot(is_cubble_spatial(x),
is.null(sfc) || inherits(sfc, "sfc"),
missing(crs) || inherits(crs, "crs"),
all(c("long", "lat") %in% names(x)))
all(c("long", "lat") %in% names(x)) || all(c("x", "y") %in% names(x)))
if (! requireNamespace("sf", quietly = TRUE))
stop("package sf required, please install it first")
if (is.null(sfc)) {
Expand Down
2 changes: 1 addition & 1 deletion vignettes/cb2create.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ as_cubble(raw, vars = "q",
```{r}
tif <- system.file("tif/L7_ETMs.tif", package = "stars")
x <- stars::read_stars(tif)
as_cubble(x)
as_cubble(x, index = band)
```

When the `dimensions` object is too complex for the `cubble` package to handle, a warning message will be generated.
Expand Down

0 comments on commit e63a8db

Please sign in to comment.