如何在R或Python中将County FIPS转换为经纬度坐标?

问题描述:

由于世界卫生组织的IARC将草甘膦列为可能的致癌物质,草甘膦最近引起了人们的关注.我对美国的农药使用模式感到好奇,并例如使用传单在R-Shiny中使用交互式地图绘制了这些数据.

Glyphosate has hogged the limelight recently due to listing as a possible carcinogen by the World Health Organization's IARC. I got curious about pesticide usage patterns in the US and plotting this data with interactive maps in R-Shiny, using leaflet, for example.

可在此处找到县级农药使用量估算值: https://water.usgs.gov/nawqa/pnsp/usage/maps/county-level/

County-level estimates of pesticide usage can be found here: https://water.usgs.gov/nawqa/pnsp/usage/maps/county-level/

使用州/县FIPS代码报告数据.我需要纬度经纬度才能显示数据.

The data are reported using State/County FIPS codes. I require lat-long coordinates to show the data.

如本API此处所示,似乎很容易从lat-long转到FIPS: https://geo.fcc.gov/api/census/

It seems possible to go from lat-long to FIPS quite easily, as illustrated by this API here: https://geo.fcc.gov/api/census/

如何反向行驶?

我找到了使用来自here.com的REST API(以下五个选项)所需要的解决方案.我首先使用library(tigris)中的表fips_codes将USGS表中的FIPS代码与县和州名称进行交叉引用.这给了我一些名字,像Boulder County, CO一样放在地址行中.接下来,我编写了一个小函数here_now,其示例用法为:

The solution I found required using a REST API from here.com of the five options (below). I first cross-referenced FIPS codes from USGS table with County and State names using table fips_codes from library(tigris). This gave me names to put together in address lines, like Boulder County, CO. Next, I wrote a small function here_now with sample usage as:

here_now("Boulder+County,+CO") # $lat: 40.08791; $lon: -105.3447

实施是使用library(jsonlite)

here_now <- function(searchtext) {

    AppCode <- getOption("hereAppCode")
    AppID <- getOption("hereAppID")       

    rootURL <- "https://geocoder.api.here.com/6.2/geocode.json?"
    app_id = paste("app_id", AppID, sep="=")
    app_code = paste("app_code", AppCode, sep="=")
    searchtext = paste("searchtext", searchtext, sep="=")

    request <- paste(paste(rootURL, app_id, sep=''), app_code, searchtext, sep="&")
    response = fromJSON(request)

    res <- list()
    res$lat <- response$Response$View$Result[[1]]$Location$NavigationPosition[[1]]$Latitude
    res$lon <- response$Response$View$Result[[1]]$Location$NavigationPosition[[1]]$Longitude

    res
}

此外,我使用了FCC的反向地理编码API进行验证: https://geo. fcc.gov/api/census/

Further, I used the FCC's reverse geo-coding API to validate: https://geo.fcc.gov/api/census/

我尝试过进行地理编码的选项包括: -通过ggmap的Google API(需要API密钥,需要信用卡) -mapquest API(需要API密钥,无需信用卡) -数据科学工具包的RDSK实施 -通过同名的R包进行地名服务 -此处的API(需要AppID和AppCode,免费增值模式)

Options I experimented with for geocoding included: - google APIs via ggmap (requires API key, requires credit card) - mapquest API (requires API key, no credit card needed) - Data Science Toolkit's RDSK implementation - Geonames service via eponymous R package - Here APIs (require AppID and AppCode, freemium model)