Skip to content

Commit 30375da

Browse files
committed
clean up c.stars() for time sequences
closes r-spatial#703
1 parent a628290 commit 30375da

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# version 0.6-8
22

3+
* `c.stars()` is more strict when combining time sequences; #703
4+
35
* fix plotting when breaks contain duplicates; #728
46

57
* fix `st_as_stars.im()`; #727 and #648, thanks to Barry Rowlingson

R/dimensions.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,11 @@ as.POSIXct.stars = function(x, ...) {
792792
d = st_dimensions(x)
793793
e = expand_dimensions(d)
794794
for (i in seq_along(d)) {
795-
p = try(as.POSIXct(e[[i]]), silent = TRUE)
796-
if (!inherits(p, "try-error"))
797-
d[[i]] = create_dimension(values = p)
795+
if (inherits(e[[i]], c("Date", "POSIXt", "udunits", "PCICt"))) {
796+
p = try(as.POSIXct(e[[i]]), silent = TRUE)
797+
if (!inherits(p, "try-error"))
798+
d[[i]] = create_dimension(values = p)
799+
}
798800
}
799801
structure(x, dimensions = d)
800802
}

R/stars.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,20 @@ merge.stars = function(x, y, ..., name = "attributes") {
904904
}
905905

906906
sort_out_along = function(ret) {
907-
d1 = st_dimensions(ret[[1]])
908-
d2 = st_dimensions(ret[[2]])
909-
if ("time" %in% names(d1) && (isTRUE(d1$time$offset != d2$time$offset) ||
910-
!any(d1$time$values %in% d2$time$values)))
911-
"time"
912-
else
913-
NA_integer_
907+
# https://github.com/r-spatial/stars/issues/703 :
908+
# 1. check that time is a dimension name in all objects
909+
l = lapply(ret, st_dimensions)
910+
if (!all(sapply(l, function(x) "time" %in% names(x))))
911+
return(NA_integer_)
912+
# 2. check that time values do not overlap
913+
lv = lapply(l, st_get_dimension_values, "time")
914+
for (i in seq_along(lv)) {
915+
if (!inherits(lv[[i]], c("POSIXt", "Date", "PCICt")))
916+
return(NA_integer_)
917+
if (i < length(lv) && max(lv[[i]]) >= min(lv[[i+1]])) # no sequence
918+
return(NA_integer_)
919+
}
920+
return("time")
914921
}
915922

916923

0 commit comments

Comments
 (0)