Skip to content

Commit 71fae2d

Browse files
fix errors when plots and graphics are required
1 parent 7ccb65e commit 71fae2d

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

R/api_plot_raster.R

+40-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' @param main_title Main title for the plot
1515
#' @param rev Reverse the color palette?
1616
#' @param scale Scale to plot map (0.4 to 1.0)
17-
#'
17+
#' @param style Style for plotting continuous data
1818
#' @return A list of plot objects
1919
.plot_false_color <- function(tile,
2020
band,
@@ -25,7 +25,8 @@
2525
palette,
2626
main_title,
2727
rev,
28-
scale) {
28+
scale,
29+
style) {
2930
# select the file to be plotted
3031
bw_file <- .tile_path(tile, band, date)
3132
# size of data to be read
@@ -36,25 +37,38 @@
3637
band_scale <- .scale(band_conf)
3738
band_offset <- .offset(band_conf)
3839
max_value <- .max_value(band_conf)
39-
# used for SAR images without tiling system
40-
#if (tile[["tile"]] == "NoTilingSystem") {
41-
bw_file <- .gdal_warp_grd(bw_file, sizes)
42-
#}
40+
# retrieve the overview if COG
41+
bw_file <- .gdal_warp_grd(bw_file, sizes)
4342
# open the file in terra
4443
rast <- terra::rast(bw_file)
4544
# apply scale and offset
4645
rast <- rast * band_scale + band_offset
47-
# get the quantiles
46+
# extract the values
4847
vals <- terra::values(rast)
49-
quantiles <- stats::quantile(vals, probs = c(0, 0.05, 0.95, 1))
50-
maxq <- quantiles[[3]]
48+
# obtain the quantiles
49+
quantiles <- stats::quantile(
50+
vals,
51+
probs = c(0, 0.02, 0.98, 1),
52+
na.rm = TRUE
53+
)
54+
minv <- quantiles[[1]]
5155
minq <- quantiles[[2]]
56+
maxq <- quantiles[[3]]
5257
maxv <- quantiles[[4]]
53-
minv <- quantiles[[1]]
58+
# get the full range of values
5459
range <- maxv - minv
60+
# get the range btw 2% and 98%
5561
rangeq <- maxq - minq
62+
# calculate the stretch factor
5663
stretch <- rangeq / range
64+
# stretch the image
5765
rast <- stretch * (rast - minv) + minq
66+
67+
# readjust palette (bug in tmap?)
68+
if (minq < 0.0) {
69+
palette <- sub("-","",palette)
70+
}
71+
5872
# tmap params
5973
labels_size <- as.numeric(.conf("tmap", "graticules_labels_size"))
6074
legend_bg_color <- .conf("tmap", "legend_bg_color")
@@ -65,11 +79,10 @@
6579
# generate plot
6680
p <- tmap::tm_shape(rast, raster.downsample = FALSE) +
6781
tmap::tm_raster(
68-
palette = palette,
69-
title = band,
70-
style = "cont",
71-
style.args = list(na.rm = TRUE)
72-
82+
palette = palette,
83+
title = band,
84+
style = style,
85+
style.args = list(na.rm = TRUE)
7386
) +
7487
tmap::tm_graticules(
7588
labels.size = labels_size
@@ -143,7 +156,8 @@
143156
main_title = main_title,
144157
sf_seg = NULL,
145158
seg_color = NULL,
146-
line_width = NULL
159+
line_width = NULL,
160+
scale = scale
147161
)
148162
return(p)
149163
}
@@ -161,6 +175,7 @@
161175
#' @param sf_seg Segments (sf object)
162176
#' @param seg_color Color to use for segment borders
163177
#' @param line_width Line width to plot the segments boundary
178+
#' @param scale Scale to plot map (0.4 to 1.0)
164179
#' @return A plot object
165180
#'
166181
.plot_rgb <- function(tile,
@@ -171,7 +186,8 @@
171186
main_title,
172187
sf_seg,
173188
seg_color,
174-
line_width) {
189+
line_width,
190+
scale) {
175191
# get RGB files for the requested timeline
176192
red_file <- .tile_path(tile, red, date)
177193
green_file <- .tile_path(tile, green, date)
@@ -198,7 +214,8 @@
198214
main_title = main_title,
199215
sf_seg = sf_seg,
200216
seg_color = seg_color,
201-
line_width = line_width
217+
line_width = line_width,
218+
scale = scale
202219
)
203220
return(p)
204221
}
@@ -216,6 +233,7 @@
216233
#' @param sf_seg Segments (sf object)
217234
#' @param seg_color Color to use for segment borders
218235
#' @param line_width Line width to plot the segments boundary
236+
#' @param scale Scale to plot map (0.4 to 1.0)
219237
#' @return A plot object
220238
#'
221239
.plot_rgb_stars <- function(red_file,
@@ -226,7 +244,8 @@
226244
main_title,
227245
sf_seg,
228246
seg_color,
229-
line_width){
247+
line_width,
248+
scale) {
230249

231250
# read raster data as a stars object with separate RGB bands
232251
rgb_st <- stars::read_stars(
@@ -257,7 +276,8 @@
257276
tmap::tm_layout(
258277
main.title = main_title,
259278
main.title.size = 1,
260-
main.title.position = "center"
279+
main.title.position = "center",
280+
scale = scale
261281
) +
262282
tmap::tm_compass()
263283

R/sits_plot.R

+19-10
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ plot.predicted <- function(x, y, ...,
325325
#' @param palette An RColorBrewer palette
326326
#' @param rev Reverse the color order in the palette?
327327
#' @param scale Scale to plot map (0.4 to 1.0)
328+
#' @param style Style for plotting continuous objects
328329
#'
329330
#' @return A plot object with an RGB image
330331
#' or a B/W image on a color scale
@@ -359,7 +360,8 @@ plot.raster_cube <- function(x, ...,
359360
dates = NULL,
360361
palette = "RdYlGn",
361362
rev = FALSE,
362-
scale = 1.2) {
363+
scale = 1.0,
364+
style = "order") {
363365
# check caller
364366
.check_set_caller(".plot_raster_cube")
365367
# retrieve dots
@@ -381,7 +383,6 @@ plot.raster_cube <- function(x, ...,
381383
.check_require_packages("stars")
382384
# verifies if tmap package is installed
383385
.check_require_packages("tmap")
384-
.check_require_packages("tmaptools")
385386
if (.has(band)) {
386387
# check palette
387388
.check_palette(palette)
@@ -391,7 +392,7 @@ plot.raster_cube <- function(x, ...,
391392
# check scale parameter
392393
.check_num_parameter(scale, min = 0.2)
393394
# reverse the color palette?
394-
if (rev)
395+
if (rev || palette == "Greys")
395396
palette <- paste0("-", palette)
396397
# filter the tile to be processed
397398
tile <- .cube_filter_tiles(cube = x, tiles = tile)
@@ -441,11 +442,13 @@ plot.raster_cube <- function(x, ...,
441442
palette = palette,
442443
main_title = main_title,
443444
rev = rev,
444-
scale = scale
445+
scale = scale,
446+
style = style
445447
)
446448
} else {
447449
# plot RGB
448-
main_title <- paste0(.tile_collection(tile)," ",
450+
main_title <- paste0(.tile_satellite(tile)," ",
451+
tile[["tile"]], " ",
449452
red, "(R) ",
450453
green, "(G) ",
451454
blue, "(B) ",
@@ -460,7 +463,8 @@ plot.raster_cube <- function(x, ...,
460463
main_title = main_title,
461464
sf_seg = NULL,
462465
seg_color = NULL,
463-
line_width = NULL
466+
line_width = NULL,
467+
scale = scale
464468
)
465469
}
466470

@@ -484,7 +488,8 @@ plot.raster_cube <- function(x, ...,
484488
#' @param line_width Line width to plot the segments boundary (in pixels)
485489
#' @param palette An RColorBrewer palette
486490
#' @param rev Reverse the color order in the palette?
487-
#' @param scale Scale to plot map (0.4 to 1.0)
491+
#' @param scale Scale to plot map (0.4 to 1.5)
492+
#' @param style Style for plotting continuous objects
488493
#'
489494
#' @return A plot object with an RGB image
490495
#' or a B/W image on a color
@@ -522,7 +527,8 @@ plot.vector_cube <- function(x, ...,
522527
line_width = 1,
523528
palette = "RdYlGn",
524529
rev = FALSE,
525-
scale = 1.0) {
530+
scale = 1.0,
531+
style = "order") {
526532
.check_set_caller(".plot_vector_cube")
527533
# retrieve dots
528534
dots <- list(...)
@@ -570,10 +576,12 @@ plot.vector_cube <- function(x, ...,
570576
palette = palette,
571577
main_title = main_title,
572578
rev = rev,
573-
scale = scale
579+
scale = scale,
580+
style = style
574581
)
575582
} else {
576583
main_title <- paste0(.tile_collection(tile)," ",
584+
tile[["tile"]],
577585
red, "(R) ",
578586
green, "(G) ",
579587
blue, "(B) ",
@@ -589,7 +597,8 @@ plot.vector_cube <- function(x, ...,
589597
main_title = main_title,
590598
sf_seg = sf_seg,
591599
seg_color = seg_color,
592-
line_width = line_width
600+
line_width = line_width,
601+
scale = scale
593602
)
594603
}
595604
return(p)

man/plot.raster_cube.Rd

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot.vector_cube.Rd

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)