How do I convert County FIPS to Lat-Long coordinates in R or Python?

假如想象 提交于 2019-12-24 19:23:04

问题


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.

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

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

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

How to go in the reverse direction?


回答1:


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

Implementation is a call to the REST API using fromJSON from 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
}

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

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)



来源:https://stackoverflow.com/questions/56639306/how-do-i-convert-county-fips-to-lat-long-coordinates-in-r-or-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!