Skip to content

Commit de5a754

Browse files
Cherry-pick from #5327
1 parent 6bc0a7f commit de5a754

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ Suggests:
5555
lattice,
5656
mapproj,
5757
maps,
58-
maptools,
5958
multcomp,
6059
munsell,
6160
nlme,

R/fortify-spatial.R

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,31 @@
99
#' @param ... not used by this method
1010
#' @keywords internal
1111
#' @name fortify.sp
12-
#' @examples
13-
#' if (require("maptools")) {
14-
#' sids <- system.file("shapes/sids.shp", package="maptools")
15-
#' nc1 <- readShapePoly(sids,
16-
#' proj4string = CRS("+proj=longlat +datum=NAD27"))
17-
#' nc1_df <- fortify(nc1)
18-
#' }
1912
NULL
2013

2114
#' @rdname fortify.sp
2215
#' @export
2316
#' @method fortify SpatialPolygonsDataFrame
2417
fortify.SpatialPolygonsDataFrame <- function(model, data, region = NULL, ...) {
18+
deprecate_warn0("3.4.4",
19+
I("`fortify(<SpatialPolygonsDataFrame>)`"),
20+
details = "Please migrate to sf."
21+
)
22+
2523
attr <- as.data.frame(model)
2624
# If not specified, split into regions based on polygons
2725
if (is.null(region)) {
28-
coords <- lapply(model@polygons,fortify)
26+
# Suppress duplicated warnings
27+
withr::with_options(list(lifecycle_verbosity = "quiet"), {
28+
coords <- lapply(model@polygons,fortify)
29+
})
2930
coords <- vec_rbind0(!!!coords)
3031
cli::cli_inform("Regions defined for each Polygons")
3132
} else {
32-
cp <- sp::polygons(model)
33-
34-
# Union together all polygons that make up a region
35-
unioned <- maptools::unionSpatialPolygons(cp, attr[, region])
36-
coords <- fortify(unioned)
37-
coords$order <- 1:nrow(coords)
33+
lifecycle::deprecate_stop("3.4.4",
34+
I("`fortify(<SpatialPolygonsDataFrame>, region = ...)` is defunct'"),
35+
details = "Please migrate to sf."
36+
)
3837
}
3938
coords
4039
}
@@ -43,14 +42,27 @@ fortify.SpatialPolygonsDataFrame <- function(model, data, region = NULL, ...) {
4342
#' @export
4443
#' @method fortify SpatialPolygons
4544
fortify.SpatialPolygons <- function(model, data, ...) {
46-
polys <- lapply(model@polygons, fortify)
45+
deprecate_warn0("3.4.4",
46+
I("`fortify(<SpatialPolygons>)`"),
47+
details = "Please migrate to sf."
48+
)
49+
50+
# Suppress duplicated warnings
51+
withr::with_options(list(lifecycle_verbosity = "quiet"), {
52+
polys <- lapply(model@polygons, fortify)
53+
})
4754
vec_rbind0(!!!polys)
4855
}
4956

5057
#' @rdname fortify.sp
5158
#' @export
5259
#' @method fortify Polygons
5360
fortify.Polygons <- function(model, data, ...) {
61+
deprecate_warn0("3.4.4",
62+
I("`fortify(<Polygons>)`"),
63+
details = "Please migrate to sf."
64+
)
65+
5466
subpolys <- model@Polygons
5567
pieces <- lapply(seq_along(subpolys), function(i) {
5668
df <- fortify(subpolys[[model@plotOrder[i]]])
@@ -70,6 +82,11 @@ fortify.Polygons <- function(model, data, ...) {
7082
#' @export
7183
#' @method fortify Polygon
7284
fortify.Polygon <- function(model, data, ...) {
85+
deprecate_warn0("3.4.4",
86+
I("`fortify(<Polygon>)`"),
87+
details = "Please migrate to sf."
88+
)
89+
7390
df <- as.data.frame(model@coords)
7491
names(df) <- c("long", "lat")
7592
df$order <- 1:nrow(df)
@@ -81,6 +98,11 @@ fortify.Polygon <- function(model, data, ...) {
8198
#' @export
8299
#' @method fortify SpatialLinesDataFrame
83100
fortify.SpatialLinesDataFrame <- function(model, data, ...) {
101+
deprecate_warn0("3.4.4",
102+
I("`fortify(<SpatialLinesDataFrame>)`"),
103+
details = "Please migrate to sf."
104+
)
105+
84106
lines <- lapply(model@lines, fortify)
85107
vec_rbind0(!!!lines)
86108
}
@@ -89,6 +111,11 @@ fortify.SpatialLinesDataFrame <- function(model, data, ...) {
89111
#' @export
90112
#' @method fortify Lines
91113
fortify.Lines <- function(model, data, ...) {
114+
deprecate_warn0("3.4.4",
115+
I("`fortify(<Lines>)`"),
116+
details = "Please migrate to sf."
117+
)
118+
92119
lines <- model@Lines
93120
pieces <- lapply(seq_along(lines), function(i) {
94121
df <- fortify(lines[[i]])
@@ -108,6 +135,11 @@ fortify.Lines <- function(model, data, ...) {
108135
#' @export
109136
#' @method fortify Line
110137
fortify.Line <- function(model, data, ...) {
138+
deprecate_warn0("3.4.4",
139+
I("`fortify(<Line>)`"),
140+
details = "Please migrate to sf."
141+
)
142+
111143
df <- as.data.frame(model@coords)
112144
names(df) <- c("long", "lat")
113145
df$order <- 1:nrow(df)

man/fortify.sp.Rd

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-fortify.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,26 @@ test_that("spatial polygons have correct ordering", {
2929
polys2 <- rev(polys)
3030
polys2_sp <- sp::SpatialPolygons(polys2)
3131
fake_sp2 <- sp::SpatialPolygonsDataFrame(polys2_sp, fake_data)
32-
expected <- fortify(fake_sp2)
32+
lifecycle::expect_deprecated(
33+
expected <- fortify(fake_sp2)
34+
)
3335
expected <- expected[order(expected$id, expected$order), ]
3436

35-
actual <- fortify(fake_sp)
37+
lifecycle::expect_deprecated(
38+
actual <- fortify(fake_sp)
39+
)
3640

3741
# the levels are different, so these columns need to be converted to character to compare
3842
expected$group <- as.character(expected$group)
3943
actual$group <- as.character(actual$group)
4044

4145
# Use expect_equal(ignore_attr = TRUE) to ignore rownames
4246
expect_equal(actual, expected, ignore_attr = TRUE)
47+
48+
lifecycle::expect_deprecated(
49+
# fortify() with region is defunct due to maptools' retirement
50+
lifecycle::expect_defunct(fortify(fake_sp, region = "foo"))
51+
)
4352
})
4453

4554
test_that("fortify.default proves a helpful error with class uneval", {

0 commit comments

Comments
 (0)