Skip to content

Commit aee0d4b

Browse files
authored
Refactor map creation functions (#17)
* Created `alaska_bbox()`, `hawaii_bbox()`, `alaska_transform()`, and `hawaii_transform()` internal functions. * These are intended to be used by `usmap::usmap_transform()` for consistency. * Removed `as_sf` deprecation warnings since this change is intended to be an implementation detail from the perspective of `usmap`. * i.e. warnings should not be seen by `usmap` users for decisions made within this package they have little control over
2 parents 5d39e8d + 9d5af3a commit aee0d4b

9 files changed

Lines changed: 100 additions & 37 deletions

File tree

‎R/create-us-map.R‎

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@
55
#' `create_us_map()` creates the modified shapefiles used by the
66
#' \link[usmap]{usmap} package.
77
#'
8+
#' `ea_crs()` returns the US National Atlas Equal Area coordinate reference system
9+
#' (CRS) used by this package and `usmap`.
10+
#'
811
#' `transform2D()` computes a two dimensional affine transformation matrix
912
#' for the provided rotation angle and scale factor.
1013
#'
14+
#' `transform_alaska()` applies the appropriate transform for the Alaska polygons.
15+
#'
16+
#' `transform_hawaii()` applies the appropriate transform for the Hawaii polygons.
17+
#'
1118
#' `compute_centroids()` computes the modified centroids for each state or
1219
#' county polygon using a center-of-mass technique on the largest polygon in
1320
#' the region.
1421
#'
22+
#' `alaska_bbox()` returns the bounding box of Alaska pre-transformation.
23+
#'
24+
#' `hawaii_bbox()` returns the bounding box of Hawaii pre-transformation.
25+
#'
1526
#' @note
1627
#' Using these functions externally is not recommended since they make certain
1728
#' undocumented assumptions that may not work with all inputs.
@@ -51,20 +62,13 @@ create_us_map <- function(
5162
us <- sf::read_sf(input_file)
5263

5364
# ea: US National Atlas Equal Area
54-
ea_crs <- sf::st_crs(9311)
55-
us_ea <- sf::st_transform(us, ea_crs)
65+
us_ea <- sf::st_transform(us, ea_crs())
5666

5767
# FIPS code for Alaska = 02
58-
alaska <- us_ea[us_ea$STATEFP == "02", ]
59-
sf::st_geometry(alaska) <- sf::st_geometry(alaska) * transform2D(-50, 1 / 2)
60-
sf::st_geometry(alaska) <- sf::st_geometry(alaska) + c(3e5, -2e6)
61-
sf::st_crs(alaska) <- ea_crs
68+
alaska <- transform_alaska(us_ea[us_ea$STATEFP == "02", ])
6269

6370
# FIPS code for Hawaii = 15
64-
hawaii <- us_ea[us_ea$STATEFP == "15", ]
65-
sf::st_geometry(hawaii) <- sf::st_geometry(hawaii) * transform2D(-35)
66-
sf::st_geometry(hawaii) <- sf::st_geometry(hawaii) + c(3.6e6, 1.8e6)
67-
sf::st_crs(hawaii) <- ea_crs
71+
hawaii <- transform_hawaii(us_ea[us_ea$STATEFP == "15", ])
6872

6973
# keep only US states (i.e. remove territories, minor outlying islands, etc.)
7074
# also remove Alaska (02) and Hawaii (15) so that we can add in shifted one
@@ -116,6 +120,12 @@ create_us_map <- function(
116120
sf::st_write(centroids, centroids_output_file, quiet = TRUE, append = FALSE)
117121
}
118122

123+
#' @rdname create_us_map
124+
#' @keywords internal
125+
ea_crs <- function() {
126+
sf::st_crs(9311) # US National Atlas Equal Area coordinate reference system
127+
}
128+
119129
#' @rdname create_us_map
120130
#' @keywords internal
121131
transform2D <- function(angle = 0, scale = 1) {
@@ -124,6 +134,26 @@ transform2D <- function(angle = 0, scale = 1) {
124134
-scale * sin(r), scale * cos(r)), 2, 2)
125135
}
126136

137+
#' @rdname create_us_map
138+
#' @keywords internal
139+
transform_alaska <- function(alaska) {
140+
sf::st_geometry(alaska) <- sf::st_geometry(alaska) * transform2D(-50, 1 / 2)
141+
sf::st_geometry(alaska) <- sf::st_geometry(alaska) + c(3e5, -2e6)
142+
sf::st_crs(alaska) <- ea_crs()
143+
144+
alaska
145+
}
146+
147+
#' @rdname create_us_map
148+
#' @keywords internal
149+
transform_hawaii <- function(hawaii) {
150+
sf::st_geometry(hawaii) <- sf::st_geometry(hawaii) * transform2D(-35)
151+
sf::st_geometry(hawaii) <- sf::st_geometry(hawaii) + c(3.6e6, 1.8e6)
152+
sf::st_crs(hawaii) <- ea_crs()
153+
154+
hawaii
155+
}
156+
127157
#' @rdname create_us_map
128158
#' @keywords internal
129159
compute_centroids <- function(polygons, iterations = 3, initial_width_step = 10) {
@@ -195,3 +225,31 @@ compute_centroids <- function(polygons, iterations = 3, initial_width_step = 10)
195225
sf::st_agr(new_polygons) <- "constant"
196226
sf::st_centroid(new_polygons)
197227
}
228+
229+
#' @rdname create_us_map
230+
#' @keywords internal
231+
alaska_bbox <- function() {
232+
sf::st_bbox(
233+
c(
234+
xmin = -4377000,
235+
xmax = -1519000,
236+
ymin = 1466000,
237+
ymax = 3914000
238+
),
239+
crs = ea_crs()
240+
)
241+
}
242+
243+
#' @rdname create_us_map
244+
#' @keywords internal
245+
hawaii_bbox <- function() {
246+
sf::st_bbox(
247+
c(
248+
xmin = -5750000,
249+
xmax = -5450000,
250+
ymin = -1050000,
251+
ymax = -441000
252+
),
253+
crs = ea_crs()
254+
)
255+
}

‎R/fips-data.R‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @param regions The region breakdown for the map, can be one of
44
#' (\code{"states"}, \code{"state"}, \code{"counties"}, \code{"county"}).
55
#' The default is \code{"states"}.
6-
#' @param as_sf DEPRECATED. This parameter has no effect and will be removed in
6+
#' @param as_sf Defunct, this parameter no longer has any effect and will be removed in
77
#' the future.
88
#'
99
#' @return An data frame of FIPS codes of the desired \code{regions}.
@@ -21,10 +21,6 @@ fips_data <- function(
2121
) {
2222
regions <- match.arg(regions)
2323

24-
if (!missing("as_sf"))
25-
warning("`as_sf` is deprecated and no longer has any effect, all data is
26-
returned as an `sf` object.")
27-
2824
map_data <- usmapdata::us_map(regions)
2925
sf::st_geometry(map_data) <- NULL
3026
map_data

‎R/us-map.R‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' same name. The regions listed in the \code{include} parameter are applied first and the
1515
#' \code{exclude} regions are then removed from the resulting map. Any excluded regions
1616
#' not present in the included regions will be ignored.
17-
#' @param as_sf DEPRECATED. This parameter has no effect and will be removed in
17+
#' @param as_sf Defunct, this parameter no longer has any effect and will be removed in
1818
#' the future.
1919
#'
2020
#' @return An `sf` data frame of US map coordinates divided by the desired \code{regions}.
@@ -36,10 +36,6 @@ us_map <- function(
3636
) {
3737
regions <- match.arg(regions)
3838

39-
if (!missing("as_sf"))
40-
warning("`as_sf` is deprecated and no longer has any effect, all data is
41-
returned as an `sf` object.")
42-
4339
if (regions == "state") regions <- "states"
4440
else if (regions == "county") regions <- "counties"
4541

@@ -70,7 +66,7 @@ us_map <- function(
7066
#' @param regions The region breakdown for the map, can be one of
7167
#' (\code{"states"}, \code{"counties"}, as specified by the internal file names.
7268
#' The default is \code{"states"}.
73-
#' @param as_sf DEPRECATED. This parameter has no effect and will be removed in
69+
#' @param as_sf Defunct, this parameter no longer has any effect and will be removed in
7470
#' the future.
7571
#'
7672
#' @return An `sf` data frame of state or county centroid labels and positions
@@ -83,10 +79,6 @@ centroid_labels <- function(
8379
) {
8480
regions <- match.arg(regions)
8581

86-
if (!missing("as_sf"))
87-
warning("`as_sf` is deprecated and no longer has any effect, all data is
88-
returned as an `sf` object.")
89-
9082
sf::read_sf(
9183
system.file("extdata", paste0("us_", regions, "_centroids.gpkg"),
9284
package = "usmapdata")

‎man/centroid_labels.Rd‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/create_us_map.Rd‎

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

‎man/fips_data.Rd‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/us_map.Rd‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-fips-data.R‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,3 @@ test_that("county FIPS codes load correctly", {
3939
expect_equal(county_fips[[3144, "county"]], "Weston County")
4040
expect_equal(county_fips[[3144, "fips"]], "56045")
4141
})
42-
43-
test_that("as_sf deprecation warning occurs", {
44-
expect_warning(fips_data(as_sf = TRUE))
45-
})

‎tests/testthat/test-usmap.R‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,3 @@ test_that("centroid labels are loaded", {
7474
expect_equal(length(centroid_labels("states")[[1]]), 51)
7575
expect_equal(length(centroid_labels("counties")[[1]]), 3144)
7676
})
77-
78-
test_that("as_sf deprecation warning occurs", {
79-
expect_warning(us_map(as_sf = TRUE))
80-
expect_warning(centroid_labels(as_sf = TRUE))
81-
})

0 commit comments

Comments
 (0)