Skip to content

Commit 5cacd19

Browse files
committed
Merge branch 'dev' of https://github.com/e-sensing/sits into feat/impute-methods
2 parents d9cb0aa + 513aa15 commit 5cacd19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1288
-288
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Imports:
6565
tibble (>= 3.3.0),
6666
tidyr (>= 1.3.0),
6767
tmap (>= 4.1),
68-
torch (>= 0.15.0),
68+
torch (>= 0.16.3),
6969
units,
7070
utils
7171
Suggests:
@@ -97,6 +97,7 @@ Suggests:
9797
RColorBrewer,
9898
RcppArmadillo (>= 14.0.0),
9999
scales,
100+
snic,
100101
spdep,
101102
stars,
102103
stringr,

NAMESPACE

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ S3method("sits_bands<-",default)
77
S3method("sits_bands<-",raster_cube)
88
S3method("sits_bands<-",sits)
99
S3method("sits_labels<-",class_cube)
10+
S3method("sits_labels<-",class_vector_cube)
1011
S3method("sits_labels<-",default)
1112
S3method("sits_labels<-",probs_cube)
13+
S3method("sits_labels<-",probs_vector_cube)
1214
S3method("sits_labels<-",sits)
1315
S3method(.accuracy_get_validation,csv)
1416
S3method(.accuracy_get_validation,data.frame)
@@ -21,6 +23,9 @@ S3method(.check_samples,default)
2123
S3method(.check_samples,sits)
2224
S3method(.cube_adjust_crs,default)
2325
S3method(.cube_adjust_crs,grd_cube)
26+
S3method(.cube_area_freq,class_cube)
27+
S3method(.cube_area_freq,class_vector_cube)
28+
S3method(.cube_area_freq,default)
2429
S3method(.cube_as_sf,default)
2530
S3method(.cube_as_sf,raster_cube)
2631
S3method(.cube_bands,default)
@@ -143,6 +148,10 @@ S3method(.samples_select_bands,sits_base)
143148
S3method(.samples_select_dates,sits)
144149
S3method(.samples_select_labels,sits)
145150
S3method(.slice_dfr,numeric)
151+
S3method(.snic_grid_seeds,diamond)
152+
S3method(.snic_grid_seeds,hexagonal)
153+
S3method(.snic_grid_seeds,random)
154+
S3method(.snic_grid_seeds,rectangular)
146155
S3method(.source_collection_access_test,"mpc_cube_sentinel-1-grd")
147156
S3method(.source_collection_access_test,cdse_cube)
148157
S3method(.source_collection_access_test,cdse_os_cube)
@@ -391,6 +400,8 @@ S3method(sits_clean,class_cube)
391400
S3method(sits_clean,default)
392401
S3method(sits_clean,derived_cube)
393402
S3method(sits_clean,raster_cube)
403+
S3method(sits_colors_qgis,class_cube)
404+
S3method(sits_colors_qgis,class_vector_cube)
394405
S3method(sits_combine_predictions,average)
395406
S3method(sits_combine_predictions,default)
396407
S3method(sits_combine_predictions,uncertainty)
@@ -549,7 +560,6 @@ export(sits_labels_summary)
549560
export(sits_lightgbm)
550561
export(sits_lighttae)
551562
export(sits_list_collections)
552-
export(sits_lstm_fcn)
553563
export(sits_merge)
554564
export(sits_mgrs_to_roi)
555565
export(sits_mixture_model)
@@ -580,6 +590,7 @@ export(sits_sgolay)
580590
export(sits_show_prediction)
581591
export(sits_slic)
582592
export(sits_smooth)
593+
export(sits_snic)
583594
export(sits_som_clean_samples)
584595
export(sits_som_evaluate_cluster)
585596
export(sits_som_map)

R/api_bbox.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,25 @@ NULL
233233
proj4string <- crs_sf[["proj4string"]]
234234
proj4string
235235
}
236-
236+
#' @title Verify if CRS is equal area
237+
#' @name .crs_is_equal_area
238+
#' @noRd
239+
#' @param wkt_crs CRS in WKT name
240+
#' @returns CRS in PROJ4 name
241+
.crs_is_equal_area <- function(crs) {
242+
# Transform CRS to Sf object
243+
proj4_string <- sf::st_crs(crs)$proj4string
244+
# Define equal area codes
245+
equal_area_codes <- c("aea", "laea", "cea", "moll", "sinu", "robin", "vandg")
246+
# Verify if CRS is equal area
247+
is_equal_area <- any(sapply(equal_area_codes, function(code)
248+
grepl(
249+
paste0("\\+proj=", code), proj4_string
250+
))
251+
)
252+
# Return!
253+
return(is_equal_area)
254+
}
237255
#' @title Check if CRS is WGS84
238256
#' @name .is_crs_wgs84
239257
#' @noRd

