'Obtain watershed shapefile from API
I am currently working with the APIs modelmywatershed.org/api and https://modelmywatershed.org/api/docs/ trying to delineate a watershed for a given location.
library(httr)
library(jsonlite)
library(dplyr)
library(rgdal)
library(sp)
headers = c(
`accept` = 'application/json',
`Authorization` = 'Token {Insert API Code Here}',
`Content-Type` = 'application/json',
`X-CSRFToken` = '9P7k41yjWNqVtysnv8iv2KfeW4vadHih1wKZWkN41Mln249QxEZVZHwfvKcufvBO',
'Accept-Encoding' = 'gzip,deflate'
)
data = '{ "location": [35.66735774,-102.4819484],
"snappingOn": true, "simplify": 0.0001, "dataSource": "nhd"}'
# Watershed Delineation
res <-
httr::POST(url = 'https://modelmywatershed.org/api/watershed/',
httr::add_headers(.headers = headers),
body = data, verbose())
api_char <- rawToChar(res$content)
api_JSON <- jsonlite::fromJSON(api_char, flatten = TRUE)
api_JSON
$job
[1] "0aa85683-37d4-4cb2-9b8c-5c1ee81c23fa"
$status
[1] "started"
job_ID <- api_JSON$job
job_ID
#"0aa85683-37d4-4cb2-9b8c-5c1ee81c23fa"
I managed to get the job_ID and was able to get the information for the watershed
headers_watershed = c(`accept` = 'application/json',
`Authorization` = ' Token {Insert API Key Here}',
`X-CSRFToken` = '1I2YxuQlHlxpxPM09qZTlT6vEuFgucuaTpFDpN56MksR6lttbWGjiQnwdamAw0NH'
)
url_1 <-paste0(URL='https://modelmywatershed.org/api/jobs/', job_ID, '/')
res_job <- GET(url=url_1,
add_headers(.headers = headers_watershed))
Watershed_char <- rawToChar(res_job$content)
Watershed_JSON <- jsonlite::fromJSON(Watershed_char, flatten = TRUE)
####################
# Watershed Result #
####################
$job_uuid
[1] "0aa85683-37d4-4cb2-9b8c-5c1ee81c23fa"
$status
[1] "complete"
$result
$result$input_pt
$result$input_pt$geometry
$result$input_pt$geometry$coordinates
[1] -102.48203 35.66726
$result$input_pt$geometry$type
[1] "Point"
$result$input_pt$properties
$result$input_pt$properties$DistStr_m
[1] 0
$result$input_pt$properties$Dist_moved
[1] 0
$result$input_pt$properties$ID
[1] 1
$result$input_pt$properties$Lat
[1] 35.66736
$result$input_pt$properties$Lon
[1] -102.4819
$result$input_pt$type
[1] "Feature"
$result$watershed
$result$watershed$geometry
$result$watershed$geometry$coordinates
[ reached getOption("max.print") -- omitted 2 matrix slice(s) ]
$result$watershed$geometry$type
[1] "Polygon"
$result$watershed$properties
$result$watershed$properties$Area_km2
[1] 3475.114
$result$watershed$properties$GRIDCODE
NULL
$result$watershed$type
[1] "Feature"
$error
[1] ""
$started
[1] "2022-02-28T21:45:41.597568Z"
$finished
[1] "2022-02-28T21:45:56.052379Z"
However the issue is that I do not know how to download the information. Typically, the website produces a .zip file with all the necessary components of a shapefile, but I am struggling on how to create the zipfile to download the information.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
