我之前知道如何使用FCC API (Apply an API Function over 2 columns of Dataframe, Output a Third Column)将经纬度转换为县FIPS代码,这要归功于@caldwellst和@rohit。不幸的是,FCC修改了API,我不知道如何修复代码才能再次工作。
这里有一个指向新接口的链接:https://geo.fcc.gov/api/census/
这是我的数据框架:
> head(df_coords)
# A tibble: 6 x 3
lon lat censusYear
<dbl> <dbl> <dbl>
1 -112. 33.4 2010
2 -73.2 44.5 2010
3 -88.2 41.9 2010
4 -88.2 41.9 2010
5 -88.4 41.9 2010
6 -77.1 39.0 2010
下面是我之前借用/改编的函数以及运行它的命令:
geo2fips <- 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'])
}
df_fips$county_fips <- mapply(geo2fips, df_fips$lat, df_fips$lon)
下面是我在运行它时得到的错误消息:
Error in function (type, msg, asError = TRUE) :
Unknown SSL protocol error in connection to geo.fcc.gov:443
有人能帮我解决这个问题吗?我认为这可能与人口普查年份的要求有关,所以我尝试修改代码,如下所示,但它返回了相同的错误消息:
geo2fips <- function(latitude, longitude, censusYear) {
+ url <- "https://geo.fcc.gov/api/census/block/find?format=json&latitude=%f&longitude=%f&censusYear=%f"
+ url <- sprintf(url, latitude, longitude, censusYear)
+ json <- RCurl::getURL(url)
+ json <- RJSONIO::fromJSON(json)
+ as.character(json$County['FIPS'])
+ }
> df_coords$county_fips <- mapply(geo2fips, df_coords$lat, df_coords$lon, df_coords$censusYear)
Error in function (type, msg, asError = TRUE) :
Unknown SSL protocol error in connection to geo.fcc.gov:443
>
非常感谢任何能帮上忙的人。-Mike
发布于 2020-11-05 15:43:49
对URL和参数做了一些细微的更改-您可以使用:
geo2fips <- function(latitude, longitude) {
url <- "https://geo.fcc.gov/api/census/area?lat=%f&lon=%f&format=json"
res <- jsonlite::fromJSON(sprintf(url, latitude, longitude))[["results"]][["county_fips"]]
unique(res)
}
如果使用jsonlite
包而不是RSJONIO
,也可以稍微简化一些,因为前者直接接受连接。
https://stackoverflow.com/questions/64700195
复制