diff --git a/R/arith.R b/R/arith.R index 00fdf71c0..5f1b2b336 100644 --- a/R/arith.R +++ b/R/arith.R @@ -19,11 +19,12 @@ #' # affine: #' st_point(c(1,2)) * m + c(2,5) #' # world in 0-360 range: -#' library(maps) -#' w = st_as_sf(map('world', plot = FALSE, fill = TRUE)) -#' w2 = (st_geometry(w) + c(360,90)) %% c(360) - c(0,90) -#' w3 = st_wrap_dateline(st_set_crs(w2 - c(180,0), 4326)) + c(180,0) -#' plot(st_set_crs(w3, 4326), axes = TRUE) +#' if (require(maps, quietly = TRUE)) { +#' w = st_as_sf(map('world', plot = FALSE, fill = TRUE)) +#' w2 = (st_geometry(w) + c(360,90)) %% c(360) - c(0,90) +#' w3 = st_wrap_dateline(st_set_crs(w2 - c(180,0), 4326)) + c(180,0) +#' plot(st_set_crs(w3, 4326), axes = TRUE) +#' } #' (mp <- st_point(c(1,2)) + st_point(c(3,4))) # MULTIPOINT (1 2, 3 4) #' mp - st_point(c(3,4)) # POINT (1 2) #' opar = par(mfrow = c(2,2), mar = c(0, 0, 1, 0)) diff --git a/R/bind.R b/R/bind.R index ef19874f8..fc3af3a9a 100644 --- a/R/bind.R +++ b/R/bind.R @@ -48,13 +48,14 @@ rbind.sf = function(..., deparse.level = 1) { #' @details If you need to \code{cbind} e.g. a \code{data.frame} to an \code{sf}, use \link{data.frame} directly and use \link{st_sf} on its result, or use \link[dplyr:bind]{bind_cols}; see examples. #' @examples #' cbind(a,b,c) # warns -#' if (require(dplyr)) +#' if (require(dplyr, quietly = TRUE)) #' dplyr::bind_cols(a,b) #' c = st_sf(a=4, geomc = st_sfc(st_multilinestring(list(matrix(1:4,2)))), crs = crs) #' cbind(a,b,c, sf_column_name = "geomc") #' df = data.frame(x=3) #' st_sf(data.frame(c, df)) -#' dplyr::bind_cols(c, df) +#' if (require(dplyr, quietly = TRUE)) +#' dplyr::bind_cols(c, df) cbind.sf = function(..., deparse.level = 1, sf_column_name = NULL) { # TODO: handle st_agr? st_sf(data.frame(...), sf_column_name = sf_column_name) diff --git a/R/crs.R b/R/crs.R index 9c2bc8c23..9ee8de48d 100644 --- a/R/crs.R +++ b/R/crs.R @@ -228,9 +228,7 @@ make_crs = function(x) { #' @name st_crs #' @examples #' sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) -#' library(dplyr) -#' x = sfc %>% st_set_crs(4326) %>% st_transform(3857) -#' x +#' sfc %>% st_set_crs(4326) %>% st_transform(3857) #' @export st_set_crs = function(x, value) { st_crs(x) = value diff --git a/R/geom-transformers.R b/R/geom-transformers.R index a6cab8813..c86cc7b7c 100644 --- a/R/geom-transformers.R +++ b/R/geom-transformers.R @@ -574,9 +574,11 @@ st_node.sf = function(x) { #' @param ... ignored #' @examples #' sf = st_sf(a=1, geom=st_sfc(st_linestring(rbind(c(0,0),c(1,1)))), crs = 4326) -#' seg = st_segmentize(sf, units::set_units(100, km)) -#' seg = st_segmentize(sf, units::set_units(0.01, rad)) -#' nrow(seg$geom[[1]]) +#' if (require(lwgeom, quietly = TRUE)) { +#' seg = st_segmentize(sf, units::set_units(100, km)) +#' seg = st_segmentize(sf, units::set_units(0.01, rad)) +#' nrow(seg$geom[[1]]) +#' } st_segmentize = function(x, dfMaxLength, ...) UseMethod("st_segmentize") diff --git a/R/graticule.R b/R/graticule.R index 8988b889f..691034320 100644 --- a/R/graticule.R +++ b/R/graticule.R @@ -27,7 +27,7 @@ #' see example. #' @examples #' library(sf) -#' library(maps) +#' if (require(maps, quietly = TRUE)) { #' #' usa = st_as_sf(map('usa', plot = FALSE, fill = TRUE)) #' laea = st_crs("+proj=laea +lat_0=30 +lon_0=-95") # Lambert equal area @@ -59,6 +59,7 @@ #' srt = g$angle_end[i] - 90, pos = 3, cex = .7) #' })) #' plot(usa, graticule = st_crs(4326), axes = TRUE, lon = seq(-60,-130,by=-10)) +#' } st_graticule = function(x = c(-180,-90,180,90), crs = st_crs(x), datum = st_crs(4326), ..., lon = NULL, lat = NULL, ndiscr = 100, margin = 0.001) diff --git a/R/jitter.R b/R/jitter.R index 3be3a84f0..a49fec85d 100644 --- a/R/jitter.R +++ b/R/jitter.R @@ -5,7 +5,7 @@ #' @param factor numeric; fractional amount of jittering to be applied #' @details jitters coordinates with an amount such that \code{runif(1, -amount, amount)} is added to the coordinates. x- and y-coordinates are jittered independently but all coordinates of a single geometry are jittered with the same amount, meaning that the geometry shape does not change. For longlat data, a latitude correction is made such that jittering in East and North directions are identical in distance in the center of the bounding box of \code{x}. #' @examples -#' nc = read_sf(system.file("gpkg/nc.gpkg", package="sf")) +#' nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) #' pts = st_centroid(st_geometry(nc)) #' plot(pts) #' plot(st_jitter(pts, .05), add = TRUE, col = 'red') diff --git a/R/join.R b/R/join.R index effcfe573..d42161720 100644 --- a/R/join.R +++ b/R/join.R @@ -105,8 +105,9 @@ st_join = function(x, y, join, ...) UseMethod("st_join") #' st_join(a, b, left = FALSE) #' # two ways to aggregate y's attribute values outcome over x's geometries: #' st_join(a, b) %>% aggregate(list(.$a.x), mean) -#' library(dplyr) -#' st_join(a, b) %>% group_by(a.x) %>% summarise(mean(a.y)) +#' if (require(dplyr, quietly = TRUE)) { +#' st_join(a, b) %>% group_by(a.x) %>% summarise(mean(a.y)) +#' } #' # example of largest = TRUE: #' nc <- st_transform(st_read(system.file("shape/nc.shp", package="sf")), 2264) #' gr = st_sf( diff --git a/R/make_grid.R b/R/make_grid.R index 7ce0daa96..35ff34cec 100644 --- a/R/make_grid.R +++ b/R/make_grid.R @@ -20,7 +20,7 @@ #' # non-default offset: #' plot(st_make_grid(sfc, cellsize = .1, square = FALSE, offset = c(0, .05 / (sqrt(3)/2)))) #' plot(sfc, add = TRUE) -#' nc = read_sf(system.file("shape/nc.shp", package="sf")) +#' nc = st_read(system.file("shape/nc.shp", package="sf")) #' g = st_make_grid(nc) #' plot(g) #' plot(st_geometry(nc), add = TRUE) diff --git a/R/nearest.R b/R/nearest.R index ec665cbbe..3a8728687 100644 --- a/R/nearest.R +++ b/R/nearest.R @@ -23,7 +23,7 @@ #' plot(st_sfc(b2, b3), add = TRUE, col = NA, border = 'blue') #' plot(ls, add = TRUE, col = 'red') #' -#' nc = read_sf(system.file("gpkg/nc.gpkg", package="sf")) +#' nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) #' plot(st_geometry(nc)) #' ls = st_nearest_points(nc[1,], nc) #' plot(ls, col = 'red', add = TRUE) diff --git a/R/read.R b/R/read.R index 06e09c624..4ea027f6a 100644 --- a/R/read.R +++ b/R/read.R @@ -121,7 +121,7 @@ set_utf8 = function(x) { #' # spatial filter, as wkt: #' wkt = st_as_text(st_geometry(nc[1,])) #' # filter by (bbox overlaps of) first feature geometry: -#' read_sf(system.file("gpkg/nc.gpkg", package="sf"), wkt_filter = wkt) +#' st_read(system.file("gpkg/nc.gpkg", package="sf"), wkt_filter = wkt) #' @export st_read = function(dsn, layer, ...) UseMethod("st_read") @@ -246,7 +246,7 @@ st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet #' # read geojson from string: #' geojson_txt <- paste("{\"type\":\"MultiPoint\",\"coordinates\":", #' "[[3.2,4],[3,4.6],[3.8,4.4],[3.5,3.8],[3.4,3.6],[3.9,4.5]]}") -#' x = read_sf(geojson_txt) +#' x = st_read(geojson_txt) #' x read_sf <- function(..., quiet = TRUE, stringsAsFactors = FALSE, as_tibble = TRUE) { st_read(..., quiet = quiet, stringsAsFactors = stringsAsFactors, as_tibble = as_tibble) @@ -369,12 +369,13 @@ abbreviate_shapefile_names = function(x) { #' nc = st_read(system.file("shape/nc.shp", package="sf")) #' st_write(nc, paste0(tempdir(), "/", "nc.shp")) #' st_write(nc, paste0(tempdir(), "/", "nc.shp"), delete_layer = TRUE) # overwrites -#' data(meuse, package = "sp") # loads data.frame from sp -#' meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) -#' # writes X and Y as columns: -#' st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_XY") -#' st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_WKT", -#' delete_dsn=TRUE) # overwrites +#' if (require(sp, quietly = TRUE)) { +#' data(meuse, package = "sp") # loads data.frame from sp +#' meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) +#' # writes X and Y as columns: +#' st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_XY") +#' st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_WKT", +#' delete_dsn=TRUE) # overwrites #' \dontrun{ #' library(sp) #' example(meuse, ask = FALSE, echo = FALSE) @@ -383,6 +384,7 @@ abbreviate_shapefile_names = function(x) { #' demo(nc, ask = FALSE) #' try(st_write(nc, "PG:dbname=postgis", "sids", layer_options = "OVERWRITE=true")) #' } +#' } #' @export st_write = function(obj, dsn, layer, ...) UseMethod("st_write") diff --git a/R/sample.R b/R/sample.R index 4f2434d0e..6e911dec2 100644 --- a/R/sample.R +++ b/R/sample.R @@ -46,6 +46,7 @@ st_sample = function(x, size, ...) UseMethod("st_sample") #' plot(x, axes = TRUE, graticule = TRUE) #' if (sf_extSoftVersion()["proj.4"] >= "4.9.0") #' plot(p <- st_sample(x, 1000), add = TRUE) +#' if (require(lwgeom, quietly = TRUE)) { # for st_segmentize() #' x2 = st_transform(st_segmentize(x, 1e4), st_crs("+proj=ortho +lat_0=30 +lon_0=45")) #' g = st_transform(st_graticule(), st_crs("+proj=ortho +lat_0=30 +lon_0=45")) #' plot(x2, graticule = g) @@ -53,6 +54,7 @@ st_sample = function(x, size, ...) UseMethod("st_sample") #' p2 = st_transform(p, st_crs("+proj=ortho +lat_0=30 +lon_0=45")) #' plot(p2, add = TRUE) #' } +#' } #' x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,10),c(0,90),c(0,0))))) # NOT long/lat: #' plot(x) #' p_exact = st_sample(x, 1000, exact = TRUE) diff --git a/R/sf.R b/R/sf.R index 66f59844e..593f2951e 100644 --- a/R/sf.R +++ b/R/sf.R @@ -29,10 +29,12 @@ st_as_sf = function(x, ...) UseMethod("st_as_sf") #' df = st_as_sf(d, wkt = "geom") #' d$geom2 = st_sfc(pt1, pt2) #' st_as_sf(d) # should warn -#' data(meuse, package = "sp") -#' meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") -#' meuse_sf[1:3,] -#' summary(meuse_sf) +#' if (require(sp, quietly = TRUE)) { +#' data(meuse, package = "sp") +#' meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") +#' meuse_sf[1:3,] +#' summary(meuse_sf) +#' } #' @export st_as_sf.data.frame = function(x, ..., agr = NA_agr_, coords, wkt, dim = "XYZ", remove = TRUE, na.fail = TRUE, sf_column_name = NULL) { diff --git a/R/sp.R b/R/sp.R index f58766479..5551361dc 100644 --- a/R/sp.R +++ b/R/sp.R @@ -23,7 +23,7 @@ #' @name st_as_sf #' @examples -#' library(sp) +#' if (require(sp, quietly = TRUE)) { #' x = rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1)) #' x1 = 0.1 * x + 0.1 #' x2 = 0.1 * x + 0.4 @@ -52,6 +52,7 @@ #' pol.grd = as(meuse.grid, "SpatialPolygonsDataFrame") #' # summary(st_as_sf(pol.grd)) #' # summary(st_as_sf(as(pol.grd, "SpatialLinesDataFrame"))) +#' } #' @export st_as_sf.Spatial = function(x, ...) { if ("data" %in% slotNames(x)) @@ -238,6 +239,7 @@ setAs("XY", "Spatial", function(from) as(st_sfc(from), "Spatial")) #' @export #' @examples #' nc <- st_read(system.file("shape/nc.shp", package="sf")) +#' if (require(sp, quietly = TRUE)) { #' # convert to SpatialPolygonsDataFrame #' spdf <- as_Spatial(nc) #' # identical to @@ -246,6 +248,7 @@ setAs("XY", "Spatial", function(from) as(st_sfc(from), "Spatial")) #' as(st_geometry(nc), "Spatial") #' # back to sf #' as(spdf, "sf") +#' } as_Spatial = function(from, cast = TRUE, IDs = paste0("ID", seq_along(from))) { if (inherits(from, "sf")) { geom = st_geometry(from) diff --git a/R/tidyverse.R b/R/tidyverse.R index 60f634512..fd3d96df8 100644 --- a/R/tidyverse.R +++ b/R/tidyverse.R @@ -38,9 +38,10 @@ group_split.sf <- function(.tbl, ..., .keep = TRUE) { #' @param ... other arguments #' @name tidyverse #' @examples -#' library(dplyr) -#' nc = st_read(system.file("shape/nc.shp", package="sf")) -#' nc %>% filter(AREA > .1) %>% plot() +#' if (require(dplyr, quietly = TRUE)) { +#' nc = read_sf(system.file("shape/nc.shp", package="sf")) +#' nc %>% filter(AREA > .1) %>% plot() +#' } filter.sf <- function(.data, ..., .dots) { agr = st_agr(.data) class(.data) <- setdiff(class(.data), "sf") @@ -50,9 +51,11 @@ filter.sf <- function(.data, ..., .dots) { #' @name tidyverse #' @examples #' # plot 10 smallest counties in grey: -#' st_geometry(nc) %>% plot() -#' nc %>% select(AREA) %>% arrange(AREA) %>% slice(1:10) %>% plot(add = TRUE, col = 'grey') -#' title("the ten counties with smallest area") +#' if (require(dplyr, quietly = TRUE)) { +#' st_geometry(nc) %>% plot() +#' nc %>% select(AREA) %>% arrange(AREA) %>% slice(1:10) %>% plot(add = TRUE, col = 'grey') +#' title("the ten counties with smallest area") +#' } arrange.sf <- function(.data, ..., .dots) { sf_column_name = attr(.data, "sf_column") class(.data) = setdiff(class(.data), "sf") @@ -62,8 +65,10 @@ arrange.sf <- function(.data, ..., .dots) { #' @name tidyverse #' @param add see corresponding function in dplyr #' @examples -#' nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -#' nc %>% group_by(area_cl) %>% class() +#' if (require(dplyr, quietly = TRUE)) { +#' nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) +#' nc %>% group_by(area_cl) %>% class() +#' } group_by.sf <- function(.data, ..., add = FALSE) { sf_column_name = attr(.data, "sf_column") class(.data) <- setdiff(class(.data), "sf") @@ -102,7 +107,9 @@ rowwise.sf <- function(x, ...) { #' @name tidyverse #' @examples -#' nc2 <- nc %>% mutate(area10 = AREA/10) +#' if (require(dplyr, quietly = TRUE)) { +#' nc2 <- nc %>% mutate(area10 = AREA/10) +#' } mutate.sf <- function(.data, ..., .dots) { #st_as_sf(NextMethod(), sf_column_name = attr(.data, "sf_column")) agr = st_agr(.data) @@ -113,8 +120,10 @@ mutate.sf <- function(.data, ..., .dots) { #' @name tidyverse #' @examples -#' nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class() -#' nc %>% transmute(AREA = AREA/10) %>% class() +#' if (require(dplyr, quietly = TRUE)) { +#' nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class() +#' nc %>% transmute(AREA = AREA/10) %>% class() +#' } transmute.sf <- function(.data, ..., .dots) { sf_column_name = attr(.data, "sf_column") agr = st_agr(.data) @@ -125,10 +134,12 @@ transmute.sf <- function(.data, ..., .dots) { #' @name tidyverse #' @examples -#' nc %>% select(SID74, SID79) %>% names() -#' nc %>% select(SID74, SID79, geometry) %>% names() -#' nc %>% select(SID74, SID79) %>% class() -#' nc %>% select(SID74, SID79, geometry) %>% class() +#' if (require(dplyr, quietly = TRUE)) { +#' nc %>% select(SID74, SID79) %>% names() +#' nc %>% select(SID74, SID79, geometry) %>% names() +#' nc %>% select(SID74, SID79) %>% class() +#' nc %>% select(SID74, SID79, geometry) %>% class() +#' } #' @details \code{select} keeps the geometry regardless whether it is selected or not; to deselect it, first pipe through \code{as.data.frame} to let dplyr's own \code{select} drop it. select.sf <- function(.data, ...) { @@ -168,7 +179,9 @@ select.sf <- function(.data, ...) { #' @name tidyverse #' @examples -#' nc2 <- nc %>% rename(area = AREA) +#' if (require(dplyr, quietly = TRUE)) { +#' nc2 <- nc %>% rename(area = AREA) +#' } rename.sf <- function(.data, ...) { if (!requireNamespace("tidyselect", quietly = TRUE)) @@ -212,7 +225,9 @@ rename.sf <- function(.data, ...) { #' @name tidyverse #' @examples -#' nc %>% slice(1:2) +#' if (require(dplyr, quietly = TRUE)) { +#' nc %>% slice(1:2) +#' } slice.sf <- function(.data, ..., .dots) { class(.data) <- setdiff(class(.data), "sf") sf_column <- attr(.data, "sf_column") @@ -229,11 +244,13 @@ slice.sf <- function(.data, ..., .dots) { #' #' In case \code{do_union} is \code{FALSE}, \code{summarise} will simply combine geometries using \link{c.sfg}. When polygons sharing a boundary are combined, this leads to geometries that are invalid; see for instance \url{https://github.com/r-spatial/sf/issues/681}. #' @examples -#' nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -#' nc.g <- nc %>% group_by(area_cl) -#' nc.g %>% summarise(mean(AREA)) -#' nc.g %>% summarise(mean(AREA)) %>% plot(col = grey(3:6 / 7)) -#' nc %>% as.data.frame %>% summarise(mean(AREA)) +#' if (require(dplyr, quietly = TRUE)) { +#' nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) +#' nc.g <- nc %>% group_by(area_cl) +#' nc.g %>% summarise(mean(AREA)) +#' nc.g %>% summarise(mean(AREA)) %>% plot(col = grey(3:6 / 7)) +#' nc %>% as.data.frame %>% summarise(mean(AREA)) +#' } summarise.sf <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) { sf_column = attr(.data, "sf_column") precision = st_precision(.data) @@ -276,7 +293,9 @@ summarise.sf <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE #' @name tidyverse #' @param .keep_all see corresponding function in dplyr #' @examples -#' nc[c(1:100, 1:10), ] %>% distinct() %>% nrow() +#' if (require(dplyr, quietly = TRUE)) { +#' nc[c(1:100, 1:10), ] %>% distinct() %>% nrow() +#' } #' @details \code{distinct} gives distinct records for which all attributes and geometries are distinct; \link{st_equals} is used to find out which geometries are distinct. distinct.sf <- function(.data, ..., .keep_all = FALSE) { sf_column = attr(.data, "sf_column") @@ -310,8 +329,9 @@ distinct.sf <- function(.data, ..., .keep_all = FALSE) { #' @param na.rm see original function docs #' @param factor_key see original function docs #' @examples -#' library(tidyr) -#' nc %>% select(SID74, SID79) %>% gather("VAR", "SID", -geometry) %>% summary() +#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) { +#' nc %>% select(SID74, SID79) %>% gather("VAR", "SID", -geometry) %>% summary() +#' } gather.sf <- function(data, key, value, ..., na.rm = FALSE, convert = FALSE, factor_key = FALSE) { if (! requireNamespace("rlang", quietly = TRUE)) @@ -415,11 +435,12 @@ pivot_wider.sf = function(data, #' @param fill see original function docs #' @param drop see original function docs #' @examples -#' library(tidyr) -#' nc$row = 1:100 # needed for spread to work -#' nc %>% select(SID74, SID79, geometry, row) %>% +#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) { +#' nc$row = 1:100 # needed for spread to work +#' nc %>% select(SID74, SID79, geometry, row) %>% #' gather("VAR", "SID", -geometry, -row) %>% #' spread(VAR, SID) %>% head() +#' } spread.sf <- function(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL) { @@ -450,12 +471,14 @@ sample_frac.sf <- function(tbl, size = 1, replace = FALSE, weight = NULL, .env = #' @name tidyverse #' @examples -#' storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) -#' x <- storms.sf %>% group_by(name, year) %>% nest -#' trs = lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]) %>% +#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) { +#' storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) +#' x <- storms.sf %>% group_by(name, year) %>% nest +#' trs = lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]) %>% #' st_sfc(crs = 4326) -#' trs.sf = st_sf(x[,1:2], trs) -#' plot(trs.sf["year"], axes = TRUE) +#' trs.sf = st_sf(x[,1:2], trs) +#' plot(trs.sf["year"], axes = TRUE) +#' } #' @details \code{nest} assumes that a simple feature geometry list-column was among the columns that were nested. nest.sf = function (.data, ...) { diff --git a/R/transform.R b/R/transform.R index 1943bc95c..0216dfbf6 100644 --- a/R/transform.R +++ b/R/transform.R @@ -167,12 +167,13 @@ st_wrap_dateline = function(x, options, quiet) UseMethod("st_wrap_dateline") #' @export #' @examples #' st_wrap_dateline(st_sfc(st_linestring(rbind(c(-179,0),c(179,0))), crs = 4326)) -#' library(maps) -#' wrld <- st_as_sf(maps::map("world", fill = TRUE, plot = FALSE)) -#' wrld_wrap <- st_wrap_dateline(wrld, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"), +#' if (require(maps, quietly = TRUE)) { +#' wrld <- st_as_sf(maps::map("world", fill = TRUE, plot = FALSE)) +#' wrld_wrap <- st_wrap_dateline(wrld, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"), #' quiet = TRUE) -#' wrld_moll <- st_transform(wrld_wrap, "+proj=moll") -#' plot(st_geometry(wrld_moll), col = "transparent") +#' wrld_moll <- st_transform(wrld_wrap, "+proj=moll") +#' plot(st_geometry(wrld_moll), col = "transparent") +#' } #' @details For a discussion of using \code{options}, see \url{https://github.com/r-spatial/sf/issues/280} and \url{https://github.com/r-spatial/sf/issues/541} st_wrap_dateline.sfc = function(x, options = "WRAPDATELINE=YES", quiet = TRUE) { if (is.na(st_crs(x))) diff --git a/R/valid.R b/R/valid.R index d6bbad312..bf16d6030 100644 --- a/R/valid.R +++ b/R/valid.R @@ -80,7 +80,7 @@ st_make_valid.sfc = function(x, ..., oriented = FALSE, s2_options = s2::s2_optio s2_options = s2::s2_options(snap = s2::s2_snap_precision(st_precision(x))) s2 = s2::s2_rebuild(s2, s2_options) st_as_sfc(s2, crs = crs) - } else if (compareVersion(CPL_geos_version(), "3.8.0") > -1) { + } else if (compareVersion(CPL_geos_version(), "3.8.0") == -1) { if (!requireNamespace("lwgeom", quietly = TRUE)) stop("package lwgeom required, please install it first") # nocov st_sfc(lwgeom::lwgeom_make_valid(x), crs = crs) diff --git a/man/Ops.Rd b/man/Ops.Rd index a31eada10..593959ff1 100644 --- a/man/Ops.Rd +++ b/man/Ops.Rd @@ -34,11 +34,12 @@ diag(m) = c(1, 3) # affine: st_point(c(1,2)) * m + c(2,5) # world in 0-360 range: -library(maps) -w = st_as_sf(map('world', plot = FALSE, fill = TRUE)) -w2 = (st_geometry(w) + c(360,90)) \%\% c(360) - c(0,90) -w3 = st_wrap_dateline(st_set_crs(w2 - c(180,0), 4326)) + c(180,0) -plot(st_set_crs(w3, 4326), axes = TRUE) +if (require(maps, quietly = TRUE)) { + w = st_as_sf(map('world', plot = FALSE, fill = TRUE)) + w2 = (st_geometry(w) + c(360,90)) \%\% c(360) - c(0,90) + w3 = st_wrap_dateline(st_set_crs(w2 - c(180,0), 4326)) + c(180,0) + plot(st_set_crs(w3, 4326), axes = TRUE) +} (mp <- st_point(c(1,2)) + st_point(c(3,4))) # MULTIPOINT (1 2, 3 4) mp - st_point(c(3,4)) # POINT (1 2) opar = par(mfrow = c(2,2), mar = c(0, 0, 1, 0)) diff --git a/man/bind.Rd b/man/bind.Rd index 4ca08cc3e..1a8df263a 100644 --- a/man/bind.Rd +++ b/man/bind.Rd @@ -45,11 +45,12 @@ rbind(a,b) rbind(a,b) rbind(b,c) cbind(a,b,c) # warns -if (require(dplyr)) +if (require(dplyr, quietly = TRUE)) dplyr::bind_cols(a,b) c = st_sf(a=4, geomc = st_sfc(st_multilinestring(list(matrix(1:4,2)))), crs = crs) cbind(a,b,c, sf_column_name = "geomc") df = data.frame(x=3) st_sf(data.frame(c, df)) -dplyr::bind_cols(c, df) +if (require(dplyr, quietly = TRUE)) + dplyr::bind_cols(c, df) } diff --git a/man/coerce-methods.Rd b/man/coerce-methods.Rd index aaea72517..bde8efde4 100644 --- a/man/coerce-methods.Rd +++ b/man/coerce-methods.Rd @@ -38,6 +38,7 @@ For converting simple features (i.e., \code{sf} objects) to their \code{Spatial} } \examples{ nc <- st_read(system.file("shape/nc.shp", package="sf")) +if (require(sp, quietly = TRUE)) { # convert to SpatialPolygonsDataFrame spdf <- as_Spatial(nc) # identical to @@ -47,3 +48,4 @@ as(st_geometry(nc), "Spatial") # back to sf as(spdf, "sf") } +} diff --git a/man/geos_unary.Rd b/man/geos_unary.Rd index e82d2729b..73819565d 100644 --- a/man/geos_unary.Rd +++ b/man/geos_unary.Rd @@ -244,7 +244,9 @@ if (compareVersion(sf_extSoftVersion()[["GEOS"]], "3.7.0") > -1) { st_polygonize(st_node(l)) st_node(st_multilinestring(list(rbind(c(0,0), c(1,1), c(0,1), c(1,0), c(0,0))))) sf = st_sf(a=1, geom=st_sfc(st_linestring(rbind(c(0,0),c(1,1)))), crs = 4326) -seg = st_segmentize(sf, units::set_units(100, km)) -seg = st_segmentize(sf, units::set_units(0.01, rad)) -nrow(seg$geom[[1]]) +if (require(lwgeom, quietly = TRUE)) { + seg = st_segmentize(sf, units::set_units(100, km)) + seg = st_segmentize(sf, units::set_units(0.01, rad)) + nrow(seg$geom[[1]]) +} } diff --git a/man/st_as_sf.Rd b/man/st_as_sf.Rd index 06b0cf26e..7dbe4e908 100644 --- a/man/st_as_sf.Rd +++ b/man/st_as_sf.Rd @@ -86,11 +86,13 @@ d$geom = c("POINT(0 0)", "POINT(0 1)") df = st_as_sf(d, wkt = "geom") d$geom2 = st_sfc(pt1, pt2) st_as_sf(d) # should warn -data(meuse, package = "sp") -meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") -meuse_sf[1:3,] -summary(meuse_sf) -library(sp) +if (require(sp, quietly = TRUE)) { + data(meuse, package = "sp") + meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") + meuse_sf[1:3,] + summary(meuse_sf) +} +if (require(sp, quietly = TRUE)) { x = rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1)) x1 = 0.1 * x + 0.1 x2 = 0.1 * x + 0.4 @@ -119,6 +121,7 @@ summary(st_as_sf(as(meuse.riv, "SpatialLines"))) pol.grd = as(meuse.grid, "SpatialPolygonsDataFrame") # summary(st_as_sf(pol.grd)) # summary(st_as_sf(as(pol.grd, "SpatialLinesDataFrame"))) +} if (require(spatstat.geom)) { g = st_as_sf(gorillas) # select only the points: diff --git a/man/st_crs.Rd b/man/st_crs.Rd index f40c88bc2..c80e17efb 100644 --- a/man/st_crs.Rd +++ b/man/st_crs.Rd @@ -133,9 +133,7 @@ sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) st_crs(sfc) = 4326 sfc sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) -library(dplyr) -x = sfc \%>\% st_set_crs(4326) \%>\% st_transform(3857) -x +sfc \%>\% st_set_crs(4326) \%>\% st_transform(3857) st_crs("EPSG:3857")$input st_crs(3857)$proj4string st_crs(3857)$b # numeric diff --git a/man/st_graticule.Rd b/man/st_graticule.Rd index 2cc37eafe..88bf353ca 100644 --- a/man/st_graticule.Rd +++ b/man/st_graticule.Rd @@ -55,7 +55,7 @@ Compute graticules and their parameters \examples{ library(sf) -library(maps) +if (require(maps, quietly = TRUE)) { usa = st_as_sf(map('usa', plot = FALSE, fill = TRUE)) laea = st_crs("+proj=laea +lat_0=30 +lon_0=-95") # Lambert equal area @@ -88,3 +88,4 @@ if (g$type[i] == "E" && g$y_end[i] - max(g$y_end) > -1000) })) plot(usa, graticule = st_crs(4326), axes = TRUE, lon = seq(-60,-130,by=-10)) } +} diff --git a/man/st_jitter.Rd b/man/st_jitter.Rd index bb72dc9cc..31f5206f1 100644 --- a/man/st_jitter.Rd +++ b/man/st_jitter.Rd @@ -20,7 +20,7 @@ jitter geometries jitters coordinates with an amount such that \code{runif(1, -amount, amount)} is added to the coordinates. x- and y-coordinates are jittered independently but all coordinates of a single geometry are jittered with the same amount, meaning that the geometry shape does not change. For longlat data, a latitude correction is made such that jittering in East and North directions are identical in distance in the center of the bounding box of \code{x}. } \examples{ -nc = read_sf(system.file("gpkg/nc.gpkg", package="sf")) +nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) pts = st_centroid(st_geometry(nc)) plot(pts) plot(st_jitter(pts, .05), add = TRUE, col = 'red') diff --git a/man/st_join.Rd b/man/st_join.Rd index fe1ec2e1c..5cecf908e 100644 --- a/man/st_join.Rd +++ b/man/st_join.Rd @@ -78,8 +78,9 @@ st_join(a, b) st_join(a, b, left = FALSE) # two ways to aggregate y's attribute values outcome over x's geometries: st_join(a, b) \%>\% aggregate(list(.$a.x), mean) -library(dplyr) -st_join(a, b) \%>\% group_by(a.x) \%>\% summarise(mean(a.y)) +if (require(dplyr, quietly = TRUE)) { + st_join(a, b) \%>\% group_by(a.x) \%>\% summarise(mean(a.y)) +} # example of largest = TRUE: nc <- st_transform(st_read(system.file("shape/nc.shp", package="sf")), 2264) gr = st_sf( diff --git a/man/st_make_grid.Rd b/man/st_make_grid.Rd index 1aefb0362..d4e15882b 100644 --- a/man/st_make_grid.Rd +++ b/man/st_make_grid.Rd @@ -48,7 +48,7 @@ plot(sfc, add = TRUE) # non-default offset: plot(st_make_grid(sfc, cellsize = .1, square = FALSE, offset = c(0, .05 / (sqrt(3)/2)))) plot(sfc, add = TRUE) -nc = read_sf(system.file("shape/nc.shp", package="sf")) +nc = st_read(system.file("shape/nc.shp", package="sf")) g = st_make_grid(nc) plot(g) plot(st_geometry(nc), add = TRUE) diff --git a/man/st_nearest_points.Rd b/man/st_nearest_points.Rd index 7a2bc643d..75926c847 100644 --- a/man/st_nearest_points.Rd +++ b/man/st_nearest_points.Rd @@ -48,7 +48,7 @@ plot(b1, xlim = c(-.2,1.2), ylim = c(-.2,1.2), col = NA, border = 'green') plot(st_sfc(b2, b3), add = TRUE, col = NA, border = 'blue') plot(ls, add = TRUE, col = 'red') -nc = read_sf(system.file("gpkg/nc.gpkg", package="sf")) +nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) plot(st_geometry(nc)) ls = st_nearest_points(nc[1,], nc) plot(ls, col = 'red', add = TRUE) diff --git a/man/st_read.Rd b/man/st_read.Rd index 7ecc50c63..0109b73d0 100644 --- a/man/st_read.Rd +++ b/man/st_read.Rd @@ -199,11 +199,11 @@ nc_gpkg_sql = st_read(system.file("gpkg/nc.gpkg", package = "sf"), # spatial filter, as wkt: wkt = st_as_text(st_geometry(nc[1,])) # filter by (bbox overlaps of) first feature geometry: -read_sf(system.file("gpkg/nc.gpkg", package="sf"), wkt_filter = wkt) +st_read(system.file("gpkg/nc.gpkg", package="sf"), wkt_filter = wkt) # read geojson from string: geojson_txt <- paste("{\"type\":\"MultiPoint\",\"coordinates\":", "[[3.2,4],[3,4.6],[3.8,4.4],[3.5,3.8],[3.4,3.6],[3.9,4.5]]}") -x = read_sf(geojson_txt) +x = st_read(geojson_txt) x \dontrun{ library(RPostgreSQL) diff --git a/man/st_sample.Rd b/man/st_sample.Rd index 08dc00274..03879c6d0 100644 --- a/man/st_sample.Rd +++ b/man/st_sample.Rd @@ -80,6 +80,7 @@ x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,90),c(0,90),c(0,0)))), crs plot(x, axes = TRUE, graticule = TRUE) if (sf_extSoftVersion()["proj.4"] >= "4.9.0") plot(p <- st_sample(x, 1000), add = TRUE) +if (require(lwgeom, quietly = TRUE)) { # for st_segmentize() x2 = st_transform(st_segmentize(x, 1e4), st_crs("+proj=ortho +lat_0=30 +lon_0=45")) g = st_transform(st_graticule(), st_crs("+proj=ortho +lat_0=30 +lon_0=45")) plot(x2, graticule = g) @@ -87,6 +88,7 @@ if (sf_extSoftVersion()["proj.4"] >= "4.9.0") { p2 = st_transform(p, st_crs("+proj=ortho +lat_0=30 +lon_0=45")) plot(p2, add = TRUE) } +} x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,10),c(0,90),c(0,0))))) # NOT long/lat: plot(x) p_exact = st_sample(x, 1000, exact = TRUE) diff --git a/man/st_transform.Rd b/man/st_transform.Rd index 84a6db023..6e3c0a0fa 100644 --- a/man/st_transform.Rd +++ b/man/st_transform.Rd @@ -121,12 +121,13 @@ library(units) set_units(st_area(st_transform(nc[1,], 2264)), m^2) st_transform(structure(p1, proj4string = "+init=epsg:4326"), "+init=epsg:3857") st_wrap_dateline(st_sfc(st_linestring(rbind(c(-179,0),c(179,0))), crs = 4326)) -library(maps) -wrld <- st_as_sf(maps::map("world", fill = TRUE, plot = FALSE)) -wrld_wrap <- st_wrap_dateline(wrld, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"), +if (require(maps, quietly = TRUE)) { + wrld <- st_as_sf(maps::map("world", fill = TRUE, plot = FALSE)) + wrld_wrap <- st_wrap_dateline(wrld, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"), quiet = TRUE) -wrld_moll <- st_transform(wrld_wrap, "+proj=moll") -plot(st_geometry(wrld_moll), col = "transparent") + wrld_moll <- st_transform(wrld_wrap, "+proj=moll") + plot(st_geometry(wrld_moll), col = "transparent") +} sf_proj_info("datum") } \seealso{ diff --git a/man/st_write.Rd b/man/st_write.Rd index 7108c8cc3..6e0b30d3f 100644 --- a/man/st_write.Rd +++ b/man/st_write.Rd @@ -118,12 +118,13 @@ omitted; it returns TRUE on success, FALSE on failure, invisibly. nc = st_read(system.file("shape/nc.shp", package="sf")) st_write(nc, paste0(tempdir(), "/", "nc.shp")) st_write(nc, paste0(tempdir(), "/", "nc.shp"), delete_layer = TRUE) # overwrites -data(meuse, package = "sp") # loads data.frame from sp -meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) -# writes X and Y as columns: -st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_XY") -st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_WKT", - delete_dsn=TRUE) # overwrites +if (require(sp, quietly = TRUE)) { + data(meuse, package = "sp") # loads data.frame from sp + meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) + # writes X and Y as columns: + st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_XY") + st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_WKT", + delete_dsn=TRUE) # overwrites \dontrun{ library(sp) example(meuse, ask = FALSE, echo = FALSE) @@ -133,6 +134,7 @@ st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETR try(st_write(nc, "PG:dbname=postgis", "sids", layer_options = "OVERWRITE=true")) } } +} \seealso{ \link{st_drivers}, \link{dbWriteTable} } diff --git a/man/tidyverse.Rd b/man/tidyverse.Rd index 8118137f3..d3ed2c8a3 100644 --- a/man/tidyverse.Rd +++ b/man/tidyverse.Rd @@ -296,41 +296,64 @@ In case \code{do_union} is \code{FALSE}, \code{summarise} will simply combine ge \code{nest} assumes that a simple feature geometry list-column was among the columns that were nested. } \examples{ -library(dplyr) -nc = st_read(system.file("shape/nc.shp", package="sf")) -nc \%>\% filter(AREA > .1) \%>\% plot() +if (require(dplyr, quietly = TRUE)) { + nc = read_sf(system.file("shape/nc.shp", package="sf")) + nc \%>\% filter(AREA > .1) \%>\% plot() +} # plot 10 smallest counties in grey: -st_geometry(nc) \%>\% plot() -nc \%>\% select(AREA) \%>\% arrange(AREA) \%>\% slice(1:10) \%>\% plot(add = TRUE, col = 'grey') -title("the ten counties with smallest area") -nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -nc \%>\% group_by(area_cl) \%>\% class() -nc2 <- nc \%>\% mutate(area10 = AREA/10) -nc \%>\% transmute(AREA = AREA/10, geometry = geometry) \%>\% class() -nc \%>\% transmute(AREA = AREA/10) \%>\% class() -nc \%>\% select(SID74, SID79) \%>\% names() -nc \%>\% select(SID74, SID79, geometry) \%>\% names() -nc \%>\% select(SID74, SID79) \%>\% class() -nc \%>\% select(SID74, SID79, geometry) \%>\% class() -nc2 <- nc \%>\% rename(area = AREA) -nc \%>\% slice(1:2) -nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -nc.g <- nc \%>\% group_by(area_cl) -nc.g \%>\% summarise(mean(AREA)) -nc.g \%>\% summarise(mean(AREA)) \%>\% plot(col = grey(3:6 / 7)) -nc \%>\% as.data.frame \%>\% summarise(mean(AREA)) -nc[c(1:100, 1:10), ] \%>\% distinct() \%>\% nrow() -library(tidyr) -nc \%>\% select(SID74, SID79) \%>\% gather("VAR", "SID", -geometry) \%>\% summary() -library(tidyr) -nc$row = 1:100 # needed for spread to work -nc \%>\% select(SID74, SID79, geometry, row) \%>\% +if (require(dplyr, quietly = TRUE)) { + st_geometry(nc) \%>\% plot() + nc \%>\% select(AREA) \%>\% arrange(AREA) \%>\% slice(1:10) \%>\% plot(add = TRUE, col = 'grey') + title("the ten counties with smallest area") +} +if (require(dplyr, quietly = TRUE)) { + nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) + nc \%>\% group_by(area_cl) \%>\% class() +} +if (require(dplyr, quietly = TRUE)) { + nc2 <- nc \%>\% mutate(area10 = AREA/10) +} +if (require(dplyr, quietly = TRUE)) { + nc \%>\% transmute(AREA = AREA/10, geometry = geometry) \%>\% class() + nc \%>\% transmute(AREA = AREA/10) \%>\% class() +} +if (require(dplyr, quietly = TRUE)) { + nc \%>\% select(SID74, SID79) \%>\% names() + nc \%>\% select(SID74, SID79, geometry) \%>\% names() + nc \%>\% select(SID74, SID79) \%>\% class() + nc \%>\% select(SID74, SID79, geometry) \%>\% class() +} +if (require(dplyr, quietly = TRUE)) { + nc2 <- nc \%>\% rename(area = AREA) +} +if (require(dplyr, quietly = TRUE)) { + nc \%>\% slice(1:2) +} +if (require(dplyr, quietly = TRUE)) { + nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) + nc.g <- nc \%>\% group_by(area_cl) + nc.g \%>\% summarise(mean(AREA)) + nc.g \%>\% summarise(mean(AREA)) \%>\% plot(col = grey(3:6 / 7)) + nc \%>\% as.data.frame \%>\% summarise(mean(AREA)) +} +if (require(dplyr, quietly = TRUE)) { + nc[c(1:100, 1:10), ] \%>\% distinct() \%>\% nrow() +} +if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) { + nc \%>\% select(SID74, SID79) \%>\% gather("VAR", "SID", -geometry) \%>\% summary() +} +if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) { + nc$row = 1:100 # needed for spread to work + nc \%>\% select(SID74, SID79, geometry, row) \%>\% gather("VAR", "SID", -geometry, -row) \%>\% spread(VAR, SID) \%>\% head() -storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) -x <- storms.sf \%>\% group_by(name, year) \%>\% nest -trs = lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]) \%>\% +} +if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) { + storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) + x <- storms.sf \%>\% group_by(name, year) \%>\% nest + trs = lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]) \%>\% st_sfc(crs = 4326) -trs.sf = st_sf(x[,1:2], trs) -plot(trs.sf["year"], axes = TRUE) + trs.sf = st_sf(x[,1:2], trs) + plot(trs.sf["year"], axes = TRUE) +} } diff --git a/tests/aggregate.Rout.save b/tests/aggregate.Rout.save index 436e8fac8..aec1b1a3b 100644 --- a/tests/aggregate.Rout.save +++ b/tests/aggregate.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -16,7 +16,7 @@ Type 'demo()' for some demos, 'help()' for on-line help, or Type 'q()' to quit R. > library(sf) -Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE +Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE > # aggregate > pl1 = st_polygon(list(rbind(c(0,0),c(1,0),c(1,1),c(0,0)))) > pl2 = st_polygon(list(rbind(c(0,0),c(1,1),c(0,1),c(0,0)))) @@ -40,39 +40,38 @@ CRS: NA Group.1 a geometry 1 1 1.5 POLYGON ((1 0, 0 0, 0 1, 1 ... > # expect_warning(st_cast(a, "POINT")) -> demo(meuse_sf, echo = FALSE, ask = FALSE) -> a = aggregate(meuse_sf, list(meuse_sf$soil), mean) -There were 12 warnings (use warnings() to see them) -> attributes(a)$agr +> if (require(sp, quietly = TRUE)) { ++ demo(meuse_sf, echo = FALSE, ask = FALSE) ++ a = aggregate(meuse_sf, list(meuse_sf$soil), mean) ++ print(attributes(a)$agr) ++ a = aggregate(meuse_sf, list(soil = meuse_sf$soil), mean) ++ print(attributes(a)$agr) ++ a = aggregate(meuse_sf, list(meuse_sf$soil, meuse_sf$ffreq), mean) ++ print(attributes(a)$agr) ++ a = aggregate(meuse_sf, list(soil = meuse_sf$soil, ff = meuse_sf$ffreq), mean) ++ print(attributes(a)$agr) ++ } Group.1 cadmium copper lead zinc elev dist om identity aggregate aggregate aggregate aggregate aggregate aggregate aggregate ffreq soil lime landuse dist.m aggregate aggregate aggregate aggregate aggregate Levels: constant aggregate identity -> a = aggregate(meuse_sf, list(soil = meuse_sf$soil), mean) -There were 12 warnings (use warnings() to see them) -> attributes(a)$agr soil cadmium copper lead zinc elev dist om identity aggregate aggregate aggregate aggregate aggregate aggregate aggregate ffreq soil.1 lime landuse dist.m aggregate aggregate aggregate aggregate aggregate Levels: constant aggregate identity -> a = aggregate(meuse_sf, list(meuse_sf$soil, meuse_sf$ffreq), mean) -There were 32 warnings (use warnings() to see them) -> attributes(a)$agr Group.1 Group.2 cadmium copper lead zinc elev dist identity identity aggregate aggregate aggregate aggregate aggregate aggregate om ffreq soil lime landuse dist.m aggregate aggregate aggregate aggregate aggregate aggregate Levels: constant aggregate identity -> a = aggregate(meuse_sf, list(soil = meuse_sf$soil, ff = meuse_sf$ffreq), mean) -There were 32 warnings (use warnings() to see them) -> attributes(a)$agr soil ff cadmium copper lead zinc elev dist identity identity aggregate aggregate aggregate aggregate aggregate aggregate om ffreq soil.1 lime landuse dist.m aggregate aggregate aggregate aggregate aggregate aggregate Levels: constant aggregate identity +There were 50 or more warnings (use warnings() to see the first 50) > > # aggregate by sf/sfc > a = st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0)))) * 2 @@ -110,4 +109,4 @@ CRS: NA > > proc.time() user system elapsed - 0.638 0.067 0.698 + 1.177 0.056 1.225 diff --git a/tests/crs.Rout.save b/tests/crs.Rout.save index 152857dd1..8ab980f4b 100644 --- a/tests/crs.Rout.save +++ b/tests/crs.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -82,8 +82,17 @@ POINT (111319.5 111325.1) + } [1] "datum files installed" > -> x0 = sp::CRS("+init=epsg:4326") -> cat(sp::wkt(x0), "\n") +> if (require(sp, quietly = TRUE)) { ++ x0 = sp::CRS("+init=epsg:4326") ++ cat(sp::wkt(x0), "\n") ++ x = st_crs(x0) ++ print(x) ++ ++ y0 = st_crs(4326) ++ y0 ++ y = as(y0, "CRS") ++ cat(sp::wkt(y), "\n") ++ } GEOGCRS["WGS 84", ENSEMBLE["World Geodetic System 1984 ensemble", MEMBER["World Geodetic System 1984 (Transit)", @@ -121,8 +130,6 @@ GEOGCRS["WGS 84", SCOPE["unknown"], AREA["World."], BBOX[-90,-180,90,180]]] -> x = st_crs(x0) -> x Coordinate Reference System: User input: WGS 84 wkt: @@ -163,40 +170,6 @@ GEOGCRS["WGS 84", SCOPE["unknown"], AREA["World."], BBOX[-90,-180,90,180]]] -> -> y0 = st_crs(4326) -> y0 -Coordinate Reference System: - User input: EPSG:4326 - wkt: -GEOGCRS["WGS 84", - ENSEMBLE["World Geodetic System 1984 ensemble", - MEMBER["World Geodetic System 1984 (Transit)"], - MEMBER["World Geodetic System 1984 (G730)"], - MEMBER["World Geodetic System 1984 (G873)"], - MEMBER["World Geodetic System 1984 (G1150)"], - MEMBER["World Geodetic System 1984 (G1674)"], - MEMBER["World Geodetic System 1984 (G1762)"], - MEMBER["World Geodetic System 1984 (G2139)"], - ELLIPSOID["WGS 84",6378137,298.257223563, - LENGTHUNIT["metre",1]], - ENSEMBLEACCURACY[2.0]], - PRIMEM["Greenwich",0, - ANGLEUNIT["degree",0.0174532925199433]], - CS[ellipsoidal,2], - AXIS["geodetic latitude (Lat)",north, - ORDER[1], - ANGLEUNIT["degree",0.0174532925199433]], - AXIS["geodetic longitude (Lon)",east, - ORDER[2], - ANGLEUNIT["degree",0.0174532925199433]], - USAGE[ - SCOPE["Horizontal component of 3D system."], - AREA["World."], - BBOX[-90,-180,90,180]], - ID["EPSG",4326]] -> y = as(y0, "CRS") -> cat(sp::wkt(y), "\n") GEOGCRS["WGS 84", ENSEMBLE["World Geodetic System 1984 ensemble", MEMBER["World Geodetic System 1984 (Transit)"], @@ -240,4 +213,4 @@ GEOGCRS["WGS 84", > > proc.time() user system elapsed - 0.874 0.060 0.927 + 1.297 0.072 1.380 diff --git a/tests/dist.Rout.save b/tests/dist.Rout.save index 2ec87dd1b..ebced7abc 100644 --- a/tests/dist.Rout.save +++ b/tests/dist.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.0 (2021-05-18) -- "Camp Pontanezen" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -16,7 +16,6 @@ Type 'demo()' for some demos, 'help()' for on-line help, or Type 'q()' to quit R. > suppressPackageStartupMessages(library(sf)) -> library(sp) > suppressPackageStartupMessages(library(units)) > > x = st_sfc( @@ -36,23 +35,25 @@ Type 'q()' to quit R. + crs = 4326 + ) > -> d.sf = st_distance(x, y) -> d.sp = spDists(as(x, "Spatial"), as(y, "Spatial")) -> units(d.sp) = as_units("km") -> round(d.sf - d.sp, 7) +> if (require(sp, quietly = TRUE)) { ++ d.sf = st_distance(x, y) ++ d.sp = spDists(as(x, "Spatial"), as(y, "Spatial")) ++ units(d.sp) = as_units("km") ++ print(round(d.sf - d.sp, 7)) ++ ++ #summary(unclass(d.sf) - d.sp) ++ ++ st_crs(x) = st_crs(y) = NA ++ d.sf = st_distance(x, y) ++ d.sp = spDists(as(x, "Spatial"), as(y, "Spatial")) ++ print(round(d.sf - d.sp, 7)) ++ } Units: [m] [,1] [,2] [,3] [,4] [,5] [1,] 6107.765 -124.3896 -248.7792 -373.1688 -497.5585 [2,] 6065.138 0.0000 -124.3896 -248.7792 -373.1688 [3,] 5940.569 -124.3896 0.0000 -124.3896 -248.7792 [4,] 5743.252 -248.7792 -124.3896 0.0000 -124.3896 -> -> #summary(unclass(d.sf) - d.sp) -> -> st_crs(x) = st_crs(y) = NA -> d.sf = st_distance(x, y) -> d.sp = spDists(as(x, "Spatial"), as(y, "Spatial")) -> round(d.sf - d.sp, 7) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 @@ -80,7 +81,7 @@ Units: [m] > st_length(z) 1451026 [m] > st_length(z) - sum(d[1,2], d[2,3], d[3,4], d[4,5]) -2.328306e-10 [m] +0 [m] > > # st_line_sample: > ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))), @@ -108,4 +109,4 @@ Error in st_line_sample(ls, density = 1/1000) : > > proc.time() user system elapsed - 1.856 0.097 2.456 + 1.343 0.096 1.431 diff --git a/tests/dplyr.R b/tests/dplyr.R index 1c326611a..50e6886a1 100644 --- a/tests/dplyr.R +++ b/tests/dplyr.R @@ -1,111 +1,112 @@ suppressPackageStartupMessages(library(sf)) -library(dplyr) -options(dplyr.summarise.inform=FALSE) -read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% +if (require(dplyr, quietly = TRUE)) { + options(dplyr.summarise.inform=FALSE) + read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% st_transform(3857) -> nc -nc %>% filter(AREA > .1) %>% plot() + nc %>% filter(AREA > .1) %>% plot() # plot 10 smallest counties in grey: -nc %>% - select(BIR74, geometry) %>% - plot() + nc %>% + select(BIR74, geometry) %>% + plot() -nc %>% + nc %>% select(AREA, geometry) %>% arrange(AREA) %>% slice(1:10) %>% plot(add = TRUE, col = 'grey', main ="") # select: check both when geometry is part of the selection, and when not: -nc %>% select(SID74, SID79) %>% names() -nc %>% select(SID74, SID79, geometry) %>% names() -nc %>% select(SID74, SID79) %>% class() -nc %>% select(SID74, SID79, geometry) %>% class() + nc %>% select(SID74, SID79) %>% names() + nc %>% select(SID74, SID79, geometry) %>% names() + nc %>% select(SID74, SID79) %>% class() + nc %>% select(SID74, SID79, geometry) %>% class() # group_by: -nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -nc %>% group_by(area_cl) %>% class() + nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) + nc %>% group_by(area_cl) %>% class() # mutate: -nc2 <- nc %>% mutate(area10 = AREA/10) + nc2 <- nc %>% mutate(area10 = AREA/10) # transmute: -nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class() -nc %>% transmute(AREA = AREA/10) %>% class() + nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class() + nc %>% transmute(AREA = AREA/10) %>% class() # rename: -nc2 <- nc %>% rename(area = AREA) + nc2 <- nc %>% rename(area = AREA) # distinct: -nc[c(1:100,1:10),] %>% distinct() %>% nrow() + nc[c(1:100,1:10),] %>% distinct() %>% nrow() # summarize: -nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -nc.g <- nc %>% group_by(area_cl) -nc.g %>% summarise(mean(AREA)) -nc.g %>% summarize(mean(AREA)) %>% plot(col = 3:6/7) + nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) + nc.g <- nc %>% group_by(area_cl) + nc.g %>% summarise(mean(AREA)) + nc.g %>% summarize(mean(AREA)) %>% plot(col = 3:6/7) -library(tidyr) + library(tidyr) # time-wide to long table, using tidyr::gather # stack the two SID columns for the July 1, 1974 - June 30, 1978 and July 1, 1979 - June 30, 1984 periods # (see https://cran.r-project.org/web/packages/spdep/vignettes/sids.pdf) -nc %>% select(SID74, SID79, geometry) %>% gather("VAR", "SID", -geometry) %>% summary() + nc %>% select(SID74, SID79, geometry) %>% gather("VAR", "SID", -geometry) %>% summary() # spread: -nc$row = 1:100 -nc.g <- nc %>% select(SID74, SID79, row) %>% gather("VAR", "SID", -row, -geometry) -nc.g %>% tail() -nc.g %>% spread(VAR, SID) %>% head() -nc %>% select(SID74, SID79, geometry, row) %>% gather("VAR", "SID", -geometry, -row) %>% spread(VAR, SID) %>% head() + nc$row = 1:100 + nc.g <- nc %>% select(SID74, SID79, row) %>% gather("VAR", "SID", -row, -geometry) + nc.g %>% tail() + nc.g %>% spread(VAR, SID) %>% head() + nc %>% select(SID74, SID79, geometry, row) %>% gather("VAR", "SID", -geometry, -row) %>% spread(VAR, SID) %>% head() # test st_set_crs in pipe: -sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) -x <- sfc %>% st_set_crs(4326) %>% st_transform(3857) -x + sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) + x <- sfc %>% st_set_crs(4326) %>% st_transform(3857) + x read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% st_transform(3857) -> nc -nc.merc <- st_transform(nc, 32119) # NC State Plane -suppressPackageStartupMessages(library(units)) -install_unit("person") -person = as_units("person") -nc.merc <- nc.merc %>% mutate(area = st_area(nc.merc), dens = BIR74 * person / area) + nc.merc <- st_transform(nc, 32119) # NC State Plane + suppressPackageStartupMessages(library(units)) + install_unit("person") + person = as_units("person") + nc.merc <- nc.merc %>% mutate(area = st_area(nc.merc), dens = BIR74 * person / area) # summary(nc.merc$dens) # requires units 0.4-2 -nc.merc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25)) -nc.grp <- nc.merc %>% group_by(area_cl) + nc.merc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25)) + nc.grp <- nc.merc %>% group_by(area_cl) -out <- nc.grp %>% summarise(A = sum(area), pop = sum(dens * area), + out <- nc.grp %>% summarise(A = sum(area), pop = sum(dens * area), new_dens = sum(dens * area)/sum(area)) # mean densities depend on grouping: -nc.merc %>% summarize(mean(dens)) -out %>% summarise(mean(new_dens)) + nc.merc %>% summarize(mean(dens)) + out %>% summarise(mean(new_dens)) # total densities don't: -nc.merc %>% summarise(sum(area * dens)) -out %>% summarise(sum(A * new_dens)) + nc.merc %>% summarise(sum(area * dens)) + out %>% summarise(sum(A * new_dens)) -conn = system.file("gpkg/nc.gpkg", package = "sf") + conn = system.file("gpkg/nc.gpkg", package = "sf") -library(DBI) -library(RSQLite) -con = dbConnect(SQLite(), dbname = system.file("gpkg/nc.gpkg", package = "sf")) -dbReadTable(con, "nc.gpkg") %>% filter(AREA > 0.2) %>% collect %>% st_sf +if (require(DBI, quietly = TRUE) && require(RSQLite, quietly = TRUE)) { + con = dbConnect(SQLite(), dbname = system.file("gpkg/nc.gpkg", package = "sf")) + dbReadTable(con, "nc.gpkg") %>% filter(AREA > 0.2) %>% collect %>% st_sf # nest: -storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) -x <- storms.sf %>% group_by(name, year) %>% nest + storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) + x <- storms.sf %>% group_by(name, year) %>% nest -nrow(distinct(nc[c(1,1,1,2,2,3:100),])) + nrow(distinct(nc[c(1,1,1,2,2,3:100),])) # set.seed(1331) -nc$gp <- sample(10, 100, replace=TRUE) + nc$gp <- sample(10, 100, replace=TRUE) # Get centroid of each group of polygons; https://github.com/r-spatial/sf/issues/969 -nc_gp_cent <- nc %>% + nc_gp_cent <- nc %>% group_by(gp) %>% group_map(st_area) -nc %>% st_filter(nc[1,]) %>% nrow + nc %>% st_filter(nc[1,]) %>% nrow +} # DBI & SQLITE +} # dplyr diff --git a/tests/dplyr.Rout.save b/tests/dplyr.Rout.save index e2d546e0f..921ae8117 100644 --- a/tests/dplyr.Rout.save +++ b/tests/dplyr.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -17,7 +17,116 @@ Type 'q()' to quit R. > suppressPackageStartupMessages(library(sf)) > -> library(dplyr) +> if (require(dplyr, quietly = TRUE)) { ++ options(dplyr.summarise.inform=FALSE) ++ read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% ++ st_transform(3857) -> nc ++ nc %>% filter(AREA > .1) %>% plot() ++ ++ # plot 10 smallest counties in grey: ++ nc %>% ++ select(BIR74, geometry) %>% ++ plot() ++ ++ nc %>% ++ select(AREA, geometry) %>% ++ arrange(AREA) %>% ++ slice(1:10) %>% ++ plot(add = TRUE, col = 'grey', main ="") ++ ++ # select: check both when geometry is part of the selection, and when not: ++ nc %>% select(SID74, SID79) %>% names() ++ nc %>% select(SID74, SID79, geometry) %>% names() ++ nc %>% select(SID74, SID79) %>% class() ++ nc %>% select(SID74, SID79, geometry) %>% class() ++ ++ # group_by: ++ nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) ++ nc %>% group_by(area_cl) %>% class() ++ ++ # mutate: ++ nc2 <- nc %>% mutate(area10 = AREA/10) ++ ++ # transmute: ++ nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class() ++ nc %>% transmute(AREA = AREA/10) %>% class() ++ ++ # rename: ++ nc2 <- nc %>% rename(area = AREA) ++ ++ # distinct: ++ nc[c(1:100,1:10),] %>% distinct() %>% nrow() ++ ++ # summarize: ++ nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) ++ nc.g <- nc %>% group_by(area_cl) ++ nc.g %>% summarise(mean(AREA)) ++ nc.g %>% summarize(mean(AREA)) %>% plot(col = 3:6/7) ++ ++ library(tidyr) ++ ++ # time-wide to long table, using tidyr::gather ++ # stack the two SID columns for the July 1, 1974 - June 30, 1978 and July 1, 1979 - June 30, 1984 periods ++ # (see https://cran.r-project.org/web/packages/spdep/vignettes/sids.pdf) ++ nc %>% select(SID74, SID79, geometry) %>% gather("VAR", "SID", -geometry) %>% summary() ++ ++ # spread: ++ nc$row = 1:100 ++ nc.g <- nc %>% select(SID74, SID79, row) %>% gather("VAR", "SID", -row, -geometry) ++ nc.g %>% tail() ++ nc.g %>% spread(VAR, SID) %>% head() ++ nc %>% select(SID74, SID79, geometry, row) %>% gather("VAR", "SID", -geometry, -row) %>% spread(VAR, SID) %>% head() ++ ++ # test st_set_crs in pipe: ++ sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) ++ x <- sfc %>% st_set_crs(4326) %>% st_transform(3857) ++ x ++ ++ read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% ++ st_transform(3857) -> nc ++ nc.merc <- st_transform(nc, 32119) # NC State Plane ++ suppressPackageStartupMessages(library(units)) ++ install_unit("person") ++ person = as_units("person") ++ nc.merc <- nc.merc %>% mutate(area = st_area(nc.merc), dens = BIR74 * person / area) ++ ++ # summary(nc.merc$dens) # requires units 0.4-2 ++ nc.merc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25)) ++ nc.grp <- nc.merc %>% group_by(area_cl) ++ ++ out <- nc.grp %>% summarise(A = sum(area), pop = sum(dens * area), ++ new_dens = sum(dens * area)/sum(area)) ++ ++ # mean densities depend on grouping: ++ nc.merc %>% summarize(mean(dens)) ++ out %>% summarise(mean(new_dens)) ++ ++ # total densities don't: ++ nc.merc %>% summarise(sum(area * dens)) ++ out %>% summarise(sum(A * new_dens)) ++ ++ conn = system.file("gpkg/nc.gpkg", package = "sf") ++ ++ if (require(DBI, quietly = TRUE) && require(RSQLite, quietly = TRUE)) { ++ con = dbConnect(SQLite(), dbname = system.file("gpkg/nc.gpkg", package = "sf")) ++ dbReadTable(con, "nc.gpkg") %>% filter(AREA > 0.2) %>% collect %>% st_sf ++ ++ # nest: ++ storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) ++ x <- storms.sf %>% group_by(name, year) %>% nest ++ ++ nrow(distinct(nc[c(1,1,1,2,2,3:100),])) ++ ++ # set.seed(1331) ++ nc$gp <- sample(10, 100, replace=TRUE) ++ # Get centroid of each group of polygons; https://github.com/r-spatial/sf/issues/969 ++ nc_gp_cent <- nc %>% ++ group_by(gp) %>% ++ group_map(st_area) ++ ++ nc %>% st_filter(nc[1,]) %>% nrow ++ } # DBI & SQLITE ++ } # dplyr Attaching package: 'dplyr' @@ -29,259 +138,10 @@ The following objects are masked from 'package:base': intersect, setdiff, setequal, union -> options(dplyr.summarise.inform=FALSE) -> read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% -+ st_transform(3857) -> nc -> nc %>% filter(AREA > .1) %>% plot() +[1] 4 Warning message: plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all > -> # plot 10 smallest counties in grey: -> nc %>% -+ select(BIR74, geometry) %>% -+ plot() -> -> nc %>% -+ select(AREA, geometry) %>% -+ arrange(AREA) %>% -+ slice(1:10) %>% -+ plot(add = TRUE, col = 'grey', main ="") -> -> # select: check both when geometry is part of the selection, and when not: -> nc %>% select(SID74, SID79) %>% names() -[1] "SID74" "SID79" "geometry" -> nc %>% select(SID74, SID79, geometry) %>% names() -[1] "SID74" "SID79" "geometry" -> nc %>% select(SID74, SID79) %>% class() -[1] "sf" "tbl_df" "tbl" "data.frame" -> nc %>% select(SID74, SID79, geometry) %>% class() -[1] "sf" "tbl_df" "tbl" "data.frame" -> -> # group_by: -> nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -> nc %>% group_by(area_cl) %>% class() -[1] "sf" "grouped_df" "tbl_df" "tbl" "data.frame" -> -> # mutate: -> nc2 <- nc %>% mutate(area10 = AREA/10) -> -> # transmute: -> nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class() -[1] "sf" "tbl_df" "tbl" "data.frame" -> nc %>% transmute(AREA = AREA/10) %>% class() -[1] "sf" "tbl_df" "tbl" "data.frame" -> -> # rename: -> nc2 <- nc %>% rename(area = AREA) -> -> # distinct: -> nc[c(1:100,1:10),] %>% distinct() %>% nrow() -[1] 100 -> -> # summarize: -> nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25)) -> nc.g <- nc %>% group_by(area_cl) -> nc.g %>% summarise(mean(AREA)) -Simple feature collection with 4 features and 2 fields -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -9386880 ymin: 4012991 xmax: -8399788 ymax: 4382079 -Projected CRS: WGS 84 / Pseudo-Mercator -# A tibble: 4 × 3 - area_cl `mean(AREA)` geometry - -1 (0,0.1] 0.0760 (((-8863447 4143479, -8857175 4151339, -8858837 4156… -2 (0.1,0.12] 0.112 (((-9362143 4197312, -9355589 4200996, -9354263 4203… -3 (0.12,0.15] 0.134 (((-9302071 4171499, -9311146 4181234, -9311292 4183… -4 (0.15,0.25] 0.190 (((-8830723 4133018, -8832558 4137955, -8832096 4141… -> nc.g %>% summarize(mean(AREA)) %>% plot(col = 3:6/7) -> -> library(tidyr) -> -> # time-wide to long table, using tidyr::gather -> # stack the two SID columns for the July 1, 1974 - June 30, 1978 and July 1, 1979 - June 30, 1984 periods -> # (see https://cran.r-project.org/web/packages/spdep/vignettes/sids.pdf) -> nc %>% select(SID74, SID79, geometry) %>% gather("VAR", "SID", -geometry) %>% summary() - geometry VAR SID - MULTIPOLYGON :200 Length:200 Min. : 0.000 - epsg:3857 : 0 Class :character 1st Qu.: 2.000 - +proj=merc...: 0 Mode :character Median : 5.000 - Mean : 7.515 - 3rd Qu.: 9.000 - Max. :57.000 -> -> # spread: -> nc$row = 1:100 -> nc.g <- nc %>% select(SID74, SID79, row) %>% gather("VAR", "SID", -row, -geometry) -> nc.g %>% tail() -Simple feature collection with 6 features and 3 fields -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -8802506 ymin: 4012991 xmax: -8492268 ymax: 4166167 -Projected CRS: WGS 84 / Pseudo-Mercator -# A tibble: 6 × 4 - row geometry VAR SID - -1 95 (((-8588146 4131923, -8589850 4133303, -8589356 4135198, -8… SID79 4 -2 96 (((-8711999 4081959, -8719511 4077863, -8731642 4078864, -8… SID79 5 -3 97 (((-8685774 4073056, -8697387 4077823, -8700120 4077570, -8… SID79 3 -4 98 (((-8755885 4021935, -8802506 4069795, -8798771 4071779, -8… SID79 17 -5 99 (((-8678517 4054264, -8679088 4061405, -8680136 4061550, -8… SID79 9 -6 100 (((-8755885 4021935, -8753548 4025868, -8753052 4030195, -8… SID79 6 -> nc.g %>% spread(VAR, SID) %>% head() -Simple feature collection with 6 features and 3 fields -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -9099356 ymin: 4310668 xmax: -8434988 ymax: 4382079 -Projected CRS: WGS 84 / Pseudo-Mercator -# A tibble: 6 × 4 - row geometry SID74 SID79 - -1 1 (((-9069486 4332934, -9077066 4338201, -9079419 4338351, -9… 1 0 -2 2 (((-9043562 4351030, -9043652 4352973, -9046117 4356516, -9… 0 3 -3 3 (((-8956335 4334068, -8958566 4335747, -8965300 4336025, -8… 5 6 -4 4 (((-8461241 4344709, -8462173 4347214, -8463902 4346972, -8… 1 2 -5 5 (((-8595797 4333852, -8597683 4330212, -8604808 4329788, -8… 9 3 -6 6 (((-8543185 4332878, -8569416 4332369, -8570981 4333107, -8… 7 5 -> nc %>% select(SID74, SID79, geometry, row) %>% gather("VAR", "SID", -geometry, -row) %>% spread(VAR, SID) %>% head() -Simple feature collection with 6 features and 3 fields -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -9099356 ymin: 4310668 xmax: -8434988 ymax: 4382079 -Projected CRS: WGS 84 / Pseudo-Mercator -# A tibble: 6 × 4 - geometry row SID74 SID79 - -1 (((-9069486 4332934, -9077066 4338201, -9079419 4338351, -9… 1 1 0 -2 (((-9043562 4351030, -9043652 4352973, -9046117 4356516, -9… 2 0 3 -3 (((-8956335 4334068, -8958566 4335747, -8965300 4336025, -8… 3 5 6 -4 (((-8461241 4344709, -8462173 4347214, -8463902 4346972, -8… 4 1 2 -5 (((-8595797 4333852, -8597683 4330212, -8604808 4329788, -8… 5 9 3 -6 (((-8543185 4332878, -8569416 4332369, -8570981 4333107, -8… 6 7 5 -> -> # test st_set_crs in pipe: -> sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) -> x <- sfc %>% st_set_crs(4326) %>% st_transform(3857) -> x -Geometry set for 2 features -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 111319.5 ymax: 111325.1 -Projected CRS: WGS 84 / Pseudo-Mercator -POINT (0 0) -POINT (111319.5 111325.1) -> -> read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% -+ st_transform(3857) -> nc -> nc.merc <- st_transform(nc, 32119) # NC State Plane -> suppressPackageStartupMessages(library(units)) -> install_unit("person") -> person = as_units("person") -> nc.merc <- nc.merc %>% mutate(area = st_area(nc.merc), dens = BIR74 * person / area) -> -> # summary(nc.merc$dens) # requires units 0.4-2 -> nc.merc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25)) -> nc.grp <- nc.merc %>% group_by(area_cl) -> -> out <- nc.grp %>% summarise(A = sum(area), pop = sum(dens * area), -+ new_dens = sum(dens * area)/sum(area)) -> -> # mean densities depend on grouping: -> nc.merc %>% summarize(mean(dens)) -Simple feature collection with 1 feature and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9 -Projected CRS: NAD83 / North Carolina -# A tibble: 1 × 2 - `mean(dens)` geometry - [person/m^2] -1 0.00000259 (((138426 177699.3, 145548.6 177837.2, 151051.2 180557.6, 152246… -> out %>% summarise(mean(new_dens)) -Simple feature collection with 1 feature and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9 -Projected CRS: NAD83 / North Carolina -# A tibble: 1 × 2 - `mean(new_dens)` geometry - [person/m^2] -1 0.00000259 (((154399.2 148898.9, 142569.6 149407.8, 123829 150481.4, 12… -> -> # total densities don't: -> nc.merc %>% summarise(sum(area * dens)) -Simple feature collection with 1 feature and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9 -Projected CRS: NAD83 / North Carolina -# A tibble: 1 × 2 - `sum(area * dens)` geometry - [person] -1 329962 (((138426 177699.3, 145548.6 177837.2, 151051.2 180557.6, … -> out %>% summarise(sum(A * new_dens)) -Simple feature collection with 1 feature and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9 -Projected CRS: NAD83 / North Carolina -# A tibble: 1 × 2 - `sum(A * new_dens)` geometry - [person] -1 329962 (((154399.2 148898.9, 142569.6 149407.8, 123829 150481.4,… -> -> conn = system.file("gpkg/nc.gpkg", package = "sf") -> -> library(DBI) -> library(RSQLite) -> con = dbConnect(SQLite(), dbname = system.file("gpkg/nc.gpkg", package = "sf")) -> dbReadTable(con, "nc.gpkg") %>% filter(AREA > 0.2) %>% collect %>% st_sf -Simple feature collection with 11 features and 15 fields -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -80.06441 ymin: 33.88199 xmax: -76.49254 ymax: 36.06665 -CRS: NA -First 10 features: - fid AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 -1 37 0.219 2.130 1938 1938 Wake 37183 37183 92 14484 16 -2 47 0.201 1.805 1968 1968 Randolph 37151 37151 76 4456 7 -3 54 0.207 1.851 1989 1989 Johnston 37101 37101 51 3999 6 -4 57 0.203 3.197 2004 2004 Beaufort 37013 37013 7 2692 7 -5 79 0.241 2.214 2083 2083 Sampson 37163 37163 82 3025 4 -6 88 0.204 1.871 2100 2100 Duplin 37061 37061 31 2483 4 -7 94 0.240 2.004 2150 2150 Robeson 37155 37155 78 7889 31 -8 96 0.225 2.107 2162 2162 Bladen 37017 37017 9 1782 8 -9 97 0.214 2.152 2185 2185 Pender 37141 37141 71 1228 4 -10 98 0.240 2.365 2232 2232 Columbus 37047 37047 24 3350 15 - NWBIR74 BIR79 SID79 NWBIR79 geom -1 4397 20857 31 6221 MULTIPOLYGON (((-78.92107 3... -2 384 5711 12 483 MULTIPOLYGON (((-79.76499 3... -3 1165 4780 13 1349 MULTIPOLYGON (((-78.53874 3... -4 1131 2909 4 1163 MULTIPOLYGON (((-77.10377 3... -5 1396 3447 4 1524 MULTIPOLYGON (((-78.11377 3... -6 1061 2777 7 1227 MULTIPOLYGON (((-77.68983 3... -7 5904 9087 26 6899 MULTIPOLYGON (((-78.86451 3... -8 818 2052 5 1023 MULTIPOLYGON (((-78.2615 34... -9 580 1602 3 763 MULTIPOLYGON (((-78.02592 3... -10 1431 4144 17 1832 MULTIPOLYGON (((-78.65572 3... -> -> # nest: -> storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326) -> x <- storms.sf %>% group_by(name, year) %>% nest -> -> nrow(distinct(nc[c(1,1,1,2,2,3:100),])) -[1] 100 -> -> # set.seed(1331) -> nc$gp <- sample(10, 100, replace=TRUE) -> # Get centroid of each group of polygons; https://github.com/r-spatial/sf/issues/969 -> nc_gp_cent <- nc %>% -+ group_by(gp) %>% -+ group_map(st_area) -> -> nc %>% st_filter(nc[1,]) %>% nrow -[1] 4 -> > proc.time() user system elapsed - 2.796 0.072 2.862 + 3.146 0.104 3.289 diff --git a/tests/graticule.R b/tests/graticule.R index 58a072d80..efc23df77 100644 --- a/tests/graticule.R +++ b/tests/graticule.R @@ -1,4 +1,4 @@ -library(maps) +if (require(maps, quietly = TRUE)) { m = map('usa', plot = FALSE, fill = TRUE) suppressPackageStartupMessages(library(sf)) m0 <- st_as_sfc(m) @@ -44,11 +44,11 @@ plot(nc[1], graticule = st_crs(nc), axes = TRUE) g = st_graticule() -library(ggplot2) -if (utils::packageVersion("ggplot2") > "2.2.1") { +if (require(ggplot2, quietly = TRUE) && utils::packageVersion("ggplot2") > "2.2.1") { ggplot() + geom_sf(data = st_set_crs(nc, NA_crs_)) # NA_crs_ for crs } library(maps) #421 (wrld2 = st_as_sf(map('world2', plot=FALSE, fill=TRUE ))) try(plot(wrld2, graticule = TRUE)) +} diff --git a/tests/graticule.Rout.save b/tests/graticule.Rout.save index 7fadbbd32..a111bd083 100644 --- a/tests/graticule.Rout.save +++ b/tests/graticule.Rout.save @@ -1,6 +1,6 @@ -R version 4.0.4 (2021-02-15) -- "Lost Library Book" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -15,26 +15,26 @@ Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. -> library(maps) -> m = map('usa', plot = FALSE, fill = TRUE) -> suppressPackageStartupMessages(library(sf)) -> m0 <- st_as_sfc(m) -> m <- st_as_sf(m) -> -> laea = st_crs("+proj=laea +lat_0=30 +lon_0=-95") # Lambert equal area -> m <- st_transform(st_as_sf(m), laea) -> -> bb = st_bbox(m) -> bbox = st_linestring(rbind(c( bb[1],bb[2]),c( bb[3],bb[2]),c( bb[3],bb[4]),c( bb[1],bb[4]),c( bb[1],bb[2]))) -> -> g = st_graticule(m) -> plot(m, xlim = 1.2 * c(-2450853.4, 2186391.9)) -> plot(g[1], add = TRUE, col = 'grey') -> plot(bbox, add = TRUE) -> points(g$x_start, g$y_start, col = 'red') -> points(g$x_end, g$y_end, col = 'blue') -> -> invisible(lapply(seq_len(nrow(g)), function(i) { +> if (require(maps, quietly = TRUE)) { ++ m = map('usa', plot = FALSE, fill = TRUE) ++ suppressPackageStartupMessages(library(sf)) ++ m0 <- st_as_sfc(m) ++ m <- st_as_sf(m) ++ ++ laea = st_crs("+proj=laea +lat_0=30 +lon_0=-95") # Lambert equal area ++ m <- st_transform(st_as_sf(m), laea) ++ ++ bb = st_bbox(m) ++ bbox = st_linestring(rbind(c( bb[1],bb[2]),c( bb[3],bb[2]),c( bb[3],bb[4]),c( bb[1],bb[4]),c( bb[1],bb[2]))) ++ ++ g = st_graticule(m) ++ plot(m, xlim = 1.2 * c(-2450853.4, 2186391.9)) ++ plot(g[1], add = TRUE, col = 'grey') ++ plot(bbox, add = TRUE) ++ points(g$x_start, g$y_start, col = 'red') ++ points(g$x_end, g$y_end, col = 'blue') ++ ++ invisible(lapply(seq_len(nrow(g)), function(i) { + if (g$type[i] == "N" && g$x_start[i] - min(g$x_start) < 1000) + text(g[i,"x_start"], g[i,"y_start"], labels = parse(text = g[i,"degree_label"]), + srt = g$angle_start[i], pos = 2, cex = .7) @@ -48,45 +48,28 @@ Type 'q()' to quit R. + text(g[i,"x_end"], g[i,"y_end"], labels = parse(text = g[i,"degree_label"]), + srt = g$angle_end[i] - 90, pos = 3, cex = .7) + })) -> -> plot(m, graticule = st_crs(4326)) -> nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) -> # options(warn=2) -> g = st_graticule(nc, datum = st_crs(nc)) -> #g = st_graticule(nc) -> -> plot(nc[1], graticule = st_crs(nc)) -> -> plot(nc[1], graticule = st_crs(nc), axes = TRUE) -> -> g = st_graticule() -> -> library(ggplot2) -> if (utils::packageVersion("ggplot2") > "2.2.1") { ++ ++ plot(m, graticule = st_crs(4326)) ++ nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) ++ # options(warn=2) ++ g = st_graticule(nc, datum = st_crs(nc)) ++ #g = st_graticule(nc) ++ ++ plot(nc[1], graticule = st_crs(nc)) ++ ++ plot(nc[1], graticule = st_crs(nc), axes = TRUE) ++ ++ g = st_graticule() ++ ++ if (require(ggplot2, quietly = TRUE) && utils::packageVersion("ggplot2") > "2.2.1") { + ggplot() + geom_sf(data = st_set_crs(nc, NA_crs_)) # NA_crs_ for crs + } -> -> library(maps) #421 -> (wrld2 = st_as_sf(map('world2', plot=FALSE, fill=TRUE ))) -Simple feature collection with 253 features and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: 0 ymin: -89.99001 xmax: 360 ymax: 83.59961 -Geodetic CRS: WGS 84 -First 10 features: - ID geom -1 Aruba MULTIPOLYGON (((290.1009 12... -2 Afghanistan MULTIPOLYGON (((74.89131 37... -3 Angola MULTIPOLYGON (((23.9665 -10... -4 Anguilla MULTIPOLYGON (((296.9988 18... -5 Albania MULTIPOLYGON (((20.06396 42... -6 Finland MULTIPOLYGON (((20.61133 60... -7 Andorra MULTIPOLYGON (((1.706055 42... -8 United Arab Emirates MULTIPOLYGON (((53.92783 24... -9 Argentina MULTIPOLYGON (((295.4508 -5... -10 Armenia MULTIPOLYGON (((45.55235 40... -> try(plot(wrld2, graticule = TRUE)) ++ ++ library(maps) #421 ++ (wrld2 = st_as_sf(map('world2', plot=FALSE, fill=TRUE ))) ++ try(plot(wrld2, graticule = TRUE)) ++ } > > proc.time() user system elapsed - 3.120 0.258 3.505 + 1.960 0.175 2.135 diff --git a/tests/grid.R b/tests/grid.R index 4147d19bc..889397245 100644 --- a/tests/grid.R +++ b/tests/grid.R @@ -15,12 +15,14 @@ pushViewport(st_viewport(nc)) invisible(lapply(st_geometry(nc), function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) # POINTS: -data(meuse, package = "sp") -meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") -grid.newpage() -pushViewport(st_viewport(meuse_sf)) -invisible(lapply(st_geometry(meuse_sf), +if (require(sp, quietly = TRUE)) { + data(meuse, package = "sp") + meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") + grid.newpage() + pushViewport(st_viewport(meuse_sf)) + invisible(lapply(st_geometry(meuse_sf), function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) +} # MULTIPOINTS mp = st_multipoint(cbind(runif(100), runif(100))) @@ -40,6 +42,7 @@ grid.newpage() pushViewport(st_viewport(ls)) grid.draw(st_as_grob(ls, gp = gpar(fill = 'red'))) +if (require(sp, quietly = TRUE)) { # POINTS, right aspect in Long/Lat: meuse_ll = st_transform(meuse_sf, 4326) grid.newpage() @@ -53,6 +56,7 @@ grid.newpage() pushViewport(st_viewport(meuse_ll)) invisible(lapply(st_geometry(meuse_ll), function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) +} gc = st_geometrycollection(list(st_point(0:1), st_linestring(matrix(1:4,2)))) grb = st_as_grob(gc) diff --git a/tests/grid.Rout.save b/tests/grid.Rout.save index bd0fb69b5..8e737eb88 100644 --- a/tests/grid.Rout.save +++ b/tests/grid.Rout.save @@ -1,6 +1,6 @@ -R version 3.5.2 (2018-12-20) -- "Eggshell Igloo" -Copyright (C) 2018 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -32,12 +32,14 @@ Type 'q()' to quit R. > invisible(lapply(st_geometry(nc), function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) > > # POINTS: -> data(meuse, package = "sp") -> meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") -> grid.newpage() -> pushViewport(st_viewport(meuse_sf)) -> invisible(lapply(st_geometry(meuse_sf), +> if (require(sp, quietly = TRUE)) { ++ data(meuse, package = "sp") ++ meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant") ++ grid.newpage() ++ pushViewport(st_viewport(meuse_sf)) ++ invisible(lapply(st_geometry(meuse_sf), + function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) ++ } > > # MULTIPOINTS > mp = st_multipoint(cbind(runif(100), runif(100))) @@ -57,23 +59,25 @@ Type 'q()' to quit R. > pushViewport(st_viewport(ls)) > grid.draw(st_as_grob(ls, gp = gpar(fill = 'red'))) > -> # POINTS, right aspect in Long/Lat: -> meuse_ll = st_transform(meuse_sf, 4326) -> grid.newpage() -> pushViewport(st_viewport(meuse_ll)) -> invisible(lapply(st_geometry(meuse_ll), +> if (require(sp, quietly = TRUE)) { ++ # POINTS, right aspect in Long/Lat: ++ meuse_ll = st_transform(meuse_sf, 4326) ++ grid.newpage() ++ pushViewport(st_viewport(meuse_ll)) ++ invisible(lapply(st_geometry(meuse_ll), + function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) -> -> # WRONG aspect: -> st_crs(meuse_ll) = NA -> grid.newpage() -> pushViewport(st_viewport(meuse_ll)) -> invisible(lapply(st_geometry(meuse_ll), ++ ++ # WRONG aspect: ++ st_crs(meuse_ll) = NA ++ grid.newpage() ++ pushViewport(st_viewport(meuse_ll)) ++ invisible(lapply(st_geometry(meuse_ll), + function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red'))))) ++ } > > gc = st_geometrycollection(list(st_point(0:1), st_linestring(matrix(1:4,2)))) > grb = st_as_grob(gc) > > proc.time() user system elapsed - 0.620 0.035 0.647 + 1.496 0.059 1.566 diff --git a/tests/maps.R b/tests/maps.R index 5f1b788f7..0bd4d8831 100644 --- a/tests/maps.R +++ b/tests/maps.R @@ -1,11 +1,12 @@ -suppressPackageStartupMessages(library(maps)) suppressPackageStartupMessages(library(sf)) -m = map(xlim = c(4,9), ylim = c(48,55), fill = TRUE, plot = FALSE) -st_as_sf(m) -m = map(xlim = c(4,9), ylim = c(48,55), plot = FALSE) -st_as_sf(m, fill = FALSE) -st_as_sf(map(), fill = FALSE) -st_as_sf(map(fill = TRUE)) -st_as_sf(map(), fill = FALSE, group = FALSE) -st_as_sf(map(fill = TRUE), group = FALSE) +if (require(maps, quietly = TRUE)) { + m = map(xlim = c(4,9), ylim = c(48,55), fill = TRUE, plot = FALSE) + st_as_sf(m) + m = map(xlim = c(4,9), ylim = c(48,55), plot = FALSE) + st_as_sf(m, fill = FALSE) + st_as_sf(map(), fill = FALSE) + st_as_sf(map(fill = TRUE)) + st_as_sf(map(), fill = FALSE, group = FALSE) + st_as_sf(map(fill = TRUE), group = FALSE) +} diff --git a/tests/maps.Rout.save b/tests/maps.Rout.save index 5f6a26714..ac515901d 100644 --- a/tests/maps.Rout.save +++ b/tests/maps.Rout.save @@ -1,6 +1,6 @@ -R version 4.0.4 (2021-02-15) -- "Lost Library Book" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -15,92 +15,18 @@ Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. -> suppressPackageStartupMessages(library(maps)) > suppressPackageStartupMessages(library(sf)) > -> m = map(xlim = c(4,9), ylim = c(48,55), fill = TRUE, plot = FALSE) -> st_as_sf(m) -Simple feature collection with 6 features and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -4.7625 ymin: 42.34048 xmax: 15.0166 ymax: 57.73692 -Geodetic CRS: WGS 84 - ID geom -1 Belgium MULTIPOLYGON (((5.993945 50... -2 Germany MULTIPOLYGON (((8.587891 54... -3 Denmark MULTIPOLYGON (((9.739746 54... -4 France MULTIPOLYGON (((5.789746 49... -5 Luxembourg MULTIPOLYGON (((6.116504 50... -6 Netherlands MULTIPOLYGON (((4.226171 51... -> m = map(xlim = c(4,9), ylim = c(48,55), plot = FALSE) -> st_as_sf(m, fill = FALSE) -Simple feature collection with 6 features and 1 field -Geometry type: MULTILINESTRING -Dimension: XY -Bounding box: xmin: 2.524902 ymin: 47.59272 xmax: 10.92617 ymax: 57.73692 -Geodetic CRS: WGS 84 - ID geom -1 Belgium MULTILINESTRING ((5.993945 ... -2 Germany MULTILINESTRING ((6.116504 ... -3 Denmark MULTILINESTRING ((4.226171 ... -4 France MULTILINESTRING ((8.587891 ... -5 Luxembourg MULTILINESTRING ((7.615625 ... -6 Netherlands MULTILINESTRING ((6.344336 ... -> st_as_sf(map(), fill = FALSE) -Simple feature collection with 253 features and 1 field -Geometry type: MULTILINESTRING -Dimension: XY -Bounding box: xmin: -180 ymin: -84.51308 xmax: 189.4905 ymax: 83.57725 -Geodetic CRS: WGS 84 -First 10 features: - ID geom -1 Aruba MULTILINESTRING ((-69.89912... -2 Afghanistan MULTILINESTRING ((74.89131 ... -3 Angola MULTILINESTRING ((74.5414 3... -4 Anguilla MULTILINESTRING ((61.26201 ... -5 Albania MULTILINESTRING ((66.52226 ... -6 Finland MULTILINESTRING ((67.75899 ... -7 Andorra MULTILINESTRING ((11.74307 ... -8 United Arab Emirates MULTILINESTRING ((13.06816 ... -9 Argentina MULTILINESTRING ((20.56621 ... -10 Armenia MULTILINESTRING ((20.61133 ... -> st_as_sf(map(fill = TRUE)) -Simple feature collection with 253 features and 1 field -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -180 ymin: -85.19218 xmax: 190.2708 ymax: 83.59961 -Geodetic CRS: WGS 84 -First 10 features: - ID geom -1 Aruba MULTIPOLYGON (((-69.89912 1... -2 Afghanistan MULTIPOLYGON (((74.89131 37... -3 Angola MULTIPOLYGON (((23.9665 -10... -4 Anguilla MULTIPOLYGON (((-63.00122 1... -5 Albania MULTIPOLYGON (((20.06396 42... -6 Finland MULTIPOLYGON (((20.61133 60... -7 Andorra MULTIPOLYGON (((1.706055 42... -8 United Arab Emirates MULTIPOLYGON (((53.92783 24... -9 Argentina MULTIPOLYGON (((-64.54916 -... -10 Armenia MULTIPOLYGON (((45.55235 40... -> st_as_sf(map(), fill = FALSE, group = FALSE) -Simple feature collection with 1627 features and 1 field -Geometry type: MULTILINESTRING -Dimension: XY -Bounding box: xmin: -180 ymin: -84.51308 xmax: 189.4905 ymax: 83.57725 -Geodetic CRS: WGS 84 -First 10 features: - ID geom -1 Aruba MULTILINESTRING ((-69.89912... -2 Afghanistan MULTILINESTRING ((74.89131 ... -3 Angola MULTILINESTRING ((74.5414 3... -4 Angola:Cabinda MULTILINESTRING ((60.84336 ... -5 Anguilla MULTILINESTRING ((61.26201 ... -6 Albania MULTILINESTRING ((66.52226 ... -7 Finland:Aland Islands:Foglo MULTILINESTRING ((67.75899 ... -8 Finland:Aland Islands:Eckero MULTILINESTRING ((23.9665 -... -9 Finland:Aland Islands:Fasta Aland MULTILINESTRING ((23.38066 ... -10 Andorra MULTILINESTRING ((11.74307 ... -> st_as_sf(map(fill = TRUE), group = FALSE) +> if (require(maps, quietly = TRUE)) { ++ m = map(xlim = c(4,9), ylim = c(48,55), fill = TRUE, plot = FALSE) ++ st_as_sf(m) ++ m = map(xlim = c(4,9), ylim = c(48,55), plot = FALSE) ++ st_as_sf(m, fill = FALSE) ++ st_as_sf(map(), fill = FALSE) ++ st_as_sf(map(fill = TRUE)) ++ st_as_sf(map(), fill = FALSE, group = FALSE) ++ st_as_sf(map(fill = TRUE), group = FALSE) ++ } Simple feature collection with 1627 features and 1 field Geometry type: MULTIPOLYGON Dimension: XY @@ -121,4 +47,4 @@ First 10 features: > > proc.time() user system elapsed - 3.026 0.138 3.334 + 1.891 0.063 1.947 diff --git a/tests/plot.R b/tests/plot.R index efcd19a5c..d815f1e20 100644 --- a/tests/plot.R +++ b/tests/plot.R @@ -1,6 +1,7 @@ suppressPackageStartupMessages(library(sf)) options(rgdal_show_exportToProj4_warnings = "none") -library(dplyr) + +if (require(dplyr, quietly = TRUE)) { # plot linestrings: l1 = st_linestring(matrix(runif(6)-0.5,,2)) @@ -114,3 +115,4 @@ plot(nc["e"], logz = TRUE) # shared key: plot(nc[c("SID74", "SID79")], key.pos = -1) plot(nc[c("BIR74", "BIR79")], key.pos = 1, logz=TRUE) +} diff --git a/tests/plot.Rout.save b/tests/plot.Rout.save index 5f43ab414..9491bf548 100644 --- a/tests/plot.Rout.save +++ b/tests/plot.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.0 (2021-05-18) -- "Camp Pontanezen" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -17,7 +17,122 @@ Type 'q()' to quit R. > suppressPackageStartupMessages(library(sf)) > options(rgdal_show_exportToProj4_warnings = "none") -> library(dplyr) +> +> if (require(dplyr, quietly = TRUE)) { ++ ++ # plot linestrings: ++ l1 = st_linestring(matrix(runif(6)-0.5,,2)) ++ l2 = st_linestring(matrix(runif(6)-0.5,,2)) ++ l3 = st_linestring(matrix(runif(6)-0.5,,2)) ++ s = st_sf(a=2:4, b=st_sfc(l1,l2,l3)) ++ plot(s, col = s$a, axes = FALSE) ++ plot(s, col = s$a) ++ attr(s$b, "proj4string") = sp::CRS("+proj=longlat +ellps=WGS84 +no_defs")@projargs ++ plot(s, col = s$a, axes = TRUE) ++ plot(s, col = s$a, lty = s$a, lwd = s$a, pch = s$a, type = 'b') ++ l4 = st_linestring(matrix(runif(6),,2)) ++ plot(st_sf(a=1,b=st_sfc(l4)), add = TRUE) ++ # plot multilinestrings: ++ ml1 = st_multilinestring(list(l1, l2)) ++ ml2 = st_multilinestring(list(l3, l4)) ++ ml = st_sf(a = 2:3, b = st_sfc(ml1, ml2)) ++ plot(ml, col = ml$a, lty = ml$a, lwd = ml$a, pch = ml$a, type = 'b') ++ # plot points: ++ p1 = st_point(c(1,2)) ++ p2 = st_point(c(3,3)) ++ p3 = st_point(c(3,0)) ++ p = st_sf(a=2:4, b=st_sfc(p1,p2,p3)) ++ plot(p, col = s$a, axes = TRUE) ++ plot(p, col = s$a) ++ plot(p, col = p$a, pch = p$a, cex = p$a, bg = s$a, lwd = 2, lty = 2, type = 'b') ++ p4 = st_point(c(2,2)) ++ plot(st_sf(a=1, st_sfc(p4)), add = TRUE) ++ # multipoints: ++ mp1 = st_multipoint(matrix(1:4,2)) ++ mp2 = st_multipoint(matrix(5:8,2)) ++ mp = st_sf(a = 2:3, b = st_sfc(mp1, mp2)) ++ plot(mp) ++ plot(mp, col = mp$a, pch = mp$a, cex = mp$a, bg = mp$a, lwd = mp$a, lty = mp$a, type = 'b') ++ # polygon: ++ outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE) ++ hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE) ++ hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE) ++ pl1 = st_polygon(list(outer, hole1, hole2)) ++ pl2 = st_polygon(list(outer+10, hole1+10, hole2+10)) ++ po = st_sf(a = 2:3, st_sfc(pl1,pl2)) ++ plot(po, col = po$a, border = rev(po$a), lwd=3) ++ # multipolygon ++ r10 = matrix(rep(c(0,10),each=5),5) ++ pl1 = list(outer, hole1, hole2) ++ pl2 = list(outer+10, hole1+10, hole2+10) ++ pl3 = list(outer+r10, hole1+r10, hole2+r10) ++ mpo1 = st_multipolygon(list(pl1,pl2)) ++ mpo2 = st_multipolygon(list(pl3)) ++ mpo = st_sf(a=2:3, b=st_sfc(mpo1,mpo2)) ++ plot(mpo, col = mpo$a, border = rev(mpo$a), lwd = 2) ++ # geometrycollection: ++ gc1 = st_geometrycollection(list(mpo1, st_point(c(21,21)), l1 * 2 + 21)) ++ gc2 = st_geometrycollection(list(mpo2, l2 - 2, l3 - 2, st_point(c(-1,-1)))) ++ gc = st_sf(a=2:3, b = st_sfc(gc1,gc2)) ++ plot(gc, cex = gc$a, col = gc$a, border = rev(gc$a) + 2, lwd = 2) ++ ++ plot(gc1) ++ ++ plot(st_sfc(mp1, mpo1)) ++ ++ # color ramp ++ nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) ++ plot(nc) ++ plot(nc, axes = TRUE) ++ plot(nc, col="lightgrey") ++ plot(st_centroid(nc), add = TRUE, col = 1) ++ nc %>% ++ select(geometry) %>% ++ plot() ++ ++ nc$f = cut(nc[[1]], 5) ++ plot(nc["f"], key.pos = 1) ++ plot(nc[1], key.pos = 1) ++ ++ # test background map plotting: ++ load("bgmap.rda") ++ merc = st_crs(3857) ++ WGS84 = st_crs(4326) ++ nc = st_transform(nc, WGS84) ++ ## ggmap: ++ #library(ggmap) ++ #bgMap = get_map(unname(st_bbox(nc)), source = "google", zoom = 8) ++ plot(st_transform(nc[1], merc), bgMap = bgMap) ++ ++ # RgoogleMaps: ++ #library(RgoogleMaps) ++ #center = c(mean(st_bbox(nc)[c(2,4)]), mean(st_bbox(nc)[c(1,3)])) ++ #g = GetMap(center=center, zoom=6) # google ++ par(mar = c(0,0,1,0)) ++ plot(st_transform(nc, merc), bgMap = g) ++ ++ m = st_make_grid() ++ st_crs(m) = NA_crs_ ++ m = st_segmentize(m, 2) ++ st_crs(m) = 4326 ++ plot(m, axes = TRUE) ++ g = st_transform(m, st_crs("+proj=ortho +lat_0=30 +lon_0=45"), check = TRUE) ++ plot(g, axes = TRUE) ++ ++ nc[[1]] = NA ++ nc[[10]] = 1 ++ plot(nc, pal = rainbow, nbreaks = 3) ++ plot(nc, pal = rainbow, breaks = "jenks", nbreaks = 3) ++ plot(nc, pal = rainbow, breaks = (0:10)/3) ++ ++ # logz: ++ nc$e = 10^(nc$SID74) ++ plot(nc["e"], logz = TRUE) ++ ++ # shared key: ++ plot(nc[c("SID74", "SID79")], key.pos = -1) ++ plot(nc[c("BIR74", "BIR79")], key.pos = 1, logz=TRUE) ++ } Attaching package: 'dplyr' @@ -29,143 +144,8 @@ The following objects are masked from 'package:base': intersect, setdiff, setequal, union -> -> # plot linestrings: -> l1 = st_linestring(matrix(runif(6)-0.5,,2)) -> l2 = st_linestring(matrix(runif(6)-0.5,,2)) -> l3 = st_linestring(matrix(runif(6)-0.5,,2)) -> s = st_sf(a=2:4, b=st_sfc(l1,l2,l3)) -> plot(s, col = s$a, axes = FALSE) -> plot(s, col = s$a) -> attr(s$b, "proj4string") = sp::CRS("+proj=longlat +ellps=WGS84 +no_defs")@projargs -> plot(s, col = s$a, axes = TRUE) -> plot(s, col = s$a, lty = s$a, lwd = s$a, pch = s$a, type = 'b') -> l4 = st_linestring(matrix(runif(6),,2)) -> plot(st_sf(a=1,b=st_sfc(l4)), add = TRUE) -> # plot multilinestrings: -> ml1 = st_multilinestring(list(l1, l2)) -> ml2 = st_multilinestring(list(l3, l4)) -> ml = st_sf(a = 2:3, b = st_sfc(ml1, ml2)) -> plot(ml, col = ml$a, lty = ml$a, lwd = ml$a, pch = ml$a, type = 'b') -> # plot points: -> p1 = st_point(c(1,2)) -> p2 = st_point(c(3,3)) -> p3 = st_point(c(3,0)) -> p = st_sf(a=2:4, b=st_sfc(p1,p2,p3)) -> plot(p, col = s$a, axes = TRUE) -> plot(p, col = s$a) -> plot(p, col = p$a, pch = p$a, cex = p$a, bg = s$a, lwd = 2, lty = 2, type = 'b') -> p4 = st_point(c(2,2)) -> plot(st_sf(a=1, st_sfc(p4)), add = TRUE) -> # multipoints: -> mp1 = st_multipoint(matrix(1:4,2)) -> mp2 = st_multipoint(matrix(5:8,2)) -> mp = st_sf(a = 2:3, b = st_sfc(mp1, mp2)) -> plot(mp) -> plot(mp, col = mp$a, pch = mp$a, cex = mp$a, bg = mp$a, lwd = mp$a, lty = mp$a, type = 'b') -> # polygon: -> outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE) -> hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE) -> hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE) -> pl1 = st_polygon(list(outer, hole1, hole2)) -> pl2 = st_polygon(list(outer+10, hole1+10, hole2+10)) -> po = st_sf(a = 2:3, st_sfc(pl1,pl2)) -> plot(po, col = po$a, border = rev(po$a), lwd=3) -> # multipolygon -> r10 = matrix(rep(c(0,10),each=5),5) -> pl1 = list(outer, hole1, hole2) -> pl2 = list(outer+10, hole1+10, hole2+10) -> pl3 = list(outer+r10, hole1+r10, hole2+r10) -> mpo1 = st_multipolygon(list(pl1,pl2)) -> mpo2 = st_multipolygon(list(pl3)) -> mpo = st_sf(a=2:3, b=st_sfc(mpo1,mpo2)) -> plot(mpo, col = mpo$a, border = rev(mpo$a), lwd = 2) -> # geometrycollection: -> gc1 = st_geometrycollection(list(mpo1, st_point(c(21,21)), l1 * 2 + 21)) -> gc2 = st_geometrycollection(list(mpo2, l2 - 2, l3 - 2, st_point(c(-1,-1)))) -> gc = st_sf(a=2:3, b = st_sfc(gc1,gc2)) -> plot(gc, cex = gc$a, col = gc$a, border = rev(gc$a) + 2, lwd = 2) -> -> plot(gc1) -> -> plot(st_sfc(mp1, mpo1)) -> -> # color ramp -> nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) -> plot(nc) -Warning message: -plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all -> plot(nc, axes = TRUE) -Warning message: -plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all -> plot(nc, col="lightgrey") -Warning message: -plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all -> plot(st_centroid(nc), add = TRUE, col = 1) -Warning messages: -1: In st_centroid.sf(nc) : - st_centroid assumes attributes are constant over geometries of x -2: In plot.sf(st_centroid(nc), add = TRUE, col = 1) : - ignoring all but the first attribute -> nc %>% -+ select(geometry) %>% -+ plot() -> -> nc$f = cut(nc[[1]], 5) -> plot(nc["f"], key.pos = 1) -> plot(nc[1], key.pos = 1) -> -> # test background map plotting: -> load("bgmap.rda") -> merc = st_crs(3857) -> WGS84 = st_crs(4326) -> nc = st_transform(nc, WGS84) -> ## ggmap: -> #library(ggmap) -> #bgMap = get_map(unname(st_bbox(nc)), source = "google", zoom = 8) -> plot(st_transform(nc[1], merc), bgMap = bgMap) -> -> # RgoogleMaps: -> #library(RgoogleMaps) -> #center = c(mean(st_bbox(nc)[c(2,4)]), mean(st_bbox(nc)[c(1,3)])) -> #g = GetMap(center=center, zoom=6) # google -> par(mar = c(0,0,1,0)) -> plot(st_transform(nc, merc), bgMap = g) -Warning message: -plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all -> -> m = st_make_grid() -> st_crs(m) = NA_crs_ -> m = st_segmentize(m, 2) -> st_crs(m) = 4326 -> plot(m, axes = TRUE) -> g = st_transform(m, st_crs("+proj=ortho +lat_0=30 +lon_0=45"), check = TRUE) -> plot(g, axes = TRUE) -> -> nc[[1]] = NA -> nc[[10]] = 1 -> plot(nc, pal = rainbow, nbreaks = 3) -Warning messages: -1: plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all -2: In min(x) : no non-missing arguments to min; returning Inf -3: In max(x) : no non-missing arguments to max; returning -Inf -> plot(nc, pal = rainbow, breaks = "jenks", nbreaks = 3) -Warning messages: -1: plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all -2: In min(x) : no non-missing arguments to min; returning Inf -3: In max(x) : no non-missing arguments to max; returning -Inf -> plot(nc, pal = rainbow, breaks = (0:10)/3) -Warning message: -plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all -> -> # logz: -> nc$e = 10^(nc$SID74) -> plot(nc["e"], logz = TRUE) -> -> # shared key: -> plot(nc[c("SID74", "SID79")], key.pos = -1) -> plot(nc[c("BIR74", "BIR79")], key.pos = 1, logz=TRUE) +There were 13 warnings (use warnings() to see them) > > proc.time() user system elapsed - 5.220 0.169 5.379 + 5.863 0.119 6.022 diff --git a/tests/roundtrip.R b/tests/roundtrip.R index 0e0ef8aad..b504160cf 100644 --- a/tests/roundtrip.R +++ b/tests/roundtrip.R @@ -12,20 +12,22 @@ pol3 = list(outer + 24) mp = list(pol1,pol2,pol3) mp1 = st_multipolygon(mp) sf = st_sf(a=1, st_sfc(mp1)) -a = as(sf, "Spatial") -class(a) -b = st_as_sf(a) -a2 = as(a, "SpatialPolygonsDataFrame") -all.equal(a, a2) # round-trip +if (require(sp, quietly = TRUE)) { + a = as(sf, "Spatial") + print(class(a)) + b = st_as_sf(a) + a2 = as(a, "SpatialPolygonsDataFrame") + print(all.equal(a, a2)) # round-trip -b1 = as(a, "sf") -all.equal(b, b1) -b = st_as_sfc(a) -b1 = as(a, "sfc") -all.equal(b, b1) + b1 = as(a, "sf") + print(all.equal(b, b1)) + b = st_as_sfc(a) + b1 = as(a, "sfc") + print(all.equal(b, b1)) +} # SpatialMultiPoints -library(sp) +if (require(sp, quietly = TRUE)) { suppressWarnings(RNGversion("3.5.3")) set.seed(1331) # example(SpatialMultiPoints, ask = FALSE, echo = FALSE) # loads mpdf @@ -57,3 +59,4 @@ all.equal(nc, st_as_sf(as(nc, "Spatial"))) st_crs(nc) == st_crs(st_as_sf(as(nc, "Spatial"))) detach("package:sp") +} diff --git a/tests/roundtrip.Rout.save b/tests/roundtrip.Rout.save index fc447722c..20183e053 100644 --- a/tests/roundtrip.Rout.save +++ b/tests/roundtrip.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -29,62 +29,61 @@ Type 'q()' to quit R. > mp = list(pol1,pol2,pol3) > mp1 = st_multipolygon(mp) > sf = st_sf(a=1, st_sfc(mp1)) -> a = as(sf, "Spatial") -> class(a) +> if (require(sp, quietly = TRUE)) { ++ a = as(sf, "Spatial") ++ print(class(a)) ++ b = st_as_sf(a) ++ a2 = as(a, "SpatialPolygonsDataFrame") ++ print(all.equal(a, a2)) # round-trip ++ ++ b1 = as(a, "sf") ++ print(all.equal(b, b1)) ++ b = st_as_sfc(a) ++ b1 = as(a, "sfc") ++ print(all.equal(b, b1)) ++ } [1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp" -> b = st_as_sf(a) -> a2 = as(a, "SpatialPolygonsDataFrame") -> all.equal(a, a2) # round-trip [1] TRUE -> -> b1 = as(a, "sf") -> all.equal(b, b1) [1] TRUE -> b = st_as_sfc(a) -> b1 = as(a, "sfc") -> all.equal(b, b1) [1] TRUE > > # SpatialMultiPoints -> library(sp) -> suppressWarnings(RNGversion("3.5.3")) -> set.seed(1331) -> # example(SpatialMultiPoints, ask = FALSE, echo = FALSE) # loads mpdf -> cl1 = cbind(rnorm(3, 10), rnorm(3, 10)) -> cl2 = cbind(rnorm(5, 10), rnorm(5, 0)) -> cl3 = cbind(rnorm(7, 0), rnorm(7, 10)) -> mpdf = SpatialMultiPointsDataFrame(list(cl1, cl2, cl3), data.frame(a = 1:3)) -> m = st_as_sf(mpdf) -> all.equal(as(m, "Spatial"), mpdf) # TRUE -[1] TRUE -> -> demo(meuse, ask = FALSE, echo = FALSE) -> #meuse = spTransform(meuse, CRS("+proj=longlat +ellps=WGS84 +no_defs")) -> pol.grd = as(meuse.grid, "SpatialPolygonsDataFrame") -> #meuse.grd = spTransform(meuse.grid, CRS("+proj=longlat +ellps=WGS84 +no_defs")) -> #pol.grd = spTransform(pol.grd, CRS("+proj=longlat +ellps=WGS84 +no_defs")) -> #meuse.area = spTransform(meuse.area, CRS("+proj=longlat +ellps=WGS84 +no_defs")) -> #meuse.riv = spTransform(meuse.riv, CRS("+proj=longlat +ellps=WGS84 +no_defs")) -> #summary(st_as_sf(meuse)) -> #summary(st_as_sf(meuse.grd)) -> #x <- st_as_sf(meuse.grid) # don't print: CRS variations. -> #summary(st_as_sf(meuse.area)) -> #summary(st_as_sf(meuse.riv)) -> #summary(st_as_sf(as(meuse.riv, "SpatialLines"))) -> #summary(st_as_sf(pol.grd)) -> #summary(st_as_sf(as(pol.grd, "SpatialLinesDataFrame"))) -> -> nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), "nc.gpkg", quiet = TRUE) -> all.equal(nc, st_as_sf(as(nc, "Spatial"))) -[1] "Names: 1 string mismatch" -[2] "Attributes: < Component \"sf_column\": 1 string mismatch >" -> st_crs(nc) == st_crs(st_as_sf(as(nc, "Spatial"))) -[1] TRUE -> -> detach("package:sp") +> if (require(sp, quietly = TRUE)) { ++ suppressWarnings(RNGversion("3.5.3")) ++ set.seed(1331) ++ # example(SpatialMultiPoints, ask = FALSE, echo = FALSE) # loads mpdf ++ cl1 = cbind(rnorm(3, 10), rnorm(3, 10)) ++ cl2 = cbind(rnorm(5, 10), rnorm(5, 0)) ++ cl3 = cbind(rnorm(7, 0), rnorm(7, 10)) ++ mpdf = SpatialMultiPointsDataFrame(list(cl1, cl2, cl3), data.frame(a = 1:3)) ++ m = st_as_sf(mpdf) ++ all.equal(as(m, "Spatial"), mpdf) # TRUE ++ ++ demo(meuse, ask = FALSE, echo = FALSE) ++ #meuse = spTransform(meuse, CRS("+proj=longlat +ellps=WGS84 +no_defs")) ++ pol.grd = as(meuse.grid, "SpatialPolygonsDataFrame") ++ #meuse.grd = spTransform(meuse.grid, CRS("+proj=longlat +ellps=WGS84 +no_defs")) ++ #pol.grd = spTransform(pol.grd, CRS("+proj=longlat +ellps=WGS84 +no_defs")) ++ #meuse.area = spTransform(meuse.area, CRS("+proj=longlat +ellps=WGS84 +no_defs")) ++ #meuse.riv = spTransform(meuse.riv, CRS("+proj=longlat +ellps=WGS84 +no_defs")) ++ #summary(st_as_sf(meuse)) ++ #summary(st_as_sf(meuse.grd)) ++ #x <- st_as_sf(meuse.grid) # don't print: CRS variations. ++ #summary(st_as_sf(meuse.area)) ++ #summary(st_as_sf(meuse.riv)) ++ #summary(st_as_sf(as(meuse.riv, "SpatialLines"))) ++ #summary(st_as_sf(pol.grd)) ++ #summary(st_as_sf(as(pol.grd, "SpatialLinesDataFrame"))) ++ ++ nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), "nc.gpkg", quiet = TRUE) ++ all.equal(nc, st_as_sf(as(nc, "Spatial"))) ++ st_crs(nc) == st_crs(st_as_sf(as(nc, "Spatial"))) ++ ++ detach("package:sp") ++ } > > proc.time() user system elapsed - 1.850 0.044 1.894 + 1.735 0.065 1.789 diff --git a/tests/s2.Rout.save b/tests/s2.Rout.save index d5c83a09d..1c6ba7416 100644 --- a/tests/s2.Rout.save +++ b/tests/s2.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -16,7 +16,7 @@ Type 'demo()' for some demos, 'help()' for on-line help, or Type 'q()' to quit R. > library(sf) -Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE +Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE > d = data.frame(z = 1:100, x = runif(100), y = runif(100)) > n0 = st_as_sf(d, coords = c("x", "y"), crs = 4326) > n1 = st_transform(n0, 3857) @@ -66,4 +66,4 @@ Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE > > proc.time() user system elapsed - 1.239 0.044 1.275 + 1.829 0.063 1.885 diff --git a/tests/sfc.R b/tests/sfc.R index ae9d58008..b2b907e79 100644 --- a/tests/sfc.R +++ b/tests/sfc.R @@ -1,5 +1,4 @@ suppressPackageStartupMessages(library(sf)) -library(testthat) p = st_point(c(1/3,1/6)) st_sfc(p, precision = 1000) @@ -34,7 +33,9 @@ attr(d1, "sf_col") = "geom" st_geometry(d1) = d$geom d$geometry = d$geom # second geometry list-column -expect_warning(st_geometry(d) <- d$geom) +if (require(testthat, quietly = TRUE)) { + expect_warning(st_geometry(d) <- d$geom) +} d x = st_sfc(list(st_point(0:1), st_point(0:1)), crs = 4326) @@ -62,7 +63,6 @@ st_cast(x, "POINT") sf = st_sf(a = 3:2, geom = x) st_cast(sf, "POINT") -suppressPackageStartupMessages( library(dplyr) ) x %>% st_cast("POINT") @@ -126,7 +126,6 @@ format(st_bbox(nc)) st_agr("constant") st_agr() x <- st_sf(a = 1:2, b = 3:4, geom = x, agr = c("constant", "aggregate")) -suppressPackageStartupMessages(library(dplyr)) y <- x %>% st_set_agr("constant") y @@ -155,6 +154,7 @@ b = data.frame(x = c("a", "b", "c"), b = c(2,5,6)) merge(a, b) merge(a, b, all = TRUE) +if (require(dplyr, quietly = TRUE)) { # joins: inner_join(a, b) left_join(a, b) @@ -163,6 +163,7 @@ full_join(a, b) semi_join(a, b) anti_join(a, b) left_join(a, data.frame(b, geometry = 1), by = "b") +} # st_joins: a = st_sf(a = 1:3, @@ -221,8 +222,10 @@ lengths(p_sample_exact) #plot(p_sample[[1]], add = TRUE) #plot(p_sample_exact[[1]], add = TRUE) -#class(st_bind_cols(nc, as.data.frame(nc)[1:3])) -class(dplyr::bind_cols(nc, as.data.frame(nc)[1:3])) +if (require(dplyr, quietly = TRUE)) { + #class(st_bind_cols(nc, as.data.frame(nc)[1:3])) + print(class(dplyr::bind_cols(nc, as.data.frame(nc)[1:3]))) +} class(rbind(nc, nc)) class(cbind(nc, nc)) @@ -238,13 +241,13 @@ plot(st_jitter(st_geometry(nc), factor = .01), add = TRUE, col = '#ff8888') st_jitter(st_sfc(st_point(0:1)), amount = .1) # st_bbox: -library(sp) +if (require(sp, quietly = TRUE) && require(raster, quietly = TRUE)) { demo(meuse, ask = FALSE, echo = FALSE) suppressWarnings(st_bbox(meuse)) crs = suppressWarnings(st_crs(meuse)) -library(raster) suppressWarnings(st_bbox(raster(meuse.grid))) st_bbox(extent(raster())) +} # st_to_s2 if (FALSE) { # stops working with GDAL 2.3.0 / PROJ 5.0.1: diff --git a/tests/sfc.Rout.save b/tests/sfc.Rout.save index ca4f557aa..7cb16e5bf 100644 --- a/tests/sfc.Rout.save +++ b/tests/sfc.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -16,7 +16,6 @@ Type 'demo()' for some demos, 'help()' for on-line help, or Type 'q()' to quit R. > suppressPackageStartupMessages(library(sf)) -> library(testthat) > > p = st_point(c(1/3,1/6)) > st_sfc(p, precision = 1000) @@ -154,7 +153,9 @@ CRS: NA > st_geometry(d1) = d$geom > > d$geometry = d$geom # second geometry list-column -> expect_warning(st_geometry(d) <- d$geom) +> if (require(testthat, quietly = TRUE)) { ++ expect_warning(st_geometry(d) <- d$geom) ++ } > d Simple feature collection with 2 features and 1 field Active geometry column: geom @@ -232,7 +233,6 @@ CRS: NA Warning message: In st_cast.MULTIPOINT(X[[i]], ...) : point from first coordinate only > -> suppressPackageStartupMessages( library(dplyr) ) > > x %>% st_cast("POINT") Geometry set for 2 features @@ -347,7 +347,7 @@ use `st_zm(...)` to coerce to XY dimensions > > sf_extSoftVersion()[1:3] GEOS GDAL proj.4 -"3.10.1" "3.4.0" "8.2.0" +"3.10.2" "3.4.3" "8.2.0" > > # Ops.sfc: > ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1)))) @@ -448,7 +448,6 @@ Levels: constant aggregate identity [1] Levels: constant aggregate identity > x <- st_sf(a = 1:2, b = 3:4, geom = x, agr = c("constant", "aggregate")) -> suppressPackageStartupMessages(library(dplyr)) > y <- x %>% st_set_agr("constant") > y Simple feature collection with 2 features and 2 fields @@ -550,71 +549,37 @@ CRS: NA 3 6 2 c POINT (1 1) 4 7 3 POINT (2 2) > -> # joins: -> inner_join(a, b) +> if (require(dplyr, quietly = TRUE)) { ++ # joins: ++ inner_join(a, b) ++ left_join(a, b) ++ right_join(a, b) ++ full_join(a, b) ++ semi_join(a, b) ++ anti_join(a, b) ++ left_join(a, data.frame(b, geometry = 1), by = "b") ++ } + +Attaching package: 'dplyr' + +The following object is masked from 'package:testthat': + + matches + +The following objects are masked from 'package:stats': + + filter, lag + +The following objects are masked from 'package:base': + + intersect, setdiff, setequal, union + Joining, by = "b" -Simple feature collection with 2 features and 3 fields -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1 -CRS: NA - a b x geometry -1 1 5 b POINT (0 0) -2 2 6 c POINT (1 1) -> left_join(a, b) Joining, by = "b" -Simple feature collection with 3 features and 3 fields -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 2 ymax: 2 -CRS: NA - a b x geometry -1 1 5 b POINT (0 0) -2 2 6 c POINT (1 1) -3 3 7 POINT (2 2) -> right_join(a, b) Joining, by = "b" -Simple feature collection with 3 features and 3 fields (with 1 geometry empty) -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1 -CRS: NA - a b x geometry -1 1 5 b POINT (0 0) -2 2 6 c POINT (1 1) -3 NA 2 a POINT EMPTY -> full_join(a, b) Joining, by = "b" -Simple feature collection with 4 features and 3 fields (with 1 geometry empty) -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 2 ymax: 2 -CRS: NA - a b x geometry -1 1 5 b POINT (0 0) -2 2 6 c POINT (1 1) -3 3 7 POINT (2 2) -4 NA 2 a POINT EMPTY -> semi_join(a, b) Joining, by = "b" -Simple feature collection with 2 features and 2 fields -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1 -CRS: NA - a b geometry -1 1 5 POINT (0 0) -2 2 6 POINT (1 1) -> anti_join(a, b) Joining, by = "b" -Simple feature collection with 1 feature and 2 fields -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 2 ymin: 2 xmax: 2 ymax: 2 -CRS: NA - a b geometry -1 3 7 POINT (2 2) -> left_join(a, data.frame(b, geometry = 1), by = "b") Simple feature collection with 3 features and 4 fields Geometry type: POINT Dimension: XY @@ -809,15 +774,17 @@ MULTIPOINT EMPTY > #plot(p_sample[[1]], add = TRUE) > #plot(p_sample_exact[[1]], add = TRUE) > -> #class(st_bind_cols(nc, as.data.frame(nc)[1:3])) -> class(dplyr::bind_cols(nc, as.data.frame(nc)[1:3])) +> if (require(dplyr, quietly = TRUE)) { ++ #class(st_bind_cols(nc, as.data.frame(nc)[1:3])) ++ print(class(dplyr::bind_cols(nc, as.data.frame(nc)[1:3]))) ++ } New names: -* AREA -> AREA...1 -* PERIMETER -> PERIMETER...2 -* CNTY_ -> CNTY_...3 -* AREA -> AREA...16 -* PERIMETER -> PERIMETER...17 -* ... +• `AREA` -> `AREA...1` +• `PERIMETER` -> `PERIMETER...2` +• `CNTY_` -> `CNTY_...3` +• `AREA` -> `AREA...16` +• `PERIMETER` -> `PERIMETER...17` +• `CNTY_` -> `CNTY_...18` [1] "sf" "data.frame" > class(rbind(nc, nc)) [1] "sf" "data.frame" @@ -852,13 +819,13 @@ CRS: NA POINT (-0.0500922 0.992953) > > # st_bbox: -> library(sp) -> demo(meuse, ask = FALSE, echo = FALSE) -> suppressWarnings(st_bbox(meuse)) - xmin ymin xmax ymax -178605 329714 181390 333611 -> crs = suppressWarnings(st_crs(meuse)) -> library(raster) +> if (require(sp, quietly = TRUE) && require(raster, quietly = TRUE)) { ++ demo(meuse, ask = FALSE, echo = FALSE) ++ suppressWarnings(st_bbox(meuse)) ++ crs = suppressWarnings(st_crs(meuse)) ++ suppressWarnings(st_bbox(raster(meuse.grid))) ++ st_bbox(extent(raster())) ++ } Attaching package: 'raster' @@ -866,12 +833,12 @@ The following object is masked from 'package:dplyr': select -> suppressWarnings(st_bbox(raster(meuse.grid))) - xmin ymin xmax ymax -178440 329600 181560 333760 -> st_bbox(extent(raster())) xmin ymin xmax ymax -180 -90 180 90 +Warning messages: +1: no function found corresponding to methods exports from 'raster' for: 'area' +2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) : + Discarded datum Amersfoort in Proj4 definition > > # st_to_s2 > if (FALSE) { # stops working with GDAL 2.3.0 / PROJ 5.0.1: @@ -1123,4 +1090,4 @@ CRS: NA > > proc.time() user system elapsed - 8.386 0.119 8.498 + 10.773 0.216 10.985 diff --git a/tests/spatstat.R b/tests/spatstat.R index 273530c10..8b17aa4e4 100644 --- a/tests/spatstat.R +++ b/tests/spatstat.R @@ -1,5 +1,5 @@ -suppressPackageStartupMessages(library(spatstat.random)) suppressPackageStartupMessages(library(sf)) +if (require(spatstat.random, quietly = TRUE)) { data(chicago) st_as_sf(chicago) @@ -107,3 +107,4 @@ as.psp(sf, marks = 5:1) (x = st_as_sf(as.psp(sf))) (y = st_as_sfc(as.psp(sf))) all.equal(st_geometry(x), y) +} diff --git a/tests/spatstat.Rout.save b/tests/spatstat.Rout.save index c82e07252..2859786c4 100644 --- a/tests/spatstat.Rout.save +++ b/tests/spatstat.Rout.save @@ -1,6 +1,6 @@ -R version 4.1.2 (2021-11-01) -- "Bird Hippie" -Copyright (C) 2021 The R Foundation for Statistical Computing +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" +Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -15,77 +15,118 @@ Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. -> suppressPackageStartupMessages(library(spatstat.random)) > suppressPackageStartupMessages(library(sf)) -> -> data(chicago) -> st_as_sf(chicago) -Simple feature collection with 620 features and 4 fields -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 0.3893523 ymin: 153.1034 xmax: 1281.986 ymax: 1276.56 -CRS: NA -First 10 features: - label seg tp marks geom -1 window NA NA POLYGON ((0.3893523 153.103... -2 segment NA NA LINESTRING (0.3894739 1253.... -3 segment NA NA LINESTRING (109.683 1251.77... -4 segment NA NA LINESTRING (109.683 1251.77... -5 segment NA NA LINESTRING (198.1486 1276.5... -6 segment NA NA LINESTRING (197.9988 1251.1... -7 segment NA NA LINESTRING (290.4787 1276.5... -8 segment NA NA LINESTRING (288.9907 1250.5... -9 segment NA NA LINESTRING (380.1326 1276.5... -10 segment NA NA LINESTRING (379.9827 1249.8... -> # ppp: -> g = gorillas -> st_as_sf(g) -Simple feature collection with 648 features and 4 fields -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 580457.9 ymin: 674172.8 xmax: 585934 ymax: 678739.2 -CRS: NA -First 10 features: - group season date label geom -NA window POLYGON ((584712 674237.1, ... -1 major dry 2006-01-06 point POINT (582518.4 676886.2) -2 major dry 2006-01-10 point POINT (581823 677422.7) -3 major dry 2006-01-15 point POINT (582131 676937.9) -4 major dry 2006-01-24 point POINT (582111.9 677420) -5 minor dry 2006-01-27 point POINT (582585.1 677509.7) -6 major dry 2006-01-28 point POINT (582302.3 677521.6) -7 major dry 2006-02-01 point POINT (583167.2 676730.5) -8 major dry 2006-02-03 point POINT (583584.5 677207.1) -9 major dry 2006-02-13 point POINT (583117.8 676850.3) -> marks(g) = NULL -> st_as_sf(g) -Simple feature collection with 648 features and 1 field -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 580457.9 ymin: 674172.8 xmax: 585934 ymax: 678739.2 -CRS: NA -First 10 features: - label geom -1 window POLYGON ((584712 674237.1, ... -2 point POINT (582518.4 676886.2) -3 point POINT (581823 677422.7) -4 point POINT (582131 676937.9) -5 point POINT (582111.9 677420) -6 point POINT (582585.1 677509.7) -7 point POINT (582302.3 677521.6) -8 point POINT (583167.2 676730.5) -9 point POINT (583584.5 677207.1) -10 point POINT (583117.8 676850.3) -> -> # multipolygon: https://github.com/r-spatial/sf/issues/1161 -> window = read_sf(system.file("shape/nc.shp", package = "sf")) %>% +> if (require(spatstat.random, quietly = TRUE)) { ++ ++ data(chicago) ++ st_as_sf(chicago) ++ # ppp: ++ g = gorillas ++ st_as_sf(g) ++ marks(g) = NULL ++ st_as_sf(g) ++ ++ # multipolygon: https://github.com/r-spatial/sf/issues/1161 ++ window = read_sf(system.file("shape/nc.shp", package = "sf")) %>% + st_transform(32119) -> -> win = spatstat.geom::as.owin(window) -> -> set.seed(1331) -> pp2a = runifpoint(n = 50, win = win) -> print(st_as_sf(pp2a)) ++ ++ win = spatstat.geom::as.owin(window) ++ ++ set.seed(1331) ++ pp2a = runifpoint(n = 50, win = win) ++ print(st_as_sf(pp2a)) ++ ++ # st_sample going the spatstat way ++ x <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(10, 0), c(10, 10), c(0, 0))))) ++ try(pts <- st_sample(x, type = "thomas")) ++ try(pts <- st_sample(x, kappa = 1, mu = 10, type = "Thomas")) ++ # points expected ++ set.seed(1331) ++ pts <- st_sample(x, kappa = 1, mu = 10, scale = 0.1, type = "Thomas") ++ #plot(x) ++ #plot(pts, add = TRUE) ++ pts ++ ++ # see https://github.com/r-spatial/sf/issues/1233 ++ # png("/tmp/spa%03d.png") ++ ++ p1 = st_point(0:1) ++ p2 = st_point(1:2) ++ p3 = st_point(c(-1,2)) ++ p = st_sfc(p1, p2, p3) ++ as.ppp(p) ++ try(as.ppp(st_set_crs(p, 4326))) ++ ++ sf = st_sf(geom = p) ++ try(as.ppp(sf)) ++ sf = st_sf(a = 1:3, geom = p) ++ as.ppp(sf) ++ sf = st_sf(a = 1:3, b=3:1, geom = p) ++ as.ppp(sf) # warns ++ ++ w = st_as_sfc(st_bbox(st_sfc(p1, p2))) ++ sf = st_sf(a = 1:3, geom = p) ++ (p0 = rbind(st_sf(a = 0, geom = w), sf)) ++ try(as.ppp(p0)) # errors: one point outside window ++ ++ w = st_as_sfc(st_bbox(p)) ++ sf = st_sf(a = 1:3, geom = p) ++ (p0 = rbind(st_sf(a = 0, geom = w), sf)) ++ as.ppp(p0) ++ ++ # as.owin.sf, as.owin.sfc_* ++ nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), check_ring_dir = TRUE, quiet = TRUE) ++ try(as.owin(nc)) # should be projected ++ nc = st_transform(nc, 32119) ++ plot(as.owin(nc), col = 'grey') ++ plot(as.owin(st_geometry(nc)), col = 'grey') ++ ++ sq = rbind(c(-1,-1), c(1, -1), c(1,1), c(-1,1), c(-1,-1)) ++ pol = st_polygon(list(0.5 * sq, sq[5:1,] * 0.45)) # w hole ++ plot(as.owin(pol), col = 'grey') ++ plot(as.owin(st_sfc(pol)), col = 'grey') ++ mpol = st_multipolygon(list( ++ list(sq, sq[5:1,] * 0.9), ++ list(sq * 2, sq[5:1,] * 1.8))) ++ plot(as.owin(mpol), col = 'grey') ++ plot(as.owin(st_sfc(mpol)), col = 'grey') ++ plot(as.owin(st_sfc(pol, mpol)), col = 'grey') ++ plot(as.owin(st_sf(a=1:2, st_sfc(pol, mpol))), col = 'grey') ++ (o = as.owin(st_sf(a=1:2, st_sfc(pol, mpol)))) ++ st_as_sfc(o) ++ ++ plot(st_as_sfc(o), col = 'blue', main = 'st_as_sfc(o)') ++ plot(st_as_sf(o), col = 'blue', main = 'st_as_sf(o)') ++ ++ data(japanesepines) ++ st_as_sf(japanesepines) # warns about multiplier ++ jp = rescale(japanesepines) ++ st_as_sf(jp) # No warning ++ ++ data(nztrees) ++ qNZ <- quadratcount(nztrees, nx=4, ny=3) ++ ts = as.tess(qNZ) ++ plot(st_as_sfc(ts)) ++ ++ ls = st_linestring(rbind(c(0,0), c(1,1), c(2,0))) ++ plot(as.psp(ls)) ++ mls = st_multilinestring(list(rbind(c(0,0), c(1,1), c(2,0)), rbind(c(3,3), c(4,2)))) ++ plot(as.psp(mls)) ++ ++ plot(as.psp(st_sfc(ls))) ++ plot(as.psp(st_sfc(mls))) ++ plot(as.psp(st_sfc(ls, mls))) ++ ++ sf = st_sf(st_cast(st_sfc(ls, mls), "MULTILINESTRING"), marks = 1:2, foo = 2:1) ++ as.psp(sf) # picks marks itself ++ as.psp(sf, marks = 5:1) ++ ++ (x = st_as_sf(as.psp(sf))) ++ (y = st_as_sfc(as.psp(sf))) ++ all.equal(st_geometry(x), y) ++ } +spatstat.geom 2.4-0 +spatstat.random 2.2-0 Simple feature collection with 51 features and 1 field Geometry type: GEOMETRY Dimension: XY @@ -103,238 +144,26 @@ First 10 features: 8 point POINT (843281.2 287246) 9 point POINT (648479.4 235471) 10 point POINT (852595.8 267252.6) -> -> # st_sample going the spatstat way -> x <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(10, 0), c(10, 10), c(0, 0))))) -> try(pts <- st_sample(x, type = "thomas")) Error in st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon) : rthomas is not an exported function from spatstat.random. -> try(pts <- st_sample(x, kappa = 1, mu = 10, type = "Thomas")) Error in st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon) : The spatstat function rThomas did not return a valid result. Consult the help file. Error message from spatstat: Error : 'scale' should be a single number -> # points expected -> set.seed(1331) -> pts <- st_sample(x, kappa = 1, mu = 10, scale = 0.1, type = "Thomas") -> #plot(x) -> #plot(pts, add = TRUE) -> pts -Simple feature collection with 597 features and 1 field -Geometry type: POINT -Dimension: XY -Bounding box: xmin: 1.213108 ymin: 0.02200954 xmax: 9.994943 ymax: 8.82534 -CRS: NA -First 10 features: - label geom -2 point POINT (9.076646 8.661168) -3 point POINT (9.347089 8.800523) -4 point POINT (9.207624 8.82534) -5 point POINT (9.403886 8.666932) -6 point POINT (9.437082 8.63911) -7 point POINT (9.254756 8.573871) -8 point POINT (9.29172 8.678031) -9 point POINT (9.735812 1.621866) -10 point POINT (9.853825 1.616409) -11 point POINT (9.665138 1.593111) -> -> # see https://github.com/r-spatial/sf/issues/1233 -> # png("/tmp/spa%03d.png") -> -> p1 = st_point(0:1) -> p2 = st_point(1:2) -> p3 = st_point(c(-1,2)) -> p = st_sfc(p1, p2, p3) -> as.ppp(p) -Planar point pattern: 3 points -window: rectangle = [-1, 1] x [1, 2] units -> try(as.ppp(st_set_crs(p, 4326))) Error : Only projected coordinates may be converted to spatstat class objects -> -> sf = st_sf(geom = p) -> try(as.ppp(sf)) -Planar point pattern: 3 points -window: rectangle = [-1, 1] x [1, 2] units -> sf = st_sf(a = 1:3, geom = p) -> as.ppp(sf) -Marked planar point pattern: 3 points -marks are numeric, of storage type 'integer' -window: rectangle = [-1, 1] x [1, 2] units -> sf = st_sf(a = 1:3, b=3:1, geom = p) -> as.ppp(sf) # warns -Marked planar point pattern: 3 points -marks are numeric, of storage type 'integer' -window: rectangle = [-1, 1] x [1, 2] units -Warning message: -In as.ppp.sf(sf) : only first attribute column is used for marks -> -> w = st_as_sfc(st_bbox(st_sfc(p1, p2))) -> sf = st_sf(a = 1:3, geom = p) -> (p0 = rbind(st_sf(a = 0, geom = w), sf)) -Simple feature collection with 4 features and 1 field -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: -1 ymin: 1 xmax: 1 ymax: 2 -CRS: NA - a geom -1 0 POLYGON ((0 1, 1 1, 1 2, 0 ... -2 1 POINT (0 1) -3 2 POINT (1 2) -4 3 POINT (-1 2) -> try(as.ppp(p0)) # errors: one point outside window Error in `marks<-.ppp`(`*tmp*`, value = value) : number of rows of data frame != number of points -In addition: Warning message: -1 point was rejected as lying outside the specified window -> -> w = st_as_sfc(st_bbox(p)) -> sf = st_sf(a = 1:3, geom = p) -> (p0 = rbind(st_sf(a = 0, geom = w), sf)) -Simple feature collection with 4 features and 1 field -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: -1 ymin: 1 xmax: 1 ymax: 2 -CRS: NA - a geom -1 0 POLYGON ((-1 1, 1 1, 1 2, -... -2 1 POINT (0 1) -3 2 POINT (1 2) -4 3 POINT (-1 2) -> as.ppp(p0) -Marked planar point pattern: 3 points -marks are numeric, of storage type 'double' -window: polygonal boundary -enclosing rectangle: [-1, 1] x [1, 2] units -> -> # as.owin.sf, as.owin.sfc_* -> nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), check_ring_dir = TRUE, quiet = TRUE) -> try(as.owin(nc)) # should be projected +In addition: Warning messages: +1: In as.ppp.sf(sf) : only first attribute column is used for marks +2: 1 point was rejected as lying outside the specified window Error in as.owin.sfc_MULTIPOLYGON(st_cast(W, "MULTIPOLYGON"), ...) : Only projected coordinates may be converted to spatstat class objects -> nc = st_transform(nc, 32119) -> plot(as.owin(nc), col = 'grey') -> plot(as.owin(st_geometry(nc)), col = 'grey') -> -> sq = rbind(c(-1,-1), c(1, -1), c(1,1), c(-1,1), c(-1,-1)) -> pol = st_polygon(list(0.5 * sq, sq[5:1,] * 0.45)) # w hole -> plot(as.owin(pol), col = 'grey') -> plot(as.owin(st_sfc(pol)), col = 'grey') -> mpol = st_multipolygon(list( -+ list(sq, sq[5:1,] * 0.9), -+ list(sq * 2, sq[5:1,] * 1.8))) -> plot(as.owin(mpol), col = 'grey') -> plot(as.owin(st_sfc(mpol)), col = 'grey') -> plot(as.owin(st_sfc(pol, mpol)), col = 'grey') -> plot(as.owin(st_sf(a=1:2, st_sfc(pol, mpol))), col = 'grey') -> (o = as.owin(st_sf(a=1:2, st_sfc(pol, mpol)))) -window: polygonal boundary -enclosing rectangle: [-2, 2] x [-2, 2] units -> st_as_sfc(o) -Geometry set for 1 feature -Geometry type: MULTIPOLYGON -Dimension: XY -Bounding box: xmin: -2 ymin: -2 xmax: 2 ymax: 2 -CRS: NA -MULTIPOLYGON (((2 2, -2 2, -2 -2, 2 -2, 2 2), (... -> -> plot(st_as_sfc(o), col = 'blue', main = 'st_as_sfc(o)') -> plot(st_as_sf(o), col = 'blue', main = 'st_as_sf(o)') -> -> data(japanesepines) -> st_as_sf(japanesepines) # warns about multiplier -Simple feature collection with 66 features and 1 field -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1 -CRS: NA -First 10 features: - label geom -1 window POLYGON ((0 0, 1 0, 1 1, 0 ... -2 point POINT (0.09 0.09) -3 point POINT (0.29 0.02) -4 point POINT (0.38 0.03) -5 point POINT (0.39 0.18) -6 point POINT (0.48 0.03) -7 point POINT (0.59 0.02) -8 point POINT (0.65 0.16) -9 point POINT (0.67 0.13) -10 point POINT (0.73 0.13) +[1] TRUE Warning message: In st_as_sfc.owin(spatstat.geom::as.owin(x)) : The spatstat object has an measurement unit multiplier != 1. Consider rescaling before converting. -> jp = rescale(japanesepines) -> st_as_sf(jp) # No warning -Simple feature collection with 66 features and 1 field -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 5.7 ymax: 5.7 -CRS: NA -First 10 features: - label geom -1 window POLYGON ((0 0, 5.7 0, 5.7 5... -2 point POINT (0.513 0.513) -3 point POINT (1.653 0.114) -4 point POINT (2.166 0.171) -5 point POINT (2.223 1.026) -6 point POINT (2.736 0.171) -7 point POINT (3.363 0.114) -8 point POINT (3.705 0.912) -9 point POINT (3.819 0.741) -10 point POINT (4.161 0.741) -> -> data(nztrees) -> qNZ <- quadratcount(nztrees, nx=4, ny=3) -> ts = as.tess(qNZ) -> plot(st_as_sfc(ts)) -> -> ls = st_linestring(rbind(c(0,0), c(1,1), c(2,0))) -> plot(as.psp(ls)) -> mls = st_multilinestring(list(rbind(c(0,0), c(1,1), c(2,0)), rbind(c(3,3), c(4,2)))) -> plot(as.psp(mls)) -> -> plot(as.psp(st_sfc(ls))) -> plot(as.psp(st_sfc(mls))) -> plot(as.psp(st_sfc(ls, mls))) -> -> sf = st_sf(st_cast(st_sfc(ls, mls), "MULTILINESTRING"), marks = 1:2, foo = 2:1) -> as.psp(sf) # picks marks itself -marked planar line segment pattern: 5 line segments -Mark variables: marks, foo -window: rectangle = [0, 4] x [0, 3] units -> as.psp(sf, marks = 5:1) -marked planar line segment pattern: 5 line segments -marks are numeric, of type 'integer' -window: rectangle = [0, 4] x [0, 3] units -> -> (x = st_as_sf(as.psp(sf))) -Simple feature collection with 6 features and 3 fields -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 4 ymax: 3 -CRS: NA - marks foo label geom -NA NA NA window POLYGON ((0 0, 4 0, 4 3, 0 ... -1 1 2 segment LINESTRING (0 0, 1 1) -1.1 1 2 segment LINESTRING (1 1, 2 0) -2 2 1 segment LINESTRING (0 0, 1 1) -2.1 2 1 segment LINESTRING (1 1, 2 0) -2.2 2 1 segment LINESTRING (3 3, 4 2) -> (y = st_as_sfc(as.psp(sf))) -Geometry set for 6 features -Geometry type: GEOMETRY -Dimension: XY -Bounding box: xmin: 0 ymin: 0 xmax: 4 ymax: 3 -CRS: NA -First 5 geometries: -POLYGON ((0 0, 4 0, 4 3, 0 3, 0 0)) -LINESTRING (0 0, 1 1) -LINESTRING (1 1, 2 0) -LINESTRING (0 0, 1 1) -LINESTRING (1 1, 2 0) -> all.equal(st_geometry(x), y) -[1] TRUE > > proc.time() user system elapsed - 2.019 0.071 2.083 + 2.801 0.108 2.903 diff --git a/tests/stars.R b/tests/stars.R index 2ec9e7237..0895b0635 100644 --- a/tests/stars.R +++ b/tests/stars.R @@ -8,7 +8,7 @@ try(gdal_metadata(tif, "wrongDomain")) gdal_metadata(tif, c("IMAGE_STRUCTURE")) try(length(gdal_metadata(tif, c("DERIVED_SUBDATASETS")))) # fails on Fedora 26 -if (require(stars)) { +if (require(stars, quietly = TRUE)) { tif = system.file("tif/geomatrix.tif", package = "sf") r = read_stars(tif) d = (st_dimensions(r)) diff --git a/tests/stars.Rout.save b/tests/stars.Rout.save index 0a71f1a2d..8a89b98d3 100644 --- a/tests/stars.Rout.save +++ b/tests/stars.Rout.save @@ -1,5 +1,5 @@ -R version 4.1.3 (2022-03-10) -- "One Push-Up" +R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) @@ -35,7 +35,7 @@ attr(,"class") > try(length(gdal_metadata(tif, c("DERIVED_SUBDATASETS")))) # fails on Fedora 26 [1] 2 > -> if (require(stars)) { +> if (require(stars, quietly = TRUE)) { + tif = system.file("tif/geomatrix.tif", package = "sf") + r = read_stars(tif) + d = (st_dimensions(r)) @@ -91,8 +91,6 @@ attr(,"class") + } + } + } -Loading required package: stars -Loading required package: abind [1] TRUE [1] TRUE Geometry set for 20 features @@ -246,6 +244,9 @@ First 10 features: +Warning message: +In read_stars(lc, RAT = "Land Cover Class") : + categorical data values starting at 0 are shifted with one to start at 1 > > r = gdal_read(tif) > gt = c(0,1,0,0,0,1) @@ -263,4 +264,4 @@ First 10 features: > > proc.time() user system elapsed - 1.954 0.081 2.036 + 2.229 0.122 2.345 diff --git a/tests/testthat.R b/tests/testthat.R index f29a5c9f6..246bdc8bc 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,4 +1,4 @@ -library(testthat) -suppressPackageStartupMessages(library(sf)) - -test_check("sf") +if (require(testthat, quietly = TRUE)) { + suppressPackageStartupMessages(library(sf)) + test_check("sf") +} diff --git a/tests/testthat/test_crs.R b/tests/testthat/test_crs.R index abab96b4c..39e8ac332 100644 --- a/tests/testthat/test_crs.R +++ b/tests/testthat/test_crs.R @@ -89,6 +89,7 @@ test_that("old-style crs are repaired", { }) test_that("sp-style CRS objects are accepted", { + skip_if_not_installed("sp") library(sp) x = CRS("+proj=longlat") x_crs = st_crs("+proj=longlat") @@ -105,6 +106,7 @@ test_that("print.crs works", { }) test_that("crs.Raster works", { + skip_if_not_installed("raster") library(raster) r = raster() x = st_crs(r) diff --git a/tests/testthat/test_sf.R b/tests/testthat/test_sf.R index 04b925da4..897794741 100644 --- a/tests/testthat/test_sf.R +++ b/tests/testthat/test_sf.R @@ -20,6 +20,7 @@ test_that("we can subset sf objects", { }) test_that("we can create points sf from data.frame", { + skip_if_not_installed("sp") data(meuse, package = "sp") # load data.frame from sp meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) meuse_sf[1:5,] @@ -63,6 +64,7 @@ test_that("rbind/cbind work", { }) test_that("st_as_sf bulk points work", { + skip_if_not_installed("sp") data(meuse, package = "sp") # load data.frame from sp x <- meuse meuse_sf = st_as_sf(x, coords = c("x", "y"), crs = 28992) @@ -82,6 +84,7 @@ test_that("st_as_sf bulk points work", { }) test_that("transform work", { + skip_if_not_installed("sp") data(meuse, package = "sp") x = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) x2 = transform(x, elev2 = elev^2, lead_zinc = lead/zinc) diff --git a/tests/testthat/test_sp.R b/tests/testthat/test_sp.R index 22412fe62..e77bbcd43 100644 --- a/tests/testthat/test_sp.R +++ b/tests/testthat/test_sp.R @@ -1,6 +1,7 @@ context("sf: sp conversion tests") test_that("we can convert points & lines to and from sp objects", { + skip_if_not_installed("sp") pt1 = st_point(1:2) pt2 = st_point(3:4) s1 = st_sf(a = c("x", "y"), geom = st_sfc(pt1, pt2)) @@ -24,6 +25,7 @@ test_that("we can convert points & lines to and from sp objects", { }) test_that("as() can convert GEOMETRY to Spatial (#131)", { + skip_if_not_installed("sp") single <- list(rbind(c(0,0), c(1,0), c(1, 1), c(0,1), c(0,0))) %>% st_polygon() multi <- list(single + 2, single + 4) %>% st_multipolygon() @@ -52,6 +54,7 @@ test_that("as() can convert GEOMETRY to Spatial (#131)", { }) test_that("as_Spatial can convert sf (#519)", { + skip_if_not_installed("sp") h <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) u <- as(h, "Spatial") @@ -65,18 +68,21 @@ test_that("as_Spatial can convert sf (#519)", { }) test_that("Can convert `XY` objects to sp", { + skip_if_not_installed("sp") expect_is(as(st_point(1:2), "Spatial"), "SpatialPoints") expect_error(as(st_point(1:3), "Spatial")) expect_error(as(st_point(1:4), "Spatial")) }) test_that("Can't convert `M` dimension to sp", { + skip_if_not_installed("sp") skip_if_not(sf_extSoftVersion()[["GDAL"]] >= "2.1.0") x <- read_sf(system.file("shape/storms_xyzm_feature.shp", package = "sf"), quiet = TRUE) expect_error(as_Spatial(x), "not supported by sp") }) test_that("conversion to sp breaks on empty geometries", { + skip_if_not_installed("sp") mysfc <- st_sfc(list( st_polygon(list(matrix(c(1,3,2,1,0,0,1,0), 4, 2))), st_polygon() # empty polygon diff --git a/tests/testthat/test_tidy.R b/tests/testthat/test_tidy.R index 49da9b623..4f9c7a832 100644 --- a/tests/testthat/test_tidy.R +++ b/tests/testthat/test_tidy.R @@ -1,14 +1,16 @@ context("sf: dplyr syntax") -suppressMessages(library(dplyr)) +suppressMessages(require(dplyr, quietly = TRUE)) nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) test_that("select works", { + skip_if_not_installed("dplyr") expect_true(nc %>% select_("AREA", attr(., "sf_column")) %>% inherits("sf")) expect_true(nc %>% select(AREA) %>% inherits("sf")) }) test_that("filter to sfc works", { + skip_if_not_installed("dplyr") tbl = tibble(a = c("A", "B", "C"), geometry = st_sfc(st_point(c(1, 1)), st_point(), @@ -20,14 +22,16 @@ test_that("filter to sfc works", { d[1:2, ]) }) -suppressMessages(library(tidyr)) +suppressMessages(require(tidyr, quietly = TRUE)) test_that("separate and unite work", { + skip_if_not_installed("dplyr") expect_true(nc %>% separate(CNTY_ID, c("a", "b"), sep = 2) %>% inherits("sf")) expect_true(nc %>% separate(CNTY_ID, c("a", "b"), sep = 2) %>% unite(CNTY_ID_NEW, c("a", "b"), sep = "") %>% inherits("sf")) }) test_that("separate_rows work", { + skip_if_not_installed("dplyr") d <- st_as_sf(data.frame( x = seq_len(3), y = c("a", "d,e,f", "g,h"), @@ -50,6 +54,7 @@ test_that("separate_rows work", { }) test_that("group/ungroup works", { + skip_if_not_installed("dplyr") tbl = tibble(a = c(1,1,2,2), g = st_sfc(st_point(0:1), st_point(1:2), st_point(2:3), st_point(3:4))) d = st_sf(tbl) e <- d %>% group_by(a) %>% ungroup @@ -57,6 +62,7 @@ test_that("group/ungroup works", { }) test_that("sample_n etc work", { + skip_if_not_installed("dplyr") tbl = tibble(a = c(1,1,2,2), g = st_sfc(st_point(0:1), st_point(1:2), st_point(2:3), st_point(3:4))) d = st_sf(tbl) @@ -72,6 +78,7 @@ test_that("sample_n etc work", { }) test_that("nest() works", { + skip_if_not_installed("dplyr") tbl = tibble(a = c(1,1,2,2), g = st_sfc(st_point(0:1), st_point(1:2), st_point(2:3), st_point(3:4))) d = st_sf(tbl) out = d %>% group_by(a) %>% nest() @@ -99,6 +106,7 @@ test_that("st_intersection of tbl returns tbl", { }) test_that("unnest works", { + skip_if_not_installed("dplyr") skip_if_not_installed("tidyr") skip_if_not(utils::packageVersion("tidyr") > "0.7.2") nc = read_sf(system.file("shape/nc.shp", package = "sf")) %>% @@ -207,6 +215,7 @@ test_that("bind_cols() returns type of first input", { }) test_that("can rename geometry column with `select()`", { + skip_if_not_installed("dplyr") sf = st_sf( x = 1, geo = st_sfc(st_point(1:2)), @@ -221,6 +230,7 @@ test_that("can rename geometry column with `select()`", { }) test_that("can rename geometry column with `rename()` (#1431)", { + skip_if_not_installed("dplyr") geo_pt = st_sfc(st_point()) geo_ln = st_sfc(st_linestring()) sf = st_sf(x = 1, geo2 = geo_pt, geo1 = geo_ln, sf_column_name = "geo1") @@ -245,6 +255,7 @@ test_that("can rename geometry column with `rename()` (#1431)", { }) test_that("`select()` and `transmute()` observe back-stickiness of geometry column (#1425)", { + skip_if_not_installed("dplyr") sf = read_sf(system.file("shape/nc.shp", package = "sf")) exp = sf[, c("NAME", "FIPS")] expect_identical(dplyr::select(sf, NAME, FIPS), exp) diff --git a/tests/testthat/test_wkb.R b/tests/testthat/test_wkb.R index 8e35e72d7..d94d07fa7 100644 --- a/tests/testthat/test_wkb.R +++ b/tests/testthat/test_wkb.R @@ -51,6 +51,7 @@ test_that("Reading of truncated buffers results in a proper error", { }) test_that("st_as_sfc() honors crs argument", { + skip_if_not_installed("blob") raw = st_as_binary(st_point(c(26e5, 12e5))) list = list(raw) diff --git a/tests/testthat/test_write.R b/tests/testthat/test_write.R index fc0bf8a5e..240250125 100644 --- a/tests/testthat/test_write.R +++ b/tests/testthat/test_write.R @@ -1,11 +1,14 @@ context("sf: write") +if (require(sp, quietly = TRUE)) { data(meuse, package = "sp") meuse <- st_as_sf(meuse, coords = c("x", "y"), crs = 28992) drvs <- st_drivers()$name[sapply(st_drivers()$name, function(x) is_driver_can(x, operation = "write"))] %>% as.character() +} test_that("sf can write to all writable formats", { + skip_if_not_installed("sp") # write to all formats available tf <- tempfile() excluded_drivers = c("gps", # requires options @@ -20,6 +23,7 @@ test_that("sf can write to all writable formats", { }) test_that("sf can write to netcdf", { + skip_if_not_installed("sp") skip_on_os("windows") tf <- tempfile() if ("netCDF" %in% drvs) { @@ -28,6 +32,7 @@ test_that("sf can write to netcdf", { }) test_that("sf can write units (#264)", { + skip_if_not_installed("sp") tf <- tempfile(fileext = ".gpkg") meuse[["length"]] <- meuse[["cadmium"]] units(meuse$length) <- units::as_units("km") @@ -90,6 +95,7 @@ test_that("delete and update work (#304)", { }) test_that("layer is deleted when fails to create features (#549)", { + skip_if_not_installed("sp") skip_on_os("mac") shp <- tempfile(fileext = ".shp") x <- st_sf(a = 1:2, geom = st_sfc(st_point(0:1), st_multipoint(matrix(1:4,2,2))))