diff --git a/DESCRIPTION b/DESCRIPTION index f4b63c3a9f..be1d0d3b10 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,7 +54,6 @@ Suggests: knitr, mapproj, maps, - maptools, multcomp, munsell, nlme, diff --git a/NEWS.md b/NEWS.md index 1d95e88137..2bbe33847d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -139,6 +139,11 @@ * `stat_contour()` and `stat_contour_filled()` now warn about and remove duplicated coordinates (@teunbrand, #5215). * Improve performance of layers without positional scales (@zeehio, #4990) +* `fortify()` for sp objects (e.g., `SpatialPolygonsDataFrame`) is now deprecated + and will be removed soon in support of [the upcoming retirement of rproj, rgeos, + and maptools](https://r-spatial.org/r/2023/05/15/evolution4.html). In advance + of the whole removal, `fortify(, region = ...)` + no longer works as of this version (@yutannihilation, #5244). # ggplot2 3.4.3 This hotfix release addresses a version comparison change in r-devel. There are diff --git a/R/fortify-spatial.R b/R/fortify-spatial.R index 3e549cc23f..6fe7392a37 100644 --- a/R/fortify-spatial.R +++ b/R/fortify-spatial.R @@ -9,32 +9,31 @@ #' @param ... not used by this method #' @keywords internal #' @name fortify.sp -#' @examples -#' if (require("maptools")) { -#' sids <- system.file("shapes/sids.shp", package="maptools") -#' nc1 <- readShapePoly(sids, -#' proj4string = CRS("+proj=longlat +datum=NAD27")) -#' nc1_df <- fortify(nc1) -#' } NULL #' @rdname fortify.sp #' @export #' @method fortify SpatialPolygonsDataFrame fortify.SpatialPolygonsDataFrame <- function(model, data, region = NULL, ...) { + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + attr <- as.data.frame(model) # If not specified, split into regions based on polygons if (is.null(region)) { - coords <- lapply(model@polygons,fortify) + # Suppress duplicated warnings + withr::with_options(list(lifecycle_verbosity = "quiet"), { + coords <- lapply(model@polygons,fortify) + }) coords <- vec_rbind0(!!!coords) cli::cli_inform("Regions defined for each Polygons") } else { - cp <- sp::polygons(model) - - # Union together all polygons that make up a region - unioned <- maptools::unionSpatialPolygons(cp, attr[, region]) - coords <- fortify(unioned) - coords$order <- 1:nrow(coords) + lifecycle::deprecate_stop("3.4.4", + I("`fortify(, region = ...)` is defunct'"), + details = "Please migrate to sf." + ) } coords } @@ -43,7 +42,15 @@ fortify.SpatialPolygonsDataFrame <- function(model, data, region = NULL, ...) { #' @export #' @method fortify SpatialPolygons fortify.SpatialPolygons <- function(model, data, ...) { - polys <- lapply(model@polygons, fortify) + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + + # Suppress duplicated warnings + withr::with_options(list(lifecycle_verbosity = "quiet"), { + polys <- lapply(model@polygons, fortify) + }) vec_rbind0(!!!polys) } @@ -51,6 +58,11 @@ fortify.SpatialPolygons <- function(model, data, ...) { #' @export #' @method fortify Polygons fortify.Polygons <- function(model, data, ...) { + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + subpolys <- model@Polygons pieces <- lapply(seq_along(subpolys), function(i) { df <- fortify(subpolys[[model@plotOrder[i]]]) @@ -70,6 +82,11 @@ fortify.Polygons <- function(model, data, ...) { #' @export #' @method fortify Polygon fortify.Polygon <- function(model, data, ...) { + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + df <- as.data.frame(model@coords) names(df) <- c("long", "lat") df$order <- 1:nrow(df) @@ -81,6 +98,11 @@ fortify.Polygon <- function(model, data, ...) { #' @export #' @method fortify SpatialLinesDataFrame fortify.SpatialLinesDataFrame <- function(model, data, ...) { + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + lines <- lapply(model@lines, fortify) vec_rbind0(!!!lines) } @@ -89,6 +111,11 @@ fortify.SpatialLinesDataFrame <- function(model, data, ...) { #' @export #' @method fortify Lines fortify.Lines <- function(model, data, ...) { + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + lines <- model@Lines pieces <- lapply(seq_along(lines), function(i) { df <- fortify(lines[[i]]) @@ -108,6 +135,11 @@ fortify.Lines <- function(model, data, ...) { #' @export #' @method fortify Line fortify.Line <- function(model, data, ...) { + deprecate_warn0("3.4.4", + I("`fortify()`"), + details = "Please migrate to sf." + ) + df <- as.data.frame(model@coords) names(df) <- c("long", "lat") df$order <- 1:nrow(df) diff --git a/man/fortify.sp.Rd b/man/fortify.sp.Rd index 31f6b6fe65..af1a587f5d 100644 --- a/man/fortify.sp.Rd +++ b/man/fortify.sp.Rd @@ -38,12 +38,4 @@ To figure out the correct variable name for region, inspect \code{as.data.frame(model)}. } -\examples{ -if (require("maptools")) { - sids <- system.file("shapes/sids.shp", package="maptools") - nc1 <- readShapePoly(sids, - proj4string = CRS("+proj=longlat +datum=NAD27")) - nc1_df <- fortify(nc1) -} -} \keyword{internal} diff --git a/tests/testthat/test-fortify.R b/tests/testthat/test-fortify.R index 8741fac2d0..43b7adb74c 100644 --- a/tests/testthat/test-fortify.R +++ b/tests/testthat/test-fortify.R @@ -29,10 +29,14 @@ test_that("spatial polygons have correct ordering", { polys2 <- rev(polys) polys2_sp <- sp::SpatialPolygons(polys2) fake_sp2 <- sp::SpatialPolygonsDataFrame(polys2_sp, fake_data) - expected <- fortify(fake_sp2) + lifecycle::expect_deprecated( + expected <- fortify(fake_sp2) + ) expected <- expected[order(expected$id, expected$order), ] - actual <- fortify(fake_sp) + lifecycle::expect_deprecated( + actual <- fortify(fake_sp) + ) # the levels are different, so these columns need to be converted to character to compare expected$group <- as.character(expected$group) @@ -40,6 +44,11 @@ test_that("spatial polygons have correct ordering", { # Use expect_equal(ignore_attr = TRUE) to ignore rownames expect_equal(actual, expected, ignore_attr = TRUE) + + lifecycle::expect_deprecated( + # fortify() with region is defunct due to maptools' retirement + lifecycle::expect_defunct(fortify(fake_sp, region = "foo")) + ) }) test_that("fortify.default proves a helpful error with class uneval", {