How to support Hydrology service
We have discussed the hydrology service as a possible supported service. However, it is not clear how this service works or how it can be useful to R users.
To me, it appears that we send data to AGOL for a job. We then wait for the job to complete and poll the job endpoint until it succeeds. When it succeeds what do we expect? Do we want data to come back into R? Do we want something informative? I'm unsure what is happening with the endpoint.
library(httr2)
token <- arcgis::auth_client()$access_token
burl <- "https://hydro.arcgis.com/arcgis/rest/services/Tools/Hydrology/GPServer/Watershed/submitJob"
resp <- request(burl) |>
req_body_form(
InputPoints = '{"geometryType":"esriGeometryPoint","features":[{"geometry":{"x":-11946425.36295705,"y":4930494.072505761,"spatialReference":{"wkid":102100,"latestWkid":3857}}}],"sr":{"wkid":102100,"latestWkid":3857}}',
SnapDistance = 500,
SnapDistanceUnits = "Meters",
Generalize = TRUE,
f = "json",
token = token
) |>
req_perform()
res_id <- resp_body_json(resp)
job_res_url <- glue::glue("https://hydro.arcgis.com/arcgis/rest/services/Tools/Hydrology/GPServer/Watershed/jobs/{res_id$jobId}")
Sys.sleep(5)
job_resp <- request(job_res_url) |>
req_body_form(
f = "json",
token = token
) |>
req_perform()
job_res <- resp_body_json(job_resp)
job_res$results
#> $WatershedArea
#> $WatershedArea$paramUrl
#> [1] "results/WatershedArea"
#>
#>
#> $SnappedPoints
#> $SnappedPoints$paramUrl
#> [1] "results/SnappedPoints"
Created on 2023-04-03 with reprex v2.0.2
Yes provide a point, get back a polygon of a watershed to pass back to R. Application example would be to summarize some landscape variable within the polygon. Or you could do several watersheds, compute slope, and do zonal statistics on the slope and watersheds to generate average slope of each watershed, and pass that back to R for further analysis.
Your code snippet is right on, @JosiahParry, I think if you just do some final requests to grab the data associated with the output parameters, the results can be brought back into R:
# ... your code above ...
job_res_url_outp1 <- glue::glue("https://hydro.arcgis.com/arcgis/rest/services/Tools/Hydrology/GPServer/Watershed/jobs/{res_id$jobId}/{job_res$results$WatershedArea}")
job_outp1 <- request(job_res_url_outp1) |>
req_body_form(
f = "json",
token = token
) |>
req_perform()
outp1 <- resp_body_json(job_outp1)
outp1
#> $paramName
#> [1] "WatershedArea"
#>
#> $dataType
#> [1] "GPFeatureRecordSetLayer"
#>
#> $value
#> $value$displayFieldName
#> [1] ""
#>
#> $value$geometryType
#> [1] "esriGeometryPolygon"
#>
#> $value$spatialReference
#> $value$spatialReference$wkid
#> [1] 4326
#>
#> $value$spatialReference$latestWkid
#> [1] 4326
#>
#>
#> $value$fields
#> $value$fields[[1]]
#> $value$fields[[1]]$name
#> [1] "OBJECTID"
#>
# etc. etc.
(and doing the same for any other output params, like SnappedPoints in this case.)
Also I was finding it helpful to test manual requests here to see what the expected results/outputs would be: https://hydro.arcgis.com/arcgis/rest/services/Tools/Hydrology/GPServer
As mentioned by Steve above, that would be on use case. Another common use case is to summarize/find out what is the predominant landuse within the watershed. Or clip a precipitation dataset within the watershed polygon and continue with other available hydrology packages in R for modeling.
@mmachir yes, don't worry too much about the correctness of the output, I can check on that later 👍