2

我正在尝试提取大量经度和纬度线的县 fips 数。我可以从 FCC API 获取数据,但很难将其读入 R。

例如,当我在 R 中运行以下代码时:

library(httr)
fips <- GET("http://data.fcc.gov/api/block/find", query = list(latitude = 48.9905, longitude = -122.2733, showall="false"))

result <- content(fips, as = "parsed")

result

对象“结果”显示如下

{xml_document}
<Response>
[1] <Block FIPS="530730102002091"/>
[2] <County FIPS="53073" name="Whatcom"/>
[3] <State FIPS="53" code="WA" name="Washington"/>

我感兴趣的信息是县 FIPS 代码“53073”。我应该如何去提取那个数字?

4

2 回答 2

1

您必须解析XML从该 API 返回的

library("httr")
library("XML")
fips <- GET("http://data.fcc.gov/api/block/find", query = list(latitude = 48.9905, longitude = -122.2733, showall="false"))
result <- content(fips, as = "parsed")
> xmlToList(xmlParse(result))$County["FIPS"]
   FIPS 
"53073" 
于 2016-03-22T20:39:05.533 回答
0

另一种选择是使用以下链接中的代码:Latitude/Longitude to FIPS Codes via the FCC's API

# FCC's Census Block Conversions API
# http://www.fcc.gov/developers/census-block-conversions-api

install.packages("RCurl")
install.packages("RJSONIO")

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)

我相信有一个查询限制,但我还没有完全确定那是什么。

于 2018-02-22T23:37:21.503 回答