Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extent for rasterImage angle #8

Open
mdsumner opened this issue Jan 17, 2024 · 2 comments
Open

extent for rasterImage angle #8

mdsumner opened this issue Jan 17, 2024 · 2 comments

Comments

@mdsumner
Copy link
Member

angle_extent <- function(dimension, extent = NULL, angle = 0){

  ## Adapted from rasterImageAdj in Ecfun by Spencer Graves
  ## https://CRAN.R-project.org/package=Ecfun
  imagePixels <- dimension
  if (is.null(extent)) extent <- c(0, dimension[1], 0, dimension[2])
  xleft <- extent[1]
  xright <- extent[2]
  ybottom <- extent[3]
  ytop <- extent[4]

  ##
  ## 2.  x, y pixels per inch
  ##
  imageUnits.x <- (xright-xleft)
  imageUnits.y <- (ytop-ybottom)
  #  2.2.   plot units per inch (we should just use the scale of extent/dimension)
  u <- par("usr")
  xyinches <-  c(u[2L] - u[1L], u[4L] - u[3L])/par("pin")
  #print(xyinches)
  #print(dimension / diff(extent)[c(1, 3)])

  #  2.3.  x, y pixels per inch in image region
  pixelsPerInch.x <- (imagePixels[1]*xyinches[1] /
                        as.numeric(imageUnits.x))
  pixelsPerInch.y <- (imagePixels[2]*xyinches[2] /
                        imageUnits.y)
  ##
  ## 3.  Shrink imageUnits to max(PixelsPerInch)
  ##
  maxPPI <- max(pixelsPerInch.x, pixelsPerInch.y)
  imageUAdj.x <- (imagePixels[1] * xyinches[1] / maxPPI)
  imageUAdj.y <- (imagePixels[2] * xyinches[2] / maxPPI)
  ##
  ## 4.  (dX, dY) = imageUnitsAdj/2
  ##              = half of the (width, height) in plotting units.
  ##
  dX <- imageUAdj.x/2
  dY <- imageUAdj.y/2
  # lower left = 45 degrees from center
  # Therefore, max deviation of a corner from the center:  at 45 degrees
  dX. <- dX*sqrt(2)
  dY. <- dY*sqrt(2)
  ##
  ## 5.  cntr = (xleft, ybottom) + imageUnits/2
  ##
  cntr.x <- (xleft+(imageUnits.x/2))
  cntr.y <- (ybottom+(imageUnits.y/2))
  ##
  ## 6.  (x, y) location of the nominal lower left corner
  ##     after rotation
  ##
  adj.x <- sin((angle-45)*pi/180)*dX.
  adj.y <- cos((angle-45)*pi/180)*dY.
  xleft0 <- (cntr.x + adj.x)
  xright0 <- (xleft0 + imageUAdj.x)
  ybottom0 <- (cntr.y - adj.y)
  ytop0 <- (ybottom0 + imageUAdj.y)

  c(xleft0, xright0, ybottom0, ytop0)
}


library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"), T)

imrot <- function(x, extent = c(0, ncol(x), 0, nrow(x)), interpolate = FALSE, add = FALSE, angle = 0, ...) {

  if (!add) {
    plot.new()
    plot.window(xlim = extent[1:2], ylim = extent[3:4], asp = 1)
  }
  ## use of par in here means plot must already be set up
   extent <- angle_extent(dim(x), extent, angle = angle)

  rasterImage(x, extent[1], extent[3], extent[2], extent[4], interpolate = interpolate, angle = angle, ...)
  invisible(extent)
}


library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"), T)
imrot(img)
imrot(img, angle = -90, add = TRUE)

image

should be able to unpick all the silly graphics device stuff and just use dimension and extent (and this possibly belongs in vaster not here)

@mdsumner
Copy link
Member Author

good fun stuff

library(whatarelief)
ex <- c(-1, 1, -1, 1) * 1e6
img <- imagery(extent = ex, projection = "+proj=laea +lon_0=147 +lat_0=-42")
imrot(img, extent = ex, angle = -90 - 45)
imrot(img, extent = ex, add = T)

(couldn't figure out alpha for a good example ...)

image

@mdsumner
Copy link
Member Author

alpha() drops dim

par(mfrow = c(1, 2), xpd = NA, mar = rep(0, 4))
library(whatarelief)
ex <- c(-1, 1, -1, 1) * 2e5
crs <- "+proj=laea +lon_0=147 +lat_0=-42 +type=crs"
cst <- coastline(extent = ex, projection = crs)
img <- imagery(extent = ex, projection = crs)

img2 <- img; img2[] <- scales::alpha(img2, 0.5)
imrot(img2, extent = ex, angle = -90 - 45)
lines(cst, col = "hotpink")
imrot(img, extent = ex, add = F)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant