Skip to content

Commit 259c23d

Browse files
authored
Merge pull request #6 from rhijmans/master
support SpatRaster
2 parents 3fc1d7a + edea7e9 commit 259c23d

File tree

5 files changed

+97
-13
lines changed

5 files changed

+97
-13
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Imports:
2424
htmlwidgets,
2525
lattice,
2626
raster,
27+
terra,
2728
viridisLite
2829
Suggests:
2930
jpeg

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ import(raster)
77
importFrom(grDevices,dev.off)
88
importFrom(grDevices,png)
99
importFrom(viridisLite,inferno)
10+
importFrom(terra,nlyr,spatSample,as.matrix)

R/raster.R

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ raster2PNG <- function(x,
77
na.color,
88
maxpixels) {
99

10-
x <- rasterCheckSize(x, maxpixels = maxpixels)
11-
12-
mat <- t(raster::as.matrix(x))
13-
10+
is_spatraster <- inherits(x, "SpatRaster")
11+
x <- rasterCheckSize(x, maxpixels = maxpixels, is_spatraster)
12+
if (is_spatraster) {
13+
mat <- t(terra::as.matrix(x, wide=TRUE))
14+
} else {
15+
mat <- t(raster::as.matrix(x))
16+
}
1417
if (missing(at)) at <- lattice::do.breaks(range(mat, na.rm = TRUE), 256)
1518

1619
cols <- lattice::level.colors(mat,
@@ -34,14 +37,12 @@ rgbStack2PNG <- function(x, r, g, b,
3437
maxpixels,
3538
...) {
3639

37-
x <- rasterCheckSize(x, maxpixels = maxpixels)
38-
39-
x3 <- raster::subset(x, c(r, g, b))
40-
41-
mat <- cbind(x[[r]][],
42-
x[[g]][],
43-
x[[b]][])
40+
is_spatraster <- inherits(x, "SpatRaster")
41+
x <- rasterCheckSize(x, maxpixels = maxpixels, is_spatraster)
4442

43+
x <- x[[c(r, g, b)]]
44+
mat <- values(x)
45+
4546
for(i in seq(ncol(mat))){
4647
z <- mat[, i]
4748
lwr <- stats::quantile(z, quantiles[1], na.rm = TRUE)
@@ -63,13 +64,17 @@ rgbStack2PNG <- function(x, r, g, b,
6364
}
6465

6566

66-
rasterCheckSize <- function(x, maxpixels) {
67+
rasterCheckSize <- function(x, maxpixels, is_spatraster) {
6768
if (maxpixels < raster::ncell(x)) {
6869
warning(paste("maximum number of pixels for Raster* viewing is",
6970
maxpixels, "; \nthe supplied Raster* has", raster::ncell(x), "\n",
7071
"... decreasing Raster* resolution to", maxpixels, "pixels\n",
7172
"to view full resolution set 'maxpixels = ", raster::ncell(x), "'"))
72-
x <- raster::sampleRegular(x, maxpixels, asRaster = TRUE, useGDAL = TRUE)
73+
if (is_spatraster) {
74+
x <- terra::spatSample(x, maxpixels, "regular", as.raster = TRUE)
75+
} else {
76+
x <- raster::sampleRegular(x, maxpixels, asRaster = TRUE, useGDAL = TRUE)
77+
}
7378
}
7479
return(x)
7580
}

R/slideView.R

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,66 @@ if ( !isGeneric('slideView') ) {
9696
#' @rdname slideView
9797
#' @aliases slideView,RasterStackBrick,RasterStackBrick-method
9898

99+
100+
setMethod("slideView", signature(img1 = "SpatRaster",
101+
img2 = "SpatRaster"),
102+
function(img1,
103+
img2,
104+
label1 = deparse(substitute(img1, env = parent.frame())),
105+
label2 = deparse(substitute(img2, env = parent.frame())),
106+
r = 3,
107+
g = 2,
108+
b = 1,
109+
col.regions = viridisLite::inferno(256),
110+
legend = TRUE,
111+
na.color = "#BEBEBE",
112+
maxpixels = 1e7,
113+
...) {
114+
115+
if (nlyr(img1) > 1) {
116+
png1 <- rgbStack2PNG(img1, r = r, g = g, b = b,
117+
na.color = na.color,
118+
maxpixels = maxpixels,
119+
...)
120+
} else {
121+
png1 <- raster2PNG(img1, col.regions = col.regions,
122+
na.color = na.color,
123+
maxpixels = maxpixels)
124+
125+
}
126+
if (nlyr(img2) > 1) {
127+
png2 <- rgbStack2PNG(img2, r = r, g = g, b = b,
128+
na.color = na.color,
129+
maxpixels = maxpixels,
130+
...)
131+
} else {
132+
png2 <- raster2PNG(img2, col.regions = col.regions,
133+
na.color = na.color,
134+
maxpixels = maxpixels)
135+
}
136+
137+
## temp dir
138+
dir <- tempfile()
139+
dir.create(dir)
140+
# r1 <- trunc(runif(1, 1000000000, 9999999999))
141+
# r2 <- trunc(runif(1, 1000000000, 9999999999))
142+
fl1 <- paste0(dir, "/", label1, ".png")
143+
fl2 <- paste0(dir, "/", label2, ".png")
144+
145+
## pngs
146+
png::writePNG(png1, fl1)
147+
png::writePNG(png2, fl2)
148+
149+
slideViewInternal(list(a="a", b="b"),
150+
img1nm = label1,
151+
img2nm = label2,
152+
filename1 = fl1,
153+
filename2 = fl2)
154+
}
155+
156+
)
157+
158+
99159
setMethod("slideView", signature(img1 = "RasterStackBrick",
100160
img2 = "RasterStackBrick"),
101161
function(img1,

man/slideView.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)