Skip to content

Commit

Permalink
Replace maptools and rgdal (#71)
Browse files Browse the repository at this point in the history
* Replaced `maptools::elide` with `sp::elide`
* Replaced `rgdal` with `sf` as `sp` now depends on it
* This should also resolve CRAN warnings/errors related to missing
functions (e.g. `maptools::elide`)

See https://r-spatial.org/r/2023/04/10/evolution3.html for more
information

fixes #57 
fixes #70
closes #68
  • Loading branch information
pdil committed Jun 9, 2023
2 parents 6c0c4f6 + c9e1373 commit 132175b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
^CRAN-RELEASE$
^codecov\.yml$
^LICENSE\.md$
^\.lintr$
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ Suggests:
ggplot2,
ggrepel,
knitr,
maptools,
proto,
rgdal,
rmarkdown,
scales,
sp,
sf,
stringr,
testthat
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
13 changes: 8 additions & 5 deletions R/plot-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ plot_usmap <- function(regions = c("states", "state", "counties", "county"),
ggplot2::ggplot(data = map_df) + polygon_layer + label_layer + ggplot2::coord_equal() + theme
}

#' This creates a nice map theme for use in plot_usmap.
#' It is borrowed from the ggthemes package located at this repository:
#' https://github.com/jrnold/ggthemes
#' Convenient theme map
#'
#' @description
#' This creates a nice map theme for use in [plot_usmap].
#' It is borrowed from the `ggthemes` package located at this repository:
#' https://github.com/jrnold/ggthemes.
#'
#' This function was manually rewritten here to avoid the need for
#' another package import.
#'
#' All theme functions (i.e. theme_bw, theme, element_blank, %+replace%)
#' come from ggplot2.
#' All theme functions (i.e. `theme_bw`, `theme`, `element_blank`, `%+replace%`)
#' come from `ggplot2`.
#'
#' @keywords internal
theme_map <- function(base_size = 9, base_family = "") {
Expand Down
47 changes: 33 additions & 14 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
usmap_transform <- function(data,
input_names = c("lon", "lat"),
output_names = c("x", "y")) {
# check for maptools
if (!requireNamespace("maptools", quietly = TRUE)) {
stop("`maptools` must be installed to use `usmap_transform`.
Use: install.packages(\"maptools\") and try again.")

# check for sf
set_sp_evolution_status()
if (!requireNamespace("sf", quietly = TRUE)) {
stop("`sf` must be installed to use `usmap_transform`.
Use: install.packages(\"sf\") and try again.")
}

# check for sp
Expand All @@ -58,12 +60,6 @@ usmap_transform <- function(data,
Use: install.packages(\"sp\") and try again.")
}

# check for rgdal
if (!requireNamespace("rgdal", quietly = TRUE)) {
stop("`rgdal` must be installed to use `usmap_transform`.
Use: install.packages(\"rgdal\") and try again.")
}

UseMethod("usmap_transform", data)
}

Expand Down Expand Up @@ -131,13 +127,13 @@ usmap_transform.data.frame <- function(data,
]

if (length(alaska) > 0) {
alaska <- maptools::elide(
alaska <- sp::elide(
alaska,
rotate = -50,
scale = max(apply(ak_bbox, 1, diff)) / 2.3,
bb = ak_bbox
)
alaska <- maptools::elide(alaska, shift = c(-1298669, -3018809))
alaska <- sp::elide(alaska, shift = c(-1298669, -3018809))
sp::proj4string(alaska) <- usmap_crs()
names(alaska) <- names(transformed)
}
Expand All @@ -163,12 +159,12 @@ usmap_transform.data.frame <- function(data,
]

if (length(hawaii) > 0) {
hawaii <- maptools::elide(
hawaii <- sp::elide(
hawaii,
rotate = -35,
bb = hi_bbox
)
hawaii <- maptools::elide(hawaii, shift = c(5400000, -1400000))
hawaii <- sp::elide(hawaii, shift = c(5400000, -1400000))
sp::proj4string(hawaii) <- usmap_crs()
names(hawaii) <- names(transformed)
}
Expand Down Expand Up @@ -204,6 +200,13 @@ usmap_transform.data.frame <- function(data,
#'
#' @export
usmap_crs <- function() {
set_sp_evolution_status()

if (!requireNamespace("sf", quietly = TRUE)) {
stop("`sf` must be installed to use `usmap_transform`.
Use: install.packages(\"sf\") and try again.")
}

if (!requireNamespace("sp", quietly = TRUE)) {
stop("`sp` must be installed to use `usmap_crs`.
Use: install.packages(\"sp\") and try again.")
Expand All @@ -212,3 +215,19 @@ usmap_crs <- function() {
sp::CRS(paste("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0",
"+a=6370997 +b=6370997 +units=m +no_defs"))
}

#' Set sp evolution status
#'
#' @description
#' Sets the `sp` evolution status to "2" to
#' force usage of `sf` instead of `rgdal`
#' which is being retired.
#'
#' This can be removed in the future when the evolution status
#' is set to >= 2 by default in `sf`.
#'
#' @keywords internal
set_sp_evolution_status <- function() {
if (sp::get_evolution_status() < 2L)
sp::set_evolution_status(2L)
}
17 changes: 17 additions & 0 deletions man/set_sp_evolution_status.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions man/theme_map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 132175b

Please sign in to comment.