diff --git a/NAMESPACE b/NAMESPACE index 457d762..764dd74 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,6 +13,7 @@ export(getInteractivity) export(leafletDragPointsOutput) export(limitSizeGraph) export(mapLayout) +export(mapLayout_no_interactive) export(modRpart) export(modXY) export(plotMap) @@ -65,6 +66,7 @@ importFrom(plotly,add_trace) importFrom(plotly,config) importFrom(plotly,layout) importFrom(plotly,plot_ly) +importFrom(sf,st_read) importFrom(shiny,runApp) importFrom(stats,as.formula) importFrom(stats,density) diff --git a/R/map_layout.R b/R/map_layout.R index 4936fcf..78b8e0e 100644 --- a/R/map_layout.R +++ b/R/map_layout.R @@ -657,18 +657,35 @@ utils::globalVariables("from") #' #' @description #' -#' this function creates a 'mapLayout' object from a study and an external -#' 'geojson' file. -#' The 'geojson' file must contain zones compatible with the study. -#' This function should be used only once per study. +#' This function creates a 'mapLayout' object from a study and an external +#' 'geojson' file. +#' +#' This function should be used only once per study. +#' #' The result should then be saved in an external file and be reused. #' #' @param path_geojson_file `character` path of geojson file #' @param opts list of simulation parameters returned by -#' the function [antaresRead::setSimulationPath()] +#' the function \code{\link[antaresRead]{setSimulationPath}} #' #' @importFrom methods as +#' @importFrom sf st_read +#' +#' @export +#' @note The 'geojson' file must contain zones compatible with the study. +#' +#' @return Object of class "mapLayout" +#' +#' @examples +#' \dontrun{ +#' # set informations to your study ("input" mode is enough) +#' setSimulationPath(path = "path/my_study", simulation = "input") +#' +#' path_geojson <- "path/my_geosjonfile.geojson" #' +#' mapLayout_no_interactive(path_geojson_file = path_geojson) +#' +#' } mapLayout_no_interactive <- function(path_geojson_file, opts = simOptions()){ # check parameters @@ -677,6 +694,24 @@ mapLayout_no_interactive <- function(path_geojson_file, # read file sf_object <- st_read(path_geojson_file) + + # check geojson file + if(!"name"%in%names(sf_object)) + stop("geosjon file must have key 'name'", + call. = FALSE) + if(!all(c("Lat", "Long")%in%names(sf_object))) + stop("geosjon file must have key {'Lat;'Long'}", + call. = FALSE) + + # check areas if compatible with geojson file + areas_names <- getAreas() + if(!any(areas_names%in%tolower(sf_object$name))) + stop("study must have areas according to geojson file", + call. = FALSE) + + cat("\nstudy compatible with geojson file\n") + + # conversion to "sp" class geojson_as_sp <- as(sf_object, "Spatial") ## @@ -704,11 +739,14 @@ mapLayout_no_interactive <- function(path_geojson_file, ## # Manage study's links ## - links <- data.table( opts$linksDef ) + if(nrow(links) %in% 0) + stop("no links are found in study", + call. = FALSE) + # keep links according to your study links <- links[from %in% all_coords$area & to %in% all_coords$area] diff --git a/man/mapLayout_no_interactive.Rd b/man/mapLayout_no_interactive.Rd index 54df515..174e214 100644 --- a/man/mapLayout_no_interactive.Rd +++ b/man/mapLayout_no_interactive.Rd @@ -10,12 +10,30 @@ mapLayout_no_interactive(path_geojson_file, opts = simOptions()) \item{path_geojson_file}{`character` path of geojson file} \item{opts}{list of simulation parameters returned by -the function [antaresRead::setSimulationPath()]} +the function \code{\link[antaresRead]{setSimulationPath}}} +} +\value{ +Object of class "mapLayout" } \description{ -this function creates a 'mapLayout' object from a study and an external -'geojson' file. -The 'geojson' file must contain zones compatible with the study. -This function should be used only once per study. +This function creates a 'mapLayout' object from a study and an external +'geojson' file. + +This function should be used only once per study. + The result should then be saved in an external file and be reused. } +\note{ +The 'geojson' file must contain zones compatible with the study. +} +\examples{ +\dontrun{ +# set informations to your study ("input" mode is enough) +setSimulationPath(path = "path/my_study", simulation = "input") + +path_geojson <- "path/my_geosjonfile.geojson" + +mapLayout_no_interactive(path_geojson_file = path_geojson) + +} +} diff --git a/tests/testthat/test-map_layout.R b/tests/testthat/test-map_layout.R index cad3649..5effca9 100644 --- a/tests/testthat/test-map_layout.R +++ b/tests/testthat/test-map_layout.R @@ -1,6 +1,10 @@ + test_that("build objet 'mapLayout' no interactive", { - # create study with areas/links according to geojson file test + skip_if_not_installed("antaresEditObject", + minimum_version = "0.3.0") + # create study ---- + # create study with areas/links according to geojson file test antaresEditObject::createStudy(path = tempdir(), study_name = "zonal_test", antares_version = "8.2.0") @@ -11,17 +15,72 @@ test_that("build objet 'mapLayout' no interactive", { antaresEditObject::createLink(from = "21_FR", to = "24_FR") - path_geojson_test <- system.file("mapLayout/filter_zonal.geojson", package = "antaresViz") - obj_mapLayout <-mapLayout_no_interactive(path_geojson_file = path_geojson_test) + # read geojson ---- + path_geojson_test <- system.file("mapLayout/filter_zonal.geojson", + package = "antaresViz") + + geo_file <- sf::st_read(path_geojson_test) + + # error case ---- + # bad areas name + bad_area_name <- geo_file + bad_area_name$name <- sample(c("titi", "toto"), + size = length(bad_name$name), + replace = TRUE) + + bad_area_name <- geojsonio::geojson_write(input = bad_area_name) + + testthat::expect_error( + mapLayout_no_interactive(path_geojson_file = bad_area_name$path), + regexp = "study must have areas according to geojson file" + ) + + # bad structure geojson file + bad_struct_file <- geo_file + bad_struct_file <- bad_struct_file[, setdiff(names(bad_struct_file), "name")] + + bad_struct_file <- geojsonio::geojson_write(input = bad_struct_file) + testthat::expect_error( + mapLayout_no_interactive(path_geojson_file = bad_struct_file$path), + regexp = "geosjon file must have key 'name'" + ) + + # no "Long" "Lat" key + bad_struct_file <- geo_file + bad_struct_file <- bad_struct_file[, setdiff(names(bad_struct_file), + c("Lat", "Long"))] + + bad_struct_file <- geojsonio::geojson_write(input = bad_struct_file) + + testthat::expect_error( + mapLayout_no_interactive(path_geojson_file = bad_struct_file$path), + regexp = "geosjon file must have key \\{'Lat;'Long'\\}" + ) + + # remove file + file.remove(bad_struct_file$path) + + # good case ---- + # build "mapLayout" object + obj_mapLayout <- mapLayout_no_interactive(path_geojson_file = path_geojson_test) + + # tests testthat::expect_s3_class(obj_mapLayout, 'mapLayout') + testthat::expect_true(all( + c("coords", "links", "map", "all_coords") %in% + names(obj_mapLayout))) + + # delete study ---- + unlink(file.path(tempdir(), "zonal_test"), recursive = TRUE) - unlink(file.path(tempdir(), "zonal_test")) + # @examples + # commented code if you want to test to plot this "mapLayout" # opts_zonal <- setSimulationPath(file.path(tempdir(), # "zonal_test")) # - # runSimulation("zonal_testsim") + # runSimulation("zonal_testsim") # run from ui if it don't work # # mydata <- readAntares(areas = obj$coords$area, # links = obj$links$link,