DOI-USGS / nawqa_wqp

Scripts/workflow for Water Quality Portal pulls for NAWQA trends and networks analyses.
Other
4 stars 3 forks source link

get state codes using dataRetrieval #4

Open aappling-usgs opened 6 years ago

aappling-usgs commented 6 years ago

I think @ldecicco-USGS pointed out that the current code for getting state codes could be simplified by using the right dataRetrieval function.

Here's what we currently have in wqp_inventory.R that could probably be simplified:

get_wqp_state_codes <- function() {
  states_xml <- xml2::read_xml('https://www.waterqualitydata.us/Codes/statecode?countrycode=US')
  states_list <- xml2::as_list(states_xml)
  states_df <- bind_rows(lapply(states_list[names(states_list)=='Code'], function(code) {
    data_frame(
      value = attr(code, 'value'),
      name = attr(code, 'desc'),
      providers = attr(code, 'providers'))
  }))
  if(nrow(states_df) < 1){stop('No State FIPS codes downloaded,
                               which is critical for later steps, check 
                               that get_wqp_state_codes works')}
  return(states_df)
}
ldecicco-USGS commented 6 years ago

Within dataRetrieval there's some data frames included that have state and county information:

library(dataRetrieval)
names(stateCd)
[1] "STATE"      "STUSAB"     "STATE_NAME" "STATENS"
names(countyCd)
[1] "STUSAB"      "STATE"       "COUNTY"      "COUNTY_NAME"
[5] "COUNTY_ID" 

So, if you were looking up things by states for instance, you could:

for(st in stateCd$STATE){

there are also 2 helper functions stateCdLookup and countyCdLookup. The 2 arguments are basically input and desired output:

fullName <- stateCdLookup("wi", "fullName")
fullName 
[1] "Wisconsin"
abbriev <- stateCdLookup("Wisconsin", "postal")
abbriev 
[1] "WI"
id <- stateCdLookup("WI", "id")
id
[1] 55
name <- countyCdLookup(state = "OH", county = 13, output = "fullName")
name
[1] "Belmont County"