diff --git a/R/srs.polygon.r b/R/srs.polygon.r index ccaf720..11a5cba 100644 --- a/R/srs.polygon.r +++ b/R/srs.polygon.r @@ -58,11 +58,22 @@ #' srs.polygon <- function( x, n ){ - # Check n + # Check n if( n < 1 ){ n <- 1 warning("Sample size less than one has been reset to 1") } + + # Function to convert SpatialPolygons to SpatialPolygonsDataFrame + makeSpatialPolygonsDataFrame <- function(x) { + x <- SpatialPolygonsDataFrame(x, data.frame(ID=1:length(x)), match.ID=FALSE) + return(x) + } + + # Check for SpatialPolygons and convert if needed + if(class(x) == "SpatialPolygons") { + x <- makeSpatialPolygonsDataFrame(x) + } # Bounding box of shapefile bb <- bbox( x ) diff --git a/R/sss.polygon.r b/R/sss.polygon.r index 73ae986..1923bd7 100644 --- a/R/sss.polygon.r +++ b/R/sss.polygon.r @@ -184,6 +184,16 @@ #' sss.polygon <- function( x, n, spacing=c(1,1), triangular=FALSE, rand.dir=FALSE ){ +# Function to convert SpatialPolygons to SpatialPolygonsDataFrame +makeSpatialPolygonsDataFrame <- function(x) { + x <- SpatialPolygonsDataFrame(x, data.frame(ID=1:length(x)), match.ID=FALSE) + return(x) +} + +# Check for SpatialPolygons and convert if needed +if(class(x) == "SpatialPolygons") { + x <- makeSpatialPolygonsDataFrame(x) +} # Bounding box of shapefile bb <- bbox( x ) diff --git a/tests/testthat/test-srs.polygon.R b/tests/testthat/test-srs.polygon.R index 6efc74c..24d19a0 100644 --- a/tests/testthat/test-srs.polygon.R +++ b/tests/testthat/test-srs.polygon.R @@ -26,6 +26,15 @@ test_that("warning when sample size less than 1",{ expect_warning(srs.polygon(WY, 0), "Sample size less than one has been reset to 1") }) +test_that("check if x is SpatialPolygons", { + expect_is(srs.polygon(spatPoly,15), "SpatialPointsDataFrame") + expect_type(srs.polygon(spatPoly, 15), "S4") +}) + +test_that("check for the column names when none specified", { + expect_named(srs.polygon(spatPoly, 15), c("sampleID", "geometryID", "ID")) +}) + test_that("x is a SpatialPointsDataFrame", { expect_is(srs.polygon(WY,25), "SpatialPointsDataFrame") expect_type(srs.polygon(WY, 20), "S4") diff --git a/tests/testthat/test-sss.polygon.R b/tests/testthat/test-sss.polygon.R index 2628022..021d529 100644 --- a/tests/testthat/test-sss.polygon.R +++ b/tests/testthat/test-sss.polygon.R @@ -5,6 +5,31 @@ context("Testing sss.polygon()") # load pre-built dataset data(WY) +# create spatial polygons object +# make "squares" with integer values rounded in a list +square <- rbind( c(2, 4, 3, 4, 3, 5, + 2, 5, 2, 4, 2, 4), + c(6, 9, 7, 9, 7, 8, + 6, 8, 6, 9, 6, 9)) + +# give these "squares" an identification +ID <- c("shape1", "shape2") + +# create SpatialPolygon object from these squares +spatPoly <- SpatialPolygons(list( + Polygons(list(Polygon(matrix(square[1, ], ncol = 2, byrow = TRUE))), ID[1]), + Polygons(list(Polygon(matrix(square[2, ], ncol = 2, byrow = TRUE))), ID[2]) +)) + + +test_that("check if x is SpatialPolygons", { + expect_is(sss.polygon(spatPoly,15), "SpatialPointsDataFrame") + expect_type(sss.polygon(spatPoly, 15), "S4") +}) + +test_that("check for the column names when none specified", { + expect_named(sss.polygon(spatPoly, 15), c("sampleID", "row", "col", "geometryID", "ID")) +}) test_that("check for the column names when none specified", { expect_named(sss.polygon(WY, 30), c("sampleID", "row", "col", "geometryID", "STATEFP", "COUNTYFP", "NAME"))