R call_geolocator_latlon function returns NA

922 Views Asked by At

I am trying to obtain the Census tract information using the R tigris package and following the process as detailed here: Retrieve Census tract from Coordinates My code was working till last month when it suddenly stopped working and returns NA for any lat, lon combination. I looked up the documentation on tigris package and do not see what I am doing wrong. I also updated my tigris package but the following command keeps returning NA.

 library(tigris)
 call_geolocator_latlon(40.61847, -74.02123)

Does anyone know if the package has been deprecated?

2

There are 2 best solutions below

1
On

try this

replacement_function <- function (lat, lon, benchmark, vintage) 
{
  if (missing(benchmark)) {
    benchmark <- "Public_AR_Census2020"
  }
  else {
    benchmark <- benchmark
  }
  if (missing(vintage)) {
    vintage <- "Census2020_Census2020"
  }
  else {
    vintage <- vintage
  }
  call_start <- "https://geocoding.geo.census.gov/geocoder/geographies/coordinates?"
  url <- paste0("x=", lon, "&y=", lat)
  benchmark0 <- paste0("&benchmark=", benchmark)
  vintage0 <- paste0("&vintage=", vintage, "&format=json")
  url_full <- paste0(call_start, url, benchmark0, vintage0)
  r <- httr::GET(url_full)
  httr::stop_for_status(r)
  response <- httr::content(r)
  return(response$result$geographies$`Census Blocks`[[1]]$GEOID)
  if (length(response$result$geographies$`2020 Census Blocks`[[1]]$GEOID) ==
      0) {
    message(paste0("Lat/lon (", lat, ", ", lon, ") returned no geocodes. An NA was returned."))
    return(NA_character_)
  }
  else {
    if (length(response$result$geographies$`2020 Census Blocks`[[1]]$GEOID) >
        1) {
      message(paste0("Lat/lon (", lat, ", ", lon, ") returned more than geocode. The first match was returned."))
    }
    return(response$result$geographies$`2020 Census Blocks`[[1]]$GEOID)
  }
}

you can call this function the same way that you would call the call_geolocator_latlon() function

replacement_function(40.61847, -74.02123)
0
On

According to official page, January and February 2021 are months for update of shapes. These will be available from March.