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
121131transform2D <- 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
129159compute_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+ }
0 commit comments