Skip to content

Instantly share code, notes, and snippets.

@mhweber
Forked from ryan-hill/streamstats_watershed.R
Created May 8, 2018 21:19
Show Gist options
  • Save mhweber/ab7bb9020e67d8ab813f3dd5de0d6e20 to your computer and use it in GitHub Desktop.
Save mhweber/ab7bb9020e67d8ab813f3dd5de0d6e20 to your computer and use it in GitHub Desktop.
library(jsonlite);library(sf)
#lon = -123.1121
#lat = 44.6390
#state = 'OR'
watershed = function(state, lon, lat){
p1 = 'https://streamstats.usgs.gov/streamstatsservices/watershed.geojson?rcode='
p2 = '&xlocation='
p3 = '&ylocation='
p4 = '&crs=4326&includeparameters=false&includeflowtypes=false&includefeatures=true&simplify=true'
query <- paste0(p1, state, p2, toString(lon), p3, toString(lat), p4)
mydata <- fromJSON(query)
tmp <- mydata$featurecollection[2,2]
stringJson <- toJSON(tmp)
new <- substr(stringJson, 2, nchar(stringJson) -1)
final <- st_read(new)
final
}
ws = watershed(state, lon, lat)
plot(ws$geometry, col='red')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment