From 33eb84480ac4975f5efa8016450c09fb571ae478 Mon Sep 17 00:00:00 2001 From: Laura Marshall Date: Thu, 30 Jan 2025 11:50:15 +0000 Subject: [PATCH] Closes #96 --- DESCRIPTION | 2 +- NEWS.md | 6 +++ R/Simulation.R | 2 +- tests/testthat/test-check_Constructors.R | 51 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f6b9143..743ead8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,7 @@ Suggests: rmarkdown Type: Package Title: Distance Sampling Simulations -Version: 1.0.5 +Version: 1.0.5.9000 Authors@R: c( person("Laura", "Marshall", email = "lhm@st-and.ac.uk", role = c("aut", "cre")), person("Thomas", "Len", email = "len.thomas@st-andrews.ac.uk", role = "ctb")) diff --git a/NEWS.md b/NEWS.md index 39062bf..4d54b4d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# dsims 1.0.6 + +Bug Fixes + +* Fixed bug when simulation checks generated an error for projected shapes. Issue #94 + # dsims 1.0.5 Bug Fixes diff --git a/R/Simulation.R b/R/Simulation.R index 690df96..4046a84 100755 --- a/R/Simulation.R +++ b/R/Simulation.R @@ -159,7 +159,7 @@ setValidity("Simulation", # Check the intersection between design and population regions - they must overlap intersect <- suppressWarnings(sf::st_intersection(object@design@region@region, object@population.description@density@density.surface[[1]])) intersect.area <- sum(sf::st_area(intersect)) - if(intersect.area == 0){ + if(as.numeric(intersect.area) == 0){ return("The regions associated with the design and the population description do not overlap!") } # Compare the area of the design region and population region with the intersection. diff --git a/tests/testthat/test-check_Constructors.R b/tests/testthat/test-check_Constructors.R index bdfb2d1..931c3eb 100644 --- a/tests/testthat/test-check_Constructors.R +++ b/tests/testthat/test-check_Constructors.R @@ -400,3 +400,54 @@ test_that("Test simulation validation and consistency checks", { #expect_warning(make.simulation(population.description = pop.desc),"The population density surface extends beyond the design survey region, only part of the population will be surveyed.") }) + + +test_that("Test issue 94", { + #Load a multi strata unprojected shapefile + # Find the full file path to the shapefile on the users machine + shapefile.path <- system.file("extdata", "AreaRProjStrata.shp", package = "dssd") + + # Create the region object + region <- make.region(region.name = "study area", + strata.name = c("North", "NW", "West Upper", "West Lower", "SW", "South"), + shape = shapefile.path) + + # Plot the survey region + design <- make.design(region = region, + transect.type = "line", + design = "systematic", + spacing = c(rep(16000, 3), rep(8000, 3)), + design.angle = c(160, 135, 80, 135, 50, 150), + edge.protocol = "minus", + truncation = 1500) + + # Make a density grid with values of 1 across the region + my.density <- make.density(region = region, + x.space = 2500, + y.space = 2500, + constant = 1) + + # Create the population description + pop.description <- make.population.description(region = region, + density = my.density, + N = rep(400,6), + fixed.N = TRUE) + + # Create the detectability + detect <- make.detectability(key.function = "hn", + scale.param = 750, + truncation = 1500) + + # Define the analyses - both the hn and hr models use the ~strata.group formula + ds.analyses <- make.ds.analysis(truncation = 1500) + + # Make simulation + simulation <- make.simulation(reps = 3, + design = design, + population.description = pop.description, + detectability = detect, + ds.analysis = ds.analyses) + + # Check a simulation object is returned + expect_true(is(simulation, "Simulation")) +})