R/api_check.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,3 +2954,21 @@
29542954
)
29552955
)
29562956
}
2957+
#' @title Check if grid system is supported
2958+
#' @name .check_snic_grid
2959+
#' @author Rolf Simoes, \email{rolf.simoes@@gmail.com}
2960+
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
2961+
#' @param snic_grid Requested SNIC grid system
2962+
#' @return Called for side effects.
2963+
#' @keywords internal
2964+
#' @noRd
2965+
.check_snic_grid <- function(snic_grid) {
2966+
.check_chr_contains(
2967+
x = .conf("snic_grids"),
2968+
contains = snic_grid,
2969+
case_sensitive = TRUE,
2970+
discriminator = "one_of",
2971+
can_repeat = FALSE,
2972+
msg = .conf("messages", ".check_snic_grid")
2973+
)
2974+
}

R/api_colors.R

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
)
142142
}
143143
#'
144-
#' @title Write a color table in QGIS Style format
144+
#' @title Write a raster color table in QGIS Style format
145145
#' @name .colors_qml
146146
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
147147
#' @keywords internal
@@ -154,7 +154,6 @@
154154
.check_chr_contains(colnames(color_table), c("index", "color", "name"))
155155
# open the file connection
156156
con <- file(file, "w")
157-
158157
# read the top part of QGIS style
159158
top_qgis_style <- system.file("extdata/qgis/qgis_style_top.xml",
160159
package = "sits"
@@ -198,6 +197,105 @@
198197
# close the file
199198
on.exit(close(con))
200199
}
200+
#'
201+
#' @title Write a vector color table in GIMP Format to be read by QGIS
202+
#' @name .colors_vector_qml
203+
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
204+
#' @keywords internal
205+
#' @noRd
206+
#' @param color_table color table to write to QGIS
207+
#' @param file the file name to be written to.
208+
#' @return Called for side effects.
209+
.colors_vector_qml <- function(color_table, file) {
210+
# open the file connection
211+
con <- file(file, "w")
212+
# find out how many labels have to be written
213+
nlabels <- nrow(color_table)
214+
# read the top part of QGIS style
215+
top_qgis_style <- system.file("extdata/qgis/qgis_style_vector_top.xml",
216+
package = "sits"
217+
)
218+
top_lines <- readLines(top_qgis_style)
219+
# write the top part of QGIS style in the output file
220+
writeLines(top_lines, con = con)
221+
# write the start of the categories
222+
writeLines("<categories>", con = con)
223+
# write the categories
224+
ncategories <- nlabels - 1
225+
for (i in 0:ncategories) {
226+
color_name <- color_table[i + 1, "name"]
227+
qml_label <- paste0("label=","\"", color_name, "\"")
228+
qml_value <- paste0("value=", "\"", color_name, "\"")
229+
qml_symbol <- paste0("symbol=","\"", as.character(i), "\"")
230+
writeLines(paste("<category render=\"true\"",
231+
qml_label,
232+
"type=\"string\"",
233+
qml_value,
234+
qml_symbol,
235+
"/>"),
236+
con)
237+
}
238+
# write the last line of categories
239+
qml_symbol_extra <- paste0("\"", as.character(nlabels), "\"")
240+
writeLines(paste("<category render=\"false\"",
241+
"label= \"\"",
242+
"type=\"string\"",
243+
"value= \"\"",
244+
"symbol=", qml_symbol_extra, "/>"),
245+
con)
246+
247+
# write the end of the categories
248+
writeLines("</categories>", con = con)
249+
250+
# write the symbols
251+
writeLines("<symbols>", con = con)
252+
for (i in 0:ncategories) {
253+
qml_name <- paste0("name=","\"", as.character(i), "\"")
254+
writeLines(paste("<symbol",
255+
"alpha=\"1\"",
256+
"type=\"fill\"",
257+
qml_name,
258+
">"),
259+
con)
260+
# read data
261+
data_def <- system.file("extdata/qgis/qgis_data_defined_properties.xml",
262+
package = "sits"
263+
)
264+
data_def_lines <- readLines(data_def)
265+
# write the top part of QGIS style in the output file
266+
writeLines(data_def_lines, con = con)
267+
# write the color values
268+
writeLines("<layer enabled=\"1\" class=\"SimpleFill\">",
269+
con)
270+
writeLines("<Option type=\"Map\">", con)
271+
# convert color to RGB and write to XML
272+
hex_color <- color_table[i + 1, "color"]
273+
rgb <- col2rgb(hex_color)
274+
# format RGBA value to be inserted in QML file
275+
color_val <- paste0(paste(as.character(rgb), collapse = ","),",255")
276+
qml_color <- paste0("value=", "\"",color_val,"\"")
277+
# write QML color
278+
writeLines(paste("<Option type=\"QString\"",
279+
qml_color, "name=\"color\"/>"),
280+
con)
281+
writeLines("<Option type=\"QString\" value=\"solid\" name=\"style\"/>",
282+
con)
283+
writeLines("</Option>", con)
284+
writeLines("</layer>", con)
285+
writeLines("</symbol>", con)
286+
}
287+
writeLines("</symbols>", con)
288+
# read the bottom part of QGIS style
289+
bottom_qgis_style <- system.file("extdata/qgis/qgis_style_vector_bottom.xml",
290+
package = "sits"
291+
)
292+
bottom_lines <- readLines(bottom_qgis_style)
293+
# write the bottom part of QGIS style in the output file
294+
writeLines(bottom_lines, con = con)
295+
# close the file
296+
on.exit(close(con))
297+
}
298+
201299
#' @title Transform an RColorBrewer name to cols4all name
202300
#' @name .colors_cols4all_name
203301
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}

R/api_cube.R

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,19 +367,13 @@ NULL
367367
#'
368368
#' @return A \code{vector} with the areas of the cube labels.
369369
.cube_class_areas <- function(cube) {
370-
# Get area for each class for each row of the cube
371-
freq_lst <- slider::slide(cube, function(tile) {
372-
# Get the frequency count and value for each labelled image
373-
.tile_area_freq(tile)
374-
})
375-
# Get a tibble by binding the row (duplicated labels with different counts)
376-
freq <- do.call(rbind, freq_lst)
370+
# Get cube area / pixel frequency
371+
freq <- .cube_area_freq(cube)
377372
# summarize the counts for each label
378373
freq <- freq |>
379374
dplyr::filter(!is.na(class)) |>
380375
dplyr::group_by(class) |>
381376
dplyr::summarise(area = sum(.data[["area"]]))
382-
383377
# Area is taken as the sum of pixels
384378
class_areas <- freq[["area"]]
385379
# Names of area are the classes
@@ -388,7 +382,40 @@ NULL
388382
class_areas[is.na(class_areas)] <- 0.0
389383
class_areas
390384
}
391-
385+
#' @title Return areas/frequency of classes of a class_cube
386+
#' @keywords internal
387+
#' @noRd
388+
#' @name .cube_area_freq
389+
#' @param cube class cube
390+
#'
391+
#' @return A \code{vector} with the area and pixel frequency of the cube labels.
392+
.cube_area_freq <- function(cube) {
393+
UseMethod(".cube_area_freq", cube)
394+
}
395+
#' @export
396+
.cube_area_freq.class_cube <- function(cube) {
397+
# Get area for each class for each row of the cube
398+
slider::slide_dfr(cube, function(tile) {
399+
# Get the frequency count and value for each labelled image
400+
.tile_area_freq(tile)
401+
}) |>
402+
dplyr::filter(!is.na(.data[["class"]]))
403+
}
404+
#' @export
405+
.cube_area_freq.class_vector_cube <- function(cube) {
406+
# Get area for each class for each row of the cube
407+
slider::slide_dfr(cube, function(tile) {
408+
# Get the frequency count and value for each labelled image
409+
.tile_area_freq(tile)
410+
}) |>
411+
dplyr::filter(!is.na(.data[["class"]]))
412+
}
413+
#' @export
414+
.cube_area_freq.default <- function(cube) {
415+
cube <- tibble::as_tibble(cube)
416+
cube <- .cube_find_class(cube)
417+
.cube_area_freq(cube)
418+
}
392419
#' @title Return bands of a data cube
393420
#' @keywords internal
394421
#' @noRd
@@ -1481,6 +1508,10 @@ NULL
14811508
file_info <- slider::slide_dfr(file_info, function(fi) {
14821509
# Get tile path
14831510
path <- fi[["path"]]
1511+
# is file NA?
1512+
if (is.na(path)) {
1513+
return(path)
1514+
}
14841515
# is file exists in local path?
14851516
if (file.exists(path)) {
14861517
return(path)

R/api_message.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@
7676
)
7777
}
7878
}
79+
#' @title Warning when converting a slow area calculation will be performed
80+
#' @name .message_warnings_slow_area_calculation
81+
#' @noRd
82+
#' @returns Called for side effects
83+
.message_warnings_slow_area_calculation <- function() {
84+
if (.message_warnings()) {
85+
warning(.conf("messages", "summary_class_cube_slow_area"), call. = FALSE)
86+
}
87+
}
7988
#' @title Test if progress bar should be shown
8089
#' @name .message_progress
8190
#' @noRd

0 commit comments

Comments
 (0)