diff --git a/R/latlong2fips.R b/R/latlong2fips.R index c5058e1..15d4657 100644 --- a/R/latlong2fips.R +++ b/R/latlong2fips.R @@ -11,7 +11,6 @@ #' @author Loren Collingwood #' @references https://geo.fcc.gov/api/census/block/ #' @examples -#' #' \dontrun{ #' # EXAMPLE: NOT RUN # #' # census_block <- list() @@ -28,28 +27,27 @@ #' #' @export latlong2fips latlong2fips <- function(latitude, longitude, number) { - cat("Communicating with geo.fcc.gov...\n") - url <- paste("https://geo.fcc.gov/api/census/block/find?latitude=", latitude, "&longitude=", longitude, "&showall=true&format=json", sep = "") - url <- sprintf(url, latitude, longitude) - - if (requireNamespace("RJSONIO", quietly = TRUE)) { - json <- RJSONIO::fromJSON(url) - } else { - message("This utility requires RJSONIO. Please install and re-run.") + if (!requireNamespace("RJSONIO", quietly = TRUE)) { + stop("This utility requires RJSONIO. Please install and re-run.") } + cat("Communicating with geo.fcc.gov...\n") + url <- sprintf( + "https://geo.fcc.gov/api/census/block/find?latitude=%f&longitude=%f&showall=true&format=json", + as.numeric(latitude), as.numeric(longitude) + ) + json <- RJSONIO::fromJSON(url) - if (length("json$Block$FIPS") == 0 | is.null(json$Block$FIPS)) { # error here - cat(paste("Probably Bad LAT/LONG Coordinate. Couldn't calculate.\nRow:", number, sep = " ")) + if (is.null(json$Block$FIPS) || length(json$Block$FIPS) == 0) { # error here + warning("Probably Bad LAT/LONG Coordinate. Couldn't calculate.\nRow:", number, ) return(data.frame(row_id = number, FIP = NA, stringsAsFactors = F)) + } + if (json$status != "OK") { + FIP <- "CONVERGE-FAIL" } else { - if (json$status == "OK") { - number <- number - } else { - number <- "CONVERGE-FAIL" - } - return(data.frame( - row_id = number, FIP = as.character(json$Block["FIPS"]), - stringsAsFactors = F - )) + FIP <- as.character(json$Block["FIPS"]) } + return(data.frame( + row_id = number, FIP = FIP, + stringsAsFactors = F + )) }