Skip to content

Commit d217077

Browse files
improvements to view SOM maps
1 parent c4097b8 commit d217077

9 files changed

+134
-31
lines changed

R/api_conf.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -1283,12 +1283,13 @@ NULL
12831283
base_groups = base_groups,
12841284
overlay_groups = vector()
12851285
)
1286-
class(sits_leaflet) <- "sits_leaflet"
12871286
# put the object in the global sits environment
12881287
sits_env[["leaflet"]] <- sits_leaflet
12891288

12901289
# create a global object for controlling leaflet false color legend
12911290
sits_env[["leaflet_false_color_legend"]] <- FALSE
1291+
# create a global object for controlling leaflet SOM neuron color display
1292+
sits_env[["leaflet_som_colors"]] <- FALSE
12921293
return(invisible(sits_leaflet))
12931294
}
12941295
#' @title Clean global leaflet

R/api_view.R

+83-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@
5555
#' @param group Leaflet group to be added
5656
#' @param legend Named vector that associates labels to colors.
5757
#' @param palette Palette provided in the configuration file.
58+
#' @param radius Radius of circle markers
5859
#' @return A leaflet object
5960
#'
6061
.view_samples <- function(leaf_map, samples, group,
61-
legend, palette) {
62+
legend, palette, radius) {
6263
.check_set_caller(".view_samples")
6364
# first select unique locations
6465
samples <- dplyr::distinct(
@@ -100,7 +101,7 @@
100101
leaflet::addCircleMarkers(
101102
data = samples,
102103
color = ~ factpal(label),
103-
radius = 4,
104+
radius = radius,
104105
stroke = FALSE,
105106
fillOpacity = 1,
106107
group = group
@@ -121,6 +122,86 @@
121122
}
122123
return(leaf_map)
123124
}
125+
#' @title Visualize a set of neurons
126+
#' @name .view_neurons
127+
#' @keywords internal
128+
#' @noRd
129+
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
130+
#'
131+
#' @param leaf_map Leaflet map
132+
#' @param samples Data.frame with columns "longitude", "latitude"
133+
#' and "label"
134+
#' @param labels Labels to display
135+
#' @param group Leaflet group to be added
136+
#' @param legend Named vector that associates labels to colors.
137+
#' @param palette Palette provided in the configuration file.
138+
#' @param radius Radius of circle markers
139+
#' @return A leaflet object
140+
#'
141+
.view_neurons <- function(leaf_map, samples, labels, group,
142+
legend, palette, radius) {
143+
.check_set_caller(".view_neurons")
144+
# first select unique locations
145+
samples <- dplyr::distinct(
146+
samples,
147+
.data[["longitude"]],
148+
.data[["latitude"]],
149+
.data[["label"]]
150+
)
151+
# convert tibble to sf
152+
samples <- sf::st_as_sf(
153+
samples[c("longitude", "latitude", "label")],
154+
coords = c("longitude", "latitude"),
155+
crs = "EPSG:4326"
156+
)
157+
# get the bounding box
158+
samples_bbox <- sf::st_bbox(samples)
159+
# get colors
160+
colors <- .colors_get(
161+
labels = labels,
162+
legend = legend,
163+
palette = palette,
164+
rev = TRUE
165+
)
166+
# create a palette of colors
167+
factpal <- leaflet::colorFactor(
168+
palette = colors,
169+
domain = labels
170+
)
171+
# add samples to leaflet
172+
leaf_map <- leaf_map |>
173+
leaflet::flyToBounds(
174+
lng1 = samples_bbox[["xmin"]],
175+
lat1 = samples_bbox[["ymin"]],
176+
lng2 = samples_bbox[["xmax"]],
177+
lat2 = samples_bbox[["ymax"]]
178+
) |>
179+
leaflet::addCircleMarkers(
180+
data = samples,
181+
color = ~ factpal(label),
182+
radius = radius,
183+
stroke = FALSE,
184+
fillOpacity = 1,
185+
group = group
186+
)
187+
# recover overlay groups
188+
overlay_groups <- sits_env[["leaflet"]][["overlay_groups"]]
189+
# add legend if it does not exist already
190+
if (!any(grepl("samples", overlay_groups)) &&
191+
!any(grepl("class", overlay_groups)) &&
192+
!sits_env[["leaflet_som_colors"]]) {
193+
leaf_map <- leaf_map |>
194+
leaflet::addLegend(
195+
position = "topright",
196+
pal = factpal,
197+
values = labels,
198+
title = "Classes",
199+
opacity = 1
200+
)
201+
sits_env[["leaflet_som_colors"]] <- TRUE
202+
}
203+
return(leaf_map)
204+
}
124205
#' @title Include leaflet to view segments
125206
#' @name .view_segments
126207
#' @keywords internal

R/sits_view.R

+28-16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#' @param last_quantile Last quantile for stretching images
8585
#' @param leaflet_megabytes Maximum size for leaflet (in MB)
8686
#' @param id_neurons Neurons from the SOM map to be shown.
87+
#' @param radius Radius of circle markers
8788
#' @param add Add image to current leaflet
8889
#'
8990
#' @return A leaflet object containing either samples or
@@ -150,6 +151,7 @@ sits_view <- function(x, ...) {
150151
sits_view.sits <- function(x, ...,
151152
legend = NULL,
152153
palette = "Set3",
154+
radius = 5,
153155
add = FALSE) {
154156
.check_set_caller("sits_view_sits")
155157
# precondition
@@ -175,7 +177,8 @@ sits_view.sits <- function(x, ...,
175177
samples = x,
176178
group = "samples",
177179
legend = legend,
178-
palette = palette
180+
palette = palette,
181+
radius = radius
179182
)
180183
# append samples to overlay groups
181184
overlay_groups <- append(overlay_groups, "samples")
@@ -204,6 +207,7 @@ sits_view.som_map <- function(x, ...,
204207
id_neurons,
205208
legend = NULL,
206209
palette = "Harmonic",
210+
radius = 5,
207211
add = FALSE) {
208212
.check_set_caller("sits_view_som_map")
209213
# check id_neuron
@@ -222,22 +226,30 @@ sits_view.som_map <- function(x, ...,
222226
overlay_groups <- sits_env[["leaflet"]][["overlay_groups"]]
223227
leaf_map <- sits_env[["leaflet"]][["leaf_map"]]
224228

225-
# assign group name
226-
group <- paste("neurons", paste(id_neurons, collapse = " "))
229+
# get the samples
230+
samples <- x[["data"]]
231+
labels <- sort(unique(samples[["label"]]))
227232

228-
# first select unique locations
229-
samples <- dplyr::filter(
230-
x[["data"]], .data[["id_neuron"]] %in% !!id_neurons
231-
)
232-
leaf_map <- leaf_map |>
233-
.view_samples(
234-
samples = samples,
235-
group = group,
236-
legend = legend,
237-
palette = palette
233+
for (id in id_neurons) {
234+
# assign group name (one neuron per)
235+
group <- paste("neuron", id)
236+
237+
# first select unique locations
238+
samples_neuron <- dplyr::filter(
239+
samples, .data[["id_neuron"]] == id
238240
)
239-
# append samples to overlay groups
240-
overlay_groups <- append(overlay_groups, group)
241+
leaf_map <- leaf_map |>
242+
.view_neurons(
243+
samples = samples_neuron,
244+
labels = labels,
245+
group = group,
246+
legend = legend,
247+
palette = palette,
248+
radius = radius
249+
)
250+
# append samples to overlay groups
251+
overlay_groups <- append(overlay_groups, group)
252+
}
241253
# add layers control and update global leaflet-related variables
242254
leaf_map <- leaf_map |>
243255
.view_add_layers_control(overlay_groups) |>
@@ -263,7 +275,7 @@ sits_view.raster_cube <- function(x, ...,
263275
max_cog_size = 2048,
264276
first_quantile = 0.02,
265277
last_quantile = 0.98,
266-
leaflet_megabytes = 32,
278+
leaflet_megabytes = 64,
267279
add = FALSE) {
268280
# set caller for errors
269281
.check_set_caller("sits_view_raster_cube")

man/plot.probs_cube.Rd

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

man/plot.probs_vector_cube.Rd

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

man/plot.uncertainty_cube.Rd

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

man/plot.variance_cube.Rd

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

man/sits_segment.Rd

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

man/sits_view.Rd

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

0 commit comments

Comments
 (0)