-
Notifications
You must be signed in to change notification settings - Fork 94
-
Notifications
You must be signed in to change notification settings - Fork 94
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
Does stars have an eqivalent way to create RGB rasters from arrays like raster::brick? #302
Comments
edit: added ggplot2 and palr to reprex. |
edit: accidently wrote |
I don't think that Since your data seem not to be spatially referenced, I'd suggest to try your luck with one of the R packages dedicated to image analysis or image handling. Otherwise, |
The data are spatially referenced. I know the extent and CRS, I just didn't include that for the reprex. Full context is here:https://github.com/anthonynorth/snapbox/blob/master/R/static_map.R#L27 Ultimately what I want to do is ggplot over the top of this thing. So right now I can't use |
Looks like my autodetection for "RGB" for stars images is not working...I think I based it off of what one gets when reading using https://github.com/paleolimbot/ggspatial/blob/master/R/layer-spatial-stars.R#L23-L24 If you set the dimension names, it works but is incorrect... s_tile <- st_as_stars(img) %>%
st_set_dimensions(names = c("x", "y", "band")) It's not standalone, but you could rig your own Geom based on what I use to plot OSM tiles: https://github.com/paleolimbot/ggspatial/blob/master/R/annotation-map-tile.R#L157-L199 |
But |
Yes so if I write my image to PNG and read it back with
Is this an RGB raster? |
No, it is a three-band raster dataset. The semantics that band 1, 2 and 3 refer to R, G and B values in 0-255 is lost. |
How do you btw get |
Oh sorry. I assigned the crs after reading from png file. :) |
Instead of moving the library(stars)
s = st_as_stars(img)
r = st_apply(s, 1:2, function(x) rgb(x[1], x[2], x[3], maxColorValue=255)) has the colors in the proper places; supporting this by plot.stars and geom_stars is pretty straightforward. |
|
Here is an example how things look for a 4-dimensional object, using library(stars)
# Loading required package: abind
# Loading required package: sf
# Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x = read_stars(tif)
x1 = x[,,,1:3]
x2 = x[,,,4:6]
st_dimensions(x1) = st_dimensions(x2)
(xx = c(x1, x2, along = "foo"))
# stars object with 4 dimensions and 1 attribute
# attribute(s):
# L7_ETMs.tif
# Min. : 1.00
# 1st Qu.: 54.00
# Median : 69.00
# Mean : 68.91
# 3rd Qu.: 86.00
# Max. :255.00
# dimension(s):
# from to offset delta refsys point values
# x 1 349 288776 28.5 UTM Zone 25, Southern Hem... FALSE NULL [x]
# y 1 352 9120761 -28.5 UTM Zone 25, Southern Hem... FALSE NULL [y]
# band 4 6 NA NA NA NA NULL
# foo 1 2 NA NA NA NA NULL
r = st_rgb(xx, 3)
plot(r) |
fortunes::fortune("illogical") Indeed, it matters how your matrix (or array) should be interpreted in terms of a (spatially oriented) image. You didn't tell. |
@MilesMcBain : swapping dimensions 1 and 2 and then flipping the second (y) gives you r = st_as_stars(aperm(img, c(2,1,3))[,18:1,])
ggplot()+geom_stars(data=st_rgb(r))+scale_fill_identity() |
Feel free to re-open for follow-up issues! |
Here's my reprex:
The text was updated successfully, but these errors were encountered: