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

ggRGB unable to plot masked image #118

Closed
bappa10085 opened this issue Jan 5, 2025 · 4 comments
Closed

ggRGB unable to plot masked image #118

bappa10085 opened this issue Jan 5, 2025 · 4 comments

Comments

@bappa10085
Copy link

bappa10085 commented Jan 5, 2025

ggRGB unable to plot the masked image. Here is a minimal, reproducible example

library(terra)
library(ggplot2)
library(RStoolbox)

lsat

plotRGB(lsat, r = 3, g = 2, b = 1, stretch = "hist")


# SpatVector as standalone object
m <- structure(list(geom = "POLYGON ((620947.959184 -412745.345369, 621312.951334 -414249.11303, 623254.709576 -414526.507064, 623897.095761 -413154.136578, 621152.354788 -411767.166405, 620947.959184 -412745.345369))"),
               class = "data.frame",
               row.names = c(NA, -1L)
)

# To SpatVector with the right CRS
m <- vect(m$geom)
crs(m) <- crs(lsat)

plot(m, add = TRUE, col = "red")

lsat.masked <- crop(lsat, m, mask = T)

plotRGB(lsat.masked, r = 3, g = 2, b = 1, stretch = "hist")

ggplot() +
  ggRGB(img = lsat.masked,
    r = 3,
    g = 2,
    b = 1,
    stretch = "hist",
    ggLayer = T)

Image

It returns the following error

Error in grid.Call.graphics(C_raster, x$raster, x$x, x$y, x$width, x$height, :
cannot allocate memory block of size 67108864 Tb
In addition: Warning message:
In matrix(z, nrow = nrow(rr), ncol = ncol(rr), byrow = TRUE) :
data length [5888] is not a sub-multiple or multiple of the number of rows [88]

@KonstiDE
Copy link
Collaborator

KonstiDE commented Feb 2, 2025

Hey,

unfortunately, I am not really able to reproduce the error. For me, already the crop call yields [crop] cannot crop a SpatRaster with an empty extent. Furthermore, the polygon you create has no CRS, valid extent whatsoever?

I recommend getting the last.rds from the inst/external directory path, save it as tif, load it into qgis and draw your desired polygon there. Then export back into R crop.

Would that be a solution?

@bappa10085
Copy link
Author

I have edited the code to make the error reproducible. Please have a look.

@KonstiDE
Copy link
Collaborator

KonstiDE commented Feb 4, 2025

Interestingly, I still cant reporduce the following part of the error:

Error in grid.Call.graphics(C_raster, x$raster, x$x, x$y, x$width, x$height, :
cannot allocate memory block of size 67108864 Tb

Anyhow, you seem to be correct, I'll look into the error this week or next weekend. For now, just do not plot the RGB as a ggLayer, but as a base ggplot using the following snippet (also do not forget to set geom_raster = T):

library(terra)
library(ggplot2)
library(RStoolbox)

lsat

plotRGB(lsat, r = 3, g = 2, b = 1, stretch = "hist")


# SpatVector as standalone object
m <- structure(
  list(geom = "POLYGON ((620947.959184 -412745.345369, 621312.951334 -414249.11303, 623254.709576 -414526.507064, 623897.095761 -413154.136578, 621152.354788 -411767.166405, 620947.959184 -412745.345369))"),
  class = "data.frame",
  row.names = c(NA, -1L)
)

# To SpatVector with the right CRS
m <- vect(m$geom, crs = crs(lsat))

plot(m, add = TRUE, col = "red")

lsat.masked <- crop(lsat, m, mask = T)

plotRGB(lsat.masked, r = 3, g = 2, b = 1, stretch = "hist")

ggRGB(img = lsat.masked, r = 3, g = 2, b = 1, stretch = "hist", geom_raster = T) + 
   geom_whatever()

@KonstiDE KonstiDE added bug and removed bug labels Feb 4, 2025
@KonstiDE
Copy link
Collaborator

KonstiDE commented Feb 6, 2025

Hi, soooo this isn't actually a bug. We are just missing scale_fill_identity(). In ggRGB() the scale_fill_identity() is only returned from the method if ggLayer is false and a whole complete ggplot() build up.

When trying to return the scale_fill_identity() property without a ggplot() directly appended, a ggproto type of object is created which somehow cannot be passed back to the ggplot() + call where is replaces ggRGB(). You should be good to go with the following snippet:

library(terra)
library(ggplot2)
library(RStoolbox)

lsat

plotRGB(lsat, r = 3, g = 2, b = 1, stretch = "hist")


# SpatVector as standalone object
m <- structure(
  list(geom = "POLYGON ((620947.959184 -412745.345369, 621312.951334 -414249.11303, 623254.709576 -414526.507064, 623897.095761 -413154.136578, 621152.354788 -411767.166405, 620947.959184 -412745.345369))"),
  class = "data.frame",
  row.names = c(NA, -1L)
)

# To SpatVector with the right CRS
m <- vect(m$geom, crs = crs(lsat))

plot(m, add = TRUE, col = "red")

lsat.masked <- crop(lsat, m, mask = T)

plotRGB(lsat.masked, r = 3, g = 2, b = 1, stretch = "hist")

ggplot() + 
   ggRGB(img = lsat.masked, r = 3, g = 2, b = 1, stretch = "hist", geom_raster = T, ggLayer = T) + 
   scale_fill_identity()

If this does not work for you, please reopen this issue. Imma close now :)

@KonstiDE KonstiDE closed this as completed Feb 6, 2025
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

2 participants