From daeff302faab67879013419f8eab8875ee20f38c Mon Sep 17 00:00:00 2001 From: Michael Sumner Date: Tue, 13 Feb 2024 00:13:27 +0000 Subject: [PATCH] support gdalraster output --- .Rbuildignore | 1 + .gitignore | 1 + DESCRIPTION | 2 +- NEWS.md | 2 ++ R/ximage.R | 18 ++++++++++++++++-- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 354b712..f22f562 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,3 +5,4 @@ ^LICENSE\.md$ ^CODE_OF_CONDUCT\.md$ ^\.github$ +^gdalwmscache$ diff --git a/.gitignore b/.gitignore index 565f2b6..e5d6877 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .Rdata .httr-oauth .DS_Store +gdalwmscache diff --git a/DESCRIPTION b/DESCRIPTION index 68717be..331e536 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ximage Title: Draw Images of Raster Data and Related Adornments -Version: 0.0.0.9009 +Version: 0.0.0.9010 Authors@R: person("Michael D.", "Sumner", , "mdsumner@gmail.com", role = c("aut", "cre")) Description: Draw images easily and as if doing that was considered desirable or diff --git a/NEWS.md b/NEWS.md index a15846d..7e8570b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ximage 0.0 +* Now support {gdalraster} output of `read_ds(, as_list = TRUE)`. + * `ximage()` now handles `breaks` argument to go with `col` (for numeric input). * `image()` now works with list output from gdalnara (same as gdal_raster_image but with nativeRaster). diff --git a/R/ximage.R b/R/ximage.R index ef942b4..63a529c 100644 --- a/R/ximage.R +++ b/R/ximage.R @@ -106,6 +106,13 @@ ximage.list <- function(x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = ## here validate that we have extent, dimension as attributes, otherwise just see if it's a matrix attrs <- attributes(x) + if ("gis" %in% names(attrs)) { + ## gdalraster output + attrs <- attrs[["gis"]] + attrs$dimension <- attrs$dim + attrs$projection <- attrs$srs + attrs$extent <- attrs$bbox[c(1, 3, 2, 4)] + } if (!is.null(attrs$extent) && is.null(extent)) extent <- attrs$extent dimension <- NULL if (!is.null(attrs$dimension)) { @@ -184,9 +191,14 @@ ximage.default <- function(x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab #x <- .make_hex_matrix(x, cols = col ) ## politely ignore numeric arrays with 3 or 4 slices dmx <- dim(x) - tt <- length(dmx %in% c(3, 4)) && is.numeric(x) && all(x >= 0, na.rm = TRUE) - if (!tt && !is.null(col)) { + tt <- length(dmx %in% c(3, 4)) && is.numeric(x) ##&& all(x >= 0, na.rm = TRUE) + + #if (!tt && !is.null(col)) { + if (!tt) { + #browser() + if (is.null(col)) col <- colorRampPalette(grDevices::hcl.colors(12, "YlOrRd", + rev = TRUE)) x <- matrix(palr::image_pal(x, col, breaks = breaks), dim(x)[1L], dim(x)[2L]) } else { x <- (x - rg[1L])/diff(rg) @@ -211,6 +223,8 @@ ximage.default <- function(x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab #ximage_meshplot(x, extent, add = add) } if (!add) plot(extent[1:2], extent[3:4], type = "n", ..., xaxs = "i", yaxs = "i", xlab = xlab, ylab = ylab) + + if (anyNA(x)) x[is.na(x)] <- 1 graphics::rasterImage(x, extent[1], extent[3], extent[2], extent[4], interpolate = FALSE) invisible(list(x = x, extent = extent)) }