Skip to content

Commit

Permalink
support gdalraster output
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Feb 13, 2024
1 parent 552be74 commit daeff30
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^LICENSE\.md$
^CODE_OF_CONDUCT\.md$
^\.github$
^gdalwmscache$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.Rdata
.httr-oauth
.DS_Store
gdalwmscache
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
18 changes: 16 additions & 2 deletions R/ximage.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
Expand All @@ -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))
}
Expand Down

0 comments on commit daeff30

Please sign in to comment.