Closed CamiloEr closed 2 years ago
Are you using the lastest github version of tmap? Could you share this raster dataset, or reproduce this example with another dataset?
The package was reinstalled from github and here I use a better fully reproducible example with Session Info.
library(tmap)
library(raster)
#> Loading required package: sp
#3 Layers with range 0-1
r <- raster(nrow=3, ncol=3)
set.seed(1)
s1 <- lapply(1:3, function(i) setValues(r, runif(ncell(r), 0, 1)))
x1 <- brick(s1)
plot(x1)
plotRGB(x1, r=1, g=2, b=3, stretch="lin") #Useful option for image enhancement
plotRGB(x1, r=1, g=2, b=3, scale=1)
tm_shape(x1)+
tm_rgb(interpolate = FALSE, max.value = 1) #With max.value the problem of range 0-1 is resolved.
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
#More than 3 layers with range 0-1
set.seed(1)
s2 <- lapply(1:5, function(i) setValues(r, runif(ncell(r), 0, 1))) #5 layers
x2 <- brick(s2)
plot(x2)
plotRGB(x2, r=1, g=2, b=3, scale=1)
plotRGB(x2, r=4, g=2, b=1, scale=1) #Useful option for false color images
tm_shape(x2)+
tm_rgb(interpolate = FALSE, max.value = 1) #With more than 3 layers the problem persist.
#> Error: Raster object does not have a color table, nor numeric data that can be converted to colors. Use tm_raster to visualize the data.
tm_shape(x2[[1:3]])+
tm_rgb(interpolate = FALSE, max.value = 1) #It works for a subset of layers
tm_shape(x2[[1:4]])+
tm_rgb(interpolate = FALSE, max.value = 1) #Strange result
tm_shape(x2[[c(4,2,1)]])+ #Workaround for false color images
tm_rgb(interpolate = FALSE, max.value = 1)
Created on 2019-04-25 by the reprex package (v0.2.1)
In the code I highlight two useful options that have raster::plotRGB and that could be useful in tmap: False Color Images and Image Enhancement . Some references: http://gsp.humboldt.edu/OLM/Courses/GSP_216_Online/lesson3-1/composites.html https://landsat.usgs.gov/what-are-band-designations-landsat-satellites https://www.nrcan.gc.ca/earth-sciences/geomatics/satellite-imagery-air-photos/satellite-imagery-products/educational-resources/9389
Thanks for your input!
How it currently works:
tm_rgb
it should take the first three layers. So this can be improved.r
, g
, b
and scale
, but maybe there are better alternatives.Thank you for clarifying how this function works. Now everything is clearer. Regarding the other matters:
raster
package use (ej. https://github.com/cran/raster/blob/master/R/stretch.R) are improved from this code: https://spatiallyexplicit.wordpress.com/2011/06/07/crop-circles/ These are the basic ones but I will explore others and will gladly share them by opening a new issue.
RStoolboox
package has functions that help in this regard (eg. getMeta()
) http://bleutner.github.io/RStoolbox/rstbx-docu/). So, because the configurations of the bands are so different beteween sensors, even between satellites of the same program (eg. Landsat7 with r = 3, g = 2 b = 1 and Landsat8 with r = 4, g = 3 b = 2, or MODIS with r = 1, g = 4 b = 3), the most direct way to indicate the position of the bands is through the arguments of the function (similar to raster::plotRGB(r,g,b)
). The option r = 1, g = 2, b = 3 could be left as the default (this works on satellites such as Planet's RapidEye, for example). In addition, this arguments could be used to subset the rasterbrick (eg. brick[[r]]) to avoid the problem of using more than 4 layers.
NULL
to avoid confusions like the one I had.I hope that these contributions have helped and feel free to close this issue when you consider it necessary. My doubts have already been resolved.
Ok, thx!
I have made a start with the channels (https://github.com/mtennekes/tmap/commit/0002f6a5f5a89a552b46fb9edb76176c8362a6c8). I added tm_rgba
which uses an alpha channel, so that tm_rgb
is by default without alpha channel. The errors, warnings, and messages have already been improved.
Let me know if it works correctly. Are there any nice examples that we can add to the documentation, e.g. a small and accessible satellite image?
Works perfect! :tada:
Here is an example from a data set that I borrowed from RStoolbox
package. I also used that package to prove that everything works as expected. The Landsat images are free under subscription on https://earthexplorer.usgs.gov/ in case you don't want to use the RStoolbox
data set (or I can send you another similar subset of an area of interest used for other shapes examples in tmap
).
library(RStoolbox)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
library(tmap)
library(raster)
#> Loading required package: sp
data(lsat, package="RStoolbox")
(Landsat5 <- lsat) #It has 7 bands, e.g. B1_dn=blue, B2_dn=green, B3_dn=red, B4_dn=NIR
#> class : RasterBrick
#> dimensions : 310, 287, 88970, 7 (nrow, ncol, ncell, nlayers)
#> resolution : 30, 30 (x, y)
#> extent : 619395, 628005, -419505, -410205 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=utm +zone=22 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
#> data source : in memory
#> names : B1_dn, B2_dn, B3_dn, B4_dn, B5_dn, B6_dn, B7_dn
#> min values : 54, 18, 11, 4, 2, 131, 1
#> max values : 185, 87, 92, 127, 148, 146, 79
RStoolbox::ggRGB(Landsat5) #They use r = 3, g = 2, b = 1 as default
tm_shape(Landsat5)+
tm_rgb(r = 3, g = 2, b = 1)
#This example is useful to see the utility of a image enhancement option:
RStoolbox::ggRGB(Landsat5, stretch = "lin") #They have other image enhancement options too ('sqrt' or 'log')
(Landsat5_linstretch <- raster::stretch(Landsat5, minq=0.02, maxq=0.98))
#> class : RasterBrick
#> dimensions : 310, 287, 88970, 7 (nrow, ncol, ncell, nlayers)
#> resolution : 30, 30 (x, y)
#> extent : 619395, 628005, -419505, -410205 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=utm +zone=22 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
#> data source : in memory
#> names : B1_dn, B2_dn, B3_dn, B4_dn, B5_dn, B6_dn, B7_dn
#> min values : 0, 0, 0, 0, 0, 0, 0
#> max values : 255, 255, 255, 255, 255, 255, 255
tm_shape(Landsat5_linstretch)+ #Works perfect!
tm_rgb(r = 3, g = 2, b = 1)
#False color to highlight vegetation (in red) using B4_dn=NIR
tm_shape(Landsat5_linstretch)+ #Great job, thanks!
tm_rgb(r = 4, g = 3, b = 2)
Created on 2019-05-05 by the reprex package (v0.2.1)
Thx @CamiloEr
Yes, I am looking for such an example for the documentation without having to suggest another package. Is there a url somewhere which directly refers to such an object? We could also include such an object in tmap. Saved as rda object with compres = "xz"
, Landsat5
occupies 280Kb. However, it will only be used in tm_rgb
since all other datasets in tmap
cover much larger areas.
Regarding the stretch
function. I thought about embedding it in tmap
, but I think this is not a very good idea since the long term plan is to replace raster
by stars
. Probably better to keep this function as is. We could also show how it works in the examples, since tmap
still imports raster
.
Cleaning up all issues that are not relevant for the first release of tmap v4.
I'am preprocessing a Landsat7 Surface Reflectance image: I loaded all bands with
raster::stack
, applied a factor scale to each band (1e-04) and removed values out the 0-1 range (saturated ones).I want to plot this image with
tmap::tm_rgb
but it shows me some errors (withraster::plotRGB
it works). The first one is because theRasterBrick
has more than 3 bands, I suppose? And the second one is because the range values of each band are 0-1 and not 0-255?.Here is a (partially) reproducible example:
Created on 2019-04-03 by the reprex package (v0.2.1)
Two more questions come in mind: how can I specify which band is red, green o blue in the funcion (ej.
plotRGB(., r=3, g=2, b=1)
) and there is a possibility to streching the image (plotRGB(.stretch=c("hist","lin")
) ?. I read documentation, vignettes and some questions on SO, but found little information about it.(P.S. reprex package is awesome and tmap too!)