Skip to content

Commit

Permalink
mapLayout_no_interactive function + doc + tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
BERTHET Clement (Externe) committed Oct 20, 2023
1 parent d723a47 commit 7bdd2df
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(getInteractivity)
export(leafletDragPointsOutput)
export(limitSizeGraph)
export(mapLayout)
export(mapLayout_no_interactive)
export(modRpart)
export(modXY)
export(plotMap)
Expand Down Expand Up @@ -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)
Expand Down
50 changes: 44 additions & 6 deletions R/map_layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

##
Expand Down Expand Up @@ -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]

Expand Down
28 changes: 23 additions & 5 deletions man/mapLayout_no_interactive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 64 additions & 5 deletions tests/testthat/test-map_layout.R
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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,
Expand Down

0 comments on commit 7bdd2df

Please sign in to comment.