-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
st_as_stars method for ncdfgeom object Fixes #65
- Loading branch information
1 parent
9ff8c41
commit c1ede8a
Showing
7 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#' Convert ncdfgeom object into stars object. | ||
#' @importFrom stars st_as_stars | ||
#' @param .x Object of class ncdfgeom as returned by read_timeseries_dsg. | ||
#' @param ... not used. | ||
#' @param sf_geometry sf data.frame with geometry and attributes to be added to stars object. | ||
#' Must have same number of rows as timeseries instances. | ||
#' @name st_as_stars | ||
#' @export | ||
#' | ||
st_as_stars.ncdfgeom <- function(.x, ..., sf_geometry = NA) { | ||
crs <- st_crs(4326)$proj4string | ||
ts_points <- data.frame(X = .x$lons, Y = .x$lats, Z = .x$alts) | ||
ts_points <- sf::st_as_sf(ts_points, coords = c("X", "Y", "Z"), crs = crs) | ||
|
||
data <- .x$data_frames[[1]] | ||
# data[["T"]] <- .x$time | ||
|
||
gdim <- stars:::create_dimension(from = 1, to = length(.x$lats), | ||
refsys = crs, point = TRUE, | ||
values = ts_points$geometry) | ||
tdim <- stars:::create_dimension(from = 1, to = length(.x$time), | ||
refsys = "POSIXct", point = FALSE, | ||
values = as.POSIXct(.x$time)) | ||
dim <- list(time = tdim, points = gdim) | ||
|
||
if("sf" %in% class(sf_geometry)) { | ||
if(length(gdim$values) != length(st_geometry(sf_geometry))) | ||
stop("geometry must be same length as instance dimension of timeseries") | ||
|
||
is_point <- any(grepl("point", class(st_geometry(sf_geometry)), ignore.case = TRUE)) | ||
|
||
sf_dim <- stars:::create_dimension(from = 1, to = length(gdim$values), | ||
refsys = st_crs(sf_geometry)$proj4string, | ||
point = is_point, is_raster = FALSE, | ||
values = st_geometry(sf_geometry)) | ||
|
||
dim <- c(dim, list(geometry = sf_dim)) | ||
} | ||
|
||
stars:::st_stars(x = setNames(list(as.matrix(.x$data_frames[[1]])), | ||
.x$varmeta[[1]]$name), | ||
dimensions = stars:::create_dimensions(dim)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
context("st_as_stars tests") | ||
|
||
test_that("basic st_as_stars", { | ||
test_list <- get_test_ncdf_object() | ||
|
||
stars_obj <- st_as_stars(test_list$ncdfgeom) | ||
|
||
expect_s3_class(stars_obj, "stars") | ||
|
||
dim <- stars::st_dimensions(stars_obj) | ||
expect_equal(sf::st_crs(dim$points$refsys), sf::st_crs(4326)) | ||
expect_equal(dim$time$refsys, "POSIXct") | ||
|
||
expect_s3_class(dim$points$values, "sfc_POINT") | ||
|
||
expect_true(dim$points$point) | ||
|
||
stars_obj <- st_as_stars(test_list$ncdfgeom, sf_geometry = test_list$sf) | ||
|
||
dim <- stars::st_dimensions(stars_obj) | ||
expect_equal(sf::st_crs(dim$geometry$refsys), sf::st_crs(test_list$sf)) | ||
|
||
expect_s3_class(dim$geometry$values, "sfc_POLYGON") | ||
|
||
expect_false(dim$geometry$point) | ||
|
||
}) |