Skip to content

Commit

Permalink
list method for vapour xcontour
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Dec 13, 2023
1 parent 14644fb commit 487223e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
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.9005
Version: 0.0.0.9006
Authors@R:
person("Michael D.", "Sumner", , "[email protected]", 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 NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(xcontour,default)
S3method(xcontour,list)
S3method(ximage,default)
S3method(ximage,list)
S3method(ximage,nativeRaster)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# ximage dev

* `ximage()` can now plot sf::gdal_read objects.
* Added suport for `xcontour()` for the output output of `gdal_raster_data()` in {vapour}.

* `ximage()` can now plot sf::gdal_read objects.

* Fixed default extent for an array/matrix, it was transposed (!).

Expand Down
52 changes: 52 additions & 0 deletions R/xcontour.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#'
#' To work with [ximage()]
#'
#' Input may be a matrix or a list from gdal_raster_data() in the vapour package.
#'
#' @param x something we can contour
#' @inheritParams ximage
#' @inheritDotParams ximage
Expand All @@ -16,6 +18,10 @@
#' #ximage(im, add = TRUE, extent = ex)
#' xcontour(v, add = TRUE, extent = ex, col = "white")
xcontour <- function(x, extent = NULL, ..., add = FALSE) {
UseMethod("xcontour")
}
#' @export
xcontour.default <- function(x, extent = NULL, ..., add = FALSE) {
x <- t(x[nrow(x):1, ])
if (is.null(extent)) extent <- c(0, ncol(x), 0, nrow(x))
xre <- diff(extent[1:2])/nrow(x)
Expand All @@ -24,3 +30,49 @@ xcontour <- function(x, extent = NULL, ..., add = FALSE) {
yy <- seq(extent[3] + yre/2, extent[4] - yre/2, length.out = ncol(x) )
graphics::contour(xx, yy, x, add = add, ...)
}
#' @export
xcontour.list <- function(x, extent = NULL, ..., add = FALSE) {

if (all(c("geotransform", "cols", "rows", "driver") %in% names(x))) {
## smells like sf
stop("no xcontour for sf")
ximage_sf_data(x, extent = extent, zlim = zlim, add = add, ..., xlab = xlab, ylab = ylab, col = col)
return(invisible(x))
}
## here validate that we have extent, dimension as attributes, otherwise just see if it's a matrix
attrs <- attributes(x)
if (!is.null(attrs$extent) && is.null(extent)) extent <- attrs$extent
dimension <- NULL
if (!is.null(attrs$dimension)) {
dimension <- attrs$dimension

}
projection <- NULL

if (is.null(dimension)) {
if (is.null(dim(x[[1]]))) {
dimension <- dim(x[[1]])
} else {
stop("no dimension known")
}
}
if (!is.null(attrs$projection)) projection <- attrs$projection
if (is.character(x[[1]])) {
if (grepl("^#", stats::na.omit(x[[1]])[1])) {
## we have image data
} else {
## can't read data in ximage
stop("can't read data in the this package")
# x[[1]] <- as.vector(t(elevation(source = x[[1]], extent = attr(x, "extent"), dimension = attr(x, "dimension"), projection = attr(x, "projection"))))
}
}


xcontour(matrix(x[[1]], dimension[2L], byrow = TRUE),
extent = extent, add = add, ...)

##if (coastline) graphics::lines(coastline(extent, projection = projection, dimension = c(512, 512)))

## return the materialized data
invisible(x)
}
3 changes: 3 additions & 0 deletions man/xcontour.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 487223e

Please sign in to comment.