Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions R/latlong2fips.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#' @author Loren Collingwood <loren.collingwood@@ucr.edu>
#' @references https://geo.fcc.gov/api/census/block/
#' @examples
#'
#' \dontrun{
#' # EXAMPLE: NOT RUN #
#' # census_block <- list()
Expand All @@ -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
))
}