Skip to content

Commit 9a31521

Browse files
committed
Add tests for sf classes
Weaken warnings in check_crs
1 parent d1fb73e commit 9a31521

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

R/normalize-sf.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ polygonData.LINESTRING <- polygonData.POLYGON
6666
# helpers -----------------------------------------------------------------
6767

6868
check_crs <- function(x) {
69-
if (!sf::st_is_longlat(x)) {
70-
stop("sf data must be long-lat", call. = FALSE)
69+
crs <- sf::st_crs(x)
70+
71+
# Don't have enough information to check
72+
if (is.na(crs))
73+
return()
74+
75+
if (identical(sf::st_is_longlat(x), FALSE)) {
76+
warning("sf layer is not long-lat data", call. = FALSE)
7177
}
7278

73-
crs <- sf::st_crs(x)
7479
if (!grepl("+datum=WGS84", crs$proj4string, fixed = TRUE)) {
7580
warning(
7681
"sf layer has inconsistent datum (", crs$proj4string, ").\n",

tests/testthat/test-normalize.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ test_that("can get point data from SpatialPointsDataFrame", {
1111
expect_equal(nrow(points), 155)
1212
})
1313

14+
test_that("derivePolygons works with sf classes", {
15+
skip_if_not_installed("sf")
16+
17+
data("meuse", package = "sp", envir = environment())
18+
sp::coordinates(meuse) <- ~x+y
19+
meuse <- sf::st_as_sf(meuse)
20+
21+
points <- derivePoints(meuse)
22+
expect_named(points, c("lng", "lat"))
23+
expect_equal(nrow(points), 155)
24+
})
25+
1426
# derivePolygons -----------------------------------------------------------
1527

1628
test_that("derivePolygons normalizes polygon data across sp polygon classes", {
@@ -55,6 +67,18 @@ test_that("derivePolygons normalizes polygon data across sp line classes", {
5567
expect_equal(derivePolygons(slinesdf), out)
5668
})
5769

70+
test_that("derivePolygons works with sf classes", {
71+
skip_if_not_installed("sf")
72+
73+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
74+
75+
expect_warning(
76+
polys <- derivePolygons(nc),
77+
"inconsistent datum"
78+
)
79+
80+
expect_length(polys, nrow(nc))
81+
})
5882

5983
# guessLatLongCols --------------------------------------------------------
6084
ll_names <- function(lng, lat) list(lng = lng, lat = lat)

0 commit comments

Comments
 (0)