Last active
December 3, 2020 01:47
-
-
Save kissmygritts/b39d881591a0f115688006c0781fadf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
usgsapi <- function () { | |
API_URL <- 'https://m2m.cr.usgs.gov/api/api/json/stable/' | |
ENDPOINTS <- list( | |
login = paste0(API_URL, 'login'), | |
permissions = paste0(API_URL, 'permissions'), | |
scene_search = paste0(API_URL, 'scene-search') | |
) | |
TOKEN <- NULL | |
# internal methods ---- | |
set_token <- function (token) { | |
TOKEN <<- token | |
} | |
get_token <- function () { | |
return(TOKEN) | |
} | |
AUTH_HEADER <- function () { | |
return(httr::add_headers('X-Auth-Token' = get_token())) | |
} | |
# login function ---- | |
login <- function (username, password) { | |
body <- jsonlite::toJSON(list( | |
username = username, | |
password = password | |
), auto_unbox = TRUE) | |
## run post request | |
req <- httr::POST(ENDPOINTS$login, | |
config = list( | |
httr::add_headers('Content-Type' = 'application/json') | |
), | |
body = body | |
) | |
## set token | |
res <- httr::content(req) | |
TOKEN <<- set_token(res$data) | |
## return response | |
return(res) | |
} | |
# permissions function ---- | |
permissions <- function (...) { | |
# because the token is within the scope of the parent function (usgsapi) | |
# you don't need to provide it manually, you can grap it from the parent scope | |
# this also means you can check if you are authenticated, and if not run the | |
# login function first | |
res <- httr::POST(ENDPOINTS$permissions, | |
add_headers('X-Auth-Token' = get_token()), | |
... | |
) | |
return(res) | |
} | |
scene_search <- function (body, ...) { | |
req <- httr::POST(ENDPOINTS$scene_search, AUTH_HEADER(), body = body, ...) | |
return(req) | |
} | |
return(list( | |
get_token = get_token, | |
login = login, | |
permissions = permissions, | |
scene_search = scene_search | |
)) | |
} | |
# initialize API | |
api <- usgsapi() | |
# run login function ---- | |
## runngint this line will also set your token! | |
res <- api$login(username = "_kmg_", password = "l2Q9B5aZjw8ibf") | |
res | |
(token <- api$get_token()) | |
# permissions ---- | |
res <- api$permissions(verbose()) | |
content(res) | |
# scene search ---- | |
body <- list( | |
maxResults = 2, | |
datasetName = 'gls_all' | |
) | |
(body <- jsonlite::toJSON(body, auto_unbox = TRUE)) | |
res <- api$scene_search(body = body, verbose(), encode = 'json') | |
jsonlite::toJSON(content(res)) | |
data <- content(res) | |
data | |
data$data$results %>% str() | |
data$data$results[[1]] %>% str() | |
# try a real request ---- | |
st_bbox_to_poly <- function (bbox) { | |
x1 <- bbox[1] | |
y1 <- bbox[2] | |
x2 <- bbox[3] | |
y2 <- bbox[4] | |
sf::st_polygon(list(matrix(c( | |
x1, y1, | |
x1, y2, | |
x2, y2, | |
x2, y1, | |
x1, y1 | |
), ncol = 2, byrow = TRUE))) | |
} | |
poly <- st_bbox_to_poly(shape) %>% | |
sf::st_sfc(crs = 4326) %>% st_as_sf() | |
(geojson <- geojsonsf::sf_geojson(poly)) | |
body <- list( | |
datasetName = 'LANDSAT_8_C1', | |
maxResults = 1, | |
sceneFilter = list( | |
SpatialFilterGeoJson = list( | |
filterType = 'geoJson', | |
geojson = geojson | |
) | |
) | |
) | |
body | |
res <- api$scene_search(body = body, verbose(), encode = 'json') | |
content(res) | |
jsonlite::toJSON(content(res)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment