-
-
Save ramhiser/f09a71d96a4dec80994c to your computer and use it in GitHub Desktop.
# FCC's Census Block Conversions API | |
# http://www.fcc.gov/developers/census-block-conversions-api | |
latlong2fips <- function(latitude, longitude) { | |
url <- "http://data.fcc.gov/api/block/find?format=json&latitude=%f&longitude=%f" | |
url <- sprintf(url, latitude, longitude) | |
json <- RCurl::getURL(url) | |
json <- RJSONIO::fromJSON(json) | |
as.character(json$County['FIPS']) | |
} | |
# Orange County | |
latlong2fips(latitude=28.35975, longitude=-81.421988) |
Thanks for sharing this! I have been trying to implement this code but have been getting the following error:
Error in function (type, msg, asError = TRUE) :
Unknown SSL protocol error in connection to geo.fcc.gov:443
I think this might be an issue with my RCurl but don't really know where to start. Any information would be helpful!
Using:
R version 3.5.3 (2019-03-11)
Windows Platform: x86_64-w64-mingw32/x64 (64-bit)
The code works if "json <- RCurl::getURL(url)" is removed.
latlong2fips <- function(latitude, longitude) {
url <- "https://geo.fcc.gov/api/census/block/find?format=json&latitude=%f&longitude=%f"
url <- sprintf(url, latitude, longitude)
json <- RJSONIO::fromJSON(url)
as.character(json$County['FIPS'])
}
Thanks for this useful code! Came in very clutch for me.
Here's my edit:
Use Lat/Long to lookup missing county FIPS ------------------------------
if (!require("RJSONIO")) install.packages("RJSONIO")
latlong2fips =
function(latitude, longitude) {
url <- "https://geo.fcc.gov/api/census/area?lat=%s&lon=%s&censusYear=2010&format=json"
url <- sprintf(url, latitude, longitude)
json <- RJSONIO::fromJSON(url)
as.character(json[["results"]][[1]][["county_fips"]])
}
latlong2fips(latitude=28.35975, longitude=-81.421988)
The result:
"12095"
Since it's probably most useful to use this not for a single lookup, but for lots of inputs, here's a short code snippet to help with that (if you have a vectorized way of doing it, please share!)
Make a data.table
a = data.table(latitude=c(28.35975,28.36975,29.33), longitude=c(-81.421988,-81.431988,-81.55))
Pre-allocate space
a[,fips:="0"]
Loop and add the new FIPS codes
for(i in 1:nrow(a)){
lat = a[i, latitude]
lon = a[i, longitude]
a[i, fips := latlong2fips(latitude = lat, longitude = lon)]
}
The FCC's Census Block Conversions API has been replaced by the new Area and Census Block API.
See notice at: https://www.fcc.gov/census-block-conversions-api
Suggest replacing the above with:
FCC's Area and Census Block API
https://geo.fcc.gov/api/census/
latlong2fips <- function(latitude, longitude) {
url <- "https://geo.fcc.gov/api/census/block/find?format=json&latitude=%f&longitude=%f"
url <- sprintf(url, latitude, longitude)
json <- RCurl::getURL(url)
json <- RJSONIO::fromJSON(json)
as.character(json$County['FIPS'])
}
Orange County
latlong2fips(latitude=28.35975, longitude=-81.421988)