Skip to content

Commit

Permalink
Use usmapdata::fips_data() for FIPS code data (#76)
Browse files Browse the repository at this point in the history
* Added support for `usmapdata 0.1.2` which includes `fips_data()`
function for retrieving state and county FIPS codes.
  * Deleted state and county FIPS files contained in `usmap`.
* Files were outdated and now come from single source of truth in
`usmapdata`.

resolves #72
  • Loading branch information
pdil authored Dec 11, 2023
2 parents 0d8d369 + d35de14 commit 5a390b7
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 3,357 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ URL: https://usmap.dev
BugReports: https://github.com/pdil/usmap/issues
Imports:
rlang,
usmapdata,
utils
usmapdata (>= 0.1.2)
Suggests:
covr,
ggplot2,
Expand Down
14 changes: 12 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# usmap 0.6.3.9999

### Improvements
* Replace local state and county FIPS files with `usmapdata::fips_data()`.
* Single source of truth for this data is now housed in `usmapdata`.
* Data will be updated in sync with shapefile updates.

### Bug Fixes
* FIPS file change resolves issue with Valdez-Cordova Census Area in Alaska, see [Issue #72](https://github.com/pdil/usmap/issues/72).

### Technical Changes
* Resolve all code-linting warnings.
* Increase test coverage to 100%.

# usmap 0.6.3
Released Saturday, October 21, 2023.
Expand Down Expand Up @@ -36,7 +46,7 @@ Released Sunday, February 27, 2022.
* Extract map data frame to external [usmapdata](https://github.com/pdil/usmapdata) package to reduce `usmap` package size, see [Issue #39](https://github.com/pdil/usmap/issues/39).
* All existing functions (including `us_map()`) should continue to work as usual.
* Add data format examples for `plot_usmap` to "Mapping" vignette, see [Issue #42](https://github.com/pdil/usmap/issues/42).

### Bug Fixes
* Fix CRS warnings, see [Issue #40](https://github.com/pdil/usmap/issues/40).
* Fix `plot_usmap()` issue when provided data has `"values"` column, see [Issue #48](https://github.com/pdil/usmap/issues/48) and [this Stack Overflow question](https://stackoverflow.com/questions/61111024/trouble-using-plot-usmap-function-in-usmap-package).
Expand Down Expand Up @@ -71,7 +81,7 @@ Released Friday, September 13, 2019.
* Ability to exclude counties and states with new `exclude` parameter in `us_map`, `map_with_data`, and `plot_usmap`, see [Issue #19](https://github.com/pdil/usmap/issues/19).
* New function (`usmap_transform`) to transform longitude/latitude point data frames into coordinate reference system that matches the plotted map, see [Issue #21](https://github.com/pdil/usmap/issues/21).
* Also includes `usmap_crs()` to easily access the coordinate reference system used by the package.

### Improvements
* In the data frames provided by `us_map()`, `long` and `lat` have been renamed to `x` and `y`, respectively, see [Issue #16](https://github.com/pdil/usmap/issues/16).
* This should not affect the behavior of `plot_usmap()` but will be a breaking change for any code that relies on `us_map()`.
Expand Down
27 changes: 8 additions & 19 deletions R/fips.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,27 @@
#' @export
fips <- function(state, county = c()) {
if (missing(state) && missing(county)) {
df <- utils::read.csv(system.file("extdata", "state_fips.csv", package = "usmap"))
return(sprintf("%02d", df$fips))
return(usmapdata::fips_data()$fips)
}

state_ <- tolower(state)
county_ <- tolower(county)

if (length(county_) == 0) {
df <- utils::read.csv(system.file("extdata", "state_fips.csv", package = "usmap"))
df <- usmapdata::fips_data()
abbr <- tolower(df$abbr)
full <- tolower(df$full)
fips2 <- c(df$fips, df$fips)

result <- fips2[match(state_, c(abbr, full))]

formatted_result <- sprintf("%02d", result)
formatted_result[formatted_result == "NA"] <- NA
formatted_result
result[result == "NA"] <- NA
result
} else {
if (length(state_) > 1) {
stop("`county` parameter cannot be used with multiple states.")
}

df <- utils::read.csv(system.file("extdata", "county_fips.csv", package = "usmap"))
df <- usmapdata::fips_data("counties")
name <- tolower(df$county)
state_abbr <- tolower(df$abbr)
state_full <- tolower(df$full)
Expand All @@ -99,7 +96,7 @@ fips <- function(state, county = c()) {
stop(paste0(county, " are not valid counties in ", state, ".\n"))
}
} else {
sprintf("%05d", result)
result
}
}
}
Expand Down Expand Up @@ -173,18 +170,10 @@ fips_info.character <- function(fips, sortAndRemoveDuplicates = FALSE) {
#' @keywords internal
get_fips_info <- function(fips, sortAndRemoveDuplicates) {
if (all(nchar(fips) == 2)) {
df <- utils::read.csv(
system.file("extdata", "state_fips.csv", package = "usmap"),
colClasses = rep("character", 3), stringsAsFactors = FALSE
)

df <- usmapdata::fips_data()
columns <- c("abbr", "fips", "full")
} else if (all(nchar(fips) == 5)) {
df <- utils::read.csv(
system.file("extdata", "county_fips.csv", package = "usmap"),
colClasses = rep("character", 4), stringsAsFactors = FALSE
)

df <- usmapdata::fips_data("counties")
columns <- c("full", "abbr", "county", "fips")
}

Expand Down
25 changes: 3 additions & 22 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ usmap_transform <- function(data,
output_names = c("x", "y")) {

# 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.")
Expand Down Expand Up @@ -121,9 +120,9 @@ usmap_transform.data.frame <- function(data,

alaska <- transformed[
transformed@coords[, 1] >= ak_bbox[1, 1] &
transformed@coords[, 1] <= ak_bbox[1, 2] &
transformed@coords[, 2] >= ak_bbox[2, 1] &
transformed@coords[, 2] <= ak_bbox[2, 2],
transformed@coords[, 1] <= ak_bbox[1, 2] &
transformed@coords[, 2] >= ak_bbox[2, 1] &
transformed@coords[, 2] <= ak_bbox[2, 2],
]

if (length(alaska) > 0) {
Expand Down Expand Up @@ -192,8 +191,6 @@ 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.")
Expand All @@ -207,19 +204,3 @@ 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)
}
Loading

0 comments on commit 5a390b7

Please sign in to comment.