Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #13 #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion R/srs.polygon.r
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
10 changes: 10 additions & 0 deletions R/sss.polygon.r
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-srs.polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-sss.polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down