Skip to content

Commit 2100d16

Browse files
author
Rolf Simoes
committed
Document openstac
1 parent 5b23e62 commit 2100d16

23 files changed

+779
-177
lines changed

DESCRIPTION

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ RoxygenNote: 7.3.1
1515
Depends: R (>= 3.5.0)
1616
Imports:
1717
jsonlite,
18+
plumber
19+
Suggests:
1820
rstac,
19-
sf
21+
mongolite

NAMESPACE

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export(api_stopifnot)
4444
export(check_bbox)
4545
export(check_collection_in_db)
4646
export(check_collections)
47-
export(check_datetime)
4847
export(check_intersects)
4948
export(check_item_in_db)
5049
export(check_limit)
@@ -56,11 +55,8 @@ export(get_host)
5655
export(get_method)
5756
export(parse_datetime)
5857
export(parse_dbl)
58+
export(parse_geojson)
5959
export(parse_int)
60-
export(parse_json)
6160
export(parse_str)
6261
export(set_db)
6362
importFrom(jsonlite,fromJSON)
64-
importFrom(mongolite,mongo)
65-
importFrom(rstac,items_filter)
66-
importFrom(sf,st_sfc)

R/api-ogcapi.R

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ogcapi_conforms_to <- c(
55
"http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30",
66
"http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson"
77
)
8-
8+
#' @rdname api_handling
99
#' @export
1010
create_ogcapi <- function(title, description, conforms_to = NULL, ...) {
1111
create_api(
@@ -15,33 +15,33 @@ create_ogcapi <- function(title, description, conforms_to = NULL, ...) {
1515
conforms_to = c(ogcapi_conforms_to, conforms_to), ...
1616
)
1717
}
18-
18+
#' @rdname api_handling
1919
#' @export
2020
api_landing_page.ogcapi <- function(api, req, res, ...) {
2121
list(title = api$title, description = api$description) |>
2222
links_landing_page(api, req, res)
2323
}
24-
24+
#' @rdname api_handling
2525
#' @export
2626
api_conformance.ogcapi <- function(api, req, res, ...) {
2727
list(conformsTo = api$conforms_to)
2828
}
29-
29+
#' @rdname api_handling
3030
#' @export
3131
api_collections.ogcapi <- function(api, req, res, ...) {
3232
db <- get_db(api)
3333
list(collections = db_collections(db)) |>
3434
links_collections(api, req, res)
3535
}
36-
36+
#' @rdname api_handling
3737
#' @export
3838
api_collection.ogcapi <- function(api, req, res, collection_id, ...) {
3939
db <- get_db(api)
4040
check_collection_in_db(db, collection_id)
4141
db_collection(db, collection_id) |>
4242
links_collection(api, req, res)
4343
}
44-
44+
#' @rdname api_handling
4545
#' @export
4646
api_items.ogcapi <- function(api,
4747
req,
@@ -73,7 +73,7 @@ api_items.ogcapi <- function(api,
7373
page = page
7474
)
7575
}
76-
76+
#' @rdname api_handling
7777
#' @export
7878
api_item.ogcapi <- function(api, req, res, collection_id, item_id, ...) {
7979
db <- get_db(api)

R/api-stac.R

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
stac_version <- "1.0.0"
22

3+
#' @rdname api_handling
34
#' @export
45
create_stac <- function(id, title, description, conforms_to = NULL, ...) {
56
create_api(
@@ -11,7 +12,7 @@ create_stac <- function(id, title, description, conforms_to = NULL, ...) {
1112
id = id, ...
1213
)
1314
}
14-
15+
#' @rdname api_handling
1516
#' @export
1617
api_landing_page.stac <- function(api, req, res, ...) {
1718
doc <- list(
@@ -24,22 +25,22 @@ api_landing_page.stac <- function(api, req, res, ...) {
2425
doc <- links_landing_page(doc, api, req, res)
2526
doc
2627
}
27-
28+
#' @rdname api_handling
2829
#' @export
2930
api_conformance.stac <- function(api, req, res, ...) {
3031
NextMethod("api_conformance", api)
3132
}
32-
33+
#' @rdname api_handling
3334
#' @export
3435
api_collections.stac <- function(api, req, res, ...) {
3536
NextMethod("api_collections", api)
3637
}
37-
38+
#' @rdname api_handling
3839
#' @export
3940
api_collection.stac <- function(api, req, res, collection_id, ...) {
4041
NextMethod("api_collection", api)
4142
}
42-
43+
#' @rdname api_handling
4344
#' @export
4445
api_items.stac <- function(api,
4546
req,
@@ -51,12 +52,12 @@ api_items.stac <- function(api,
5152
page, ...) {
5253
NextMethod("api_items", api)
5354
}
54-
55+
#' @rdname api_handling
5556
#' @export
5657
api_item.stac <- function(api, req, res, collection_id, item_id, ...) {
5758
NextMethod("api_item", api)
5859
}
59-
60+
#' @rdname api_handling
6061
#' @export
6162
api_search.stac <- function(api,
6263
req,

R/api.R

+51-57
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,45 @@
88
#'
99
#' Users should parse and validate parameters such as `collection_id`,
1010
#' `bbox`, and `datetime` before calling these functions.
11-
#' `openstac` provides functions like `parse_json()`, `parse_datetime()`,
12-
#' `parse_dbl()`, `parse_int()`, and `parse_str()` to facilitate this process.
11+
#' `openstac` provides functions like `parse_geojson()`, `parse_datetime()`,
12+
#' `parse_dbl()`, `parse_int()`, and `parse_str()` to facilitate this
13+
#' process.
1314
#'
1415
#' \itemize{
1516
#'
16-
#' \item `api_landing_page`: handles the STAC `/` endpoint
17+
#' \item `create_ogcapi`: Creates an API object for OGC API Features.
1718
#'
18-
#' \item `api_conformance`: handles the STAC `/conformance` endpoint
19+
#' \item `create_stac`: Creates an API object for STAC.
1920
#'
20-
#' \item `api_collections`: handles the STAC `/collections` endpoint
21+
#' \item `api_landing_page`: Handles the STAC `/` endpoint.
2122
#'
22-
#' \item `api_collection`: handles the STAC
23-
#' `/collection/\{collection_id\}` endpoint
23+
#' \item `api_conformance`: Handles the STAC `/conformance` endpoint.
2424
#'
25-
#' \item `api_items`: handles the STAC
26-
#' `/collection/\{collection_id\}/items` endpoint
25+
#' \item `api_collections`: Handles the STAC `/collections` endpoint.
2726
#'
28-
#' \item `api_item`: handles the STAC
29-
#' `/collection/\{collection_id\}/item/\{item_id\}` endpoint
27+
#' \item `api_collection`: Handles the STAC
28+
#' `/collection/{collection_id}` endpoint.
3029
#'
31-
#' \item `api_search`: handles the STAC `/search` endpoint
30+
#' \item `api_items`: Handles the STAC
31+
#' `/collection/{collection_id}/items` endpoint.
32+
#'
33+
#' \item `api_item`: Handles the STAC
34+
#' `/collection/{collection_id}/item/{item_id}` endpoint.
35+
#'
36+
#' \item `api_search`: Handles the STAC `/search` endpoint.
3237
#'
3338
#' }
3439
#'
40+
#' @param id A character string specifying the id of the API.
41+
#'
42+
#' @param title A character string specifying the title of the API.
43+
#'
44+
#' @param description A character string describing the API.
45+
#'
46+
#' @param conforms_to A character vector specifying the conformance
47+
#' standards adhered to by the API. This parameter can be NULL or
48+
#' contain additional conformance standards to add to the defaults.
49+
#'
3550
#' @param api An object representing the API. This object is typically
3651
#' created using either the `create_stac` or `create_ogcapi`
3752
#'
@@ -45,12 +60,15 @@
4560
#' @param collection_id The identifier of the collection. This parameter
4661
#' specifies which collection the request is targeting.
4762
#'
63+
#' @param item_id The identifier of the item within the specified collection.
64+
#' This parameter specifies which item the request is targeting.
65+
#'
4866
#' @param limit The maximum number of items to return. If not specified,
4967
#' the default value is used.
5068
#'
5169
#' @param bbox The bounding box for spatial filtering, specified as a
5270
#' numeric vector of four coordinates
53-
#' (`minlon`, `minlat`, `maxlon`, `maxlat`). Use `parse_dbl()` to
71+
#' (`long_min`, `lat_min`, `long_max`, `lat_max`). Use `parse_dbl()` to
5472
#' convert comma-separated string to numeric vector.
5573
#'
5674
#' @param datetime The temporal filter for items. It must be specified
@@ -59,24 +77,38 @@
5977
#' string to this object.
6078
#'
6179
#' @param intersects The spatial filter for items, specified as a GeoJSON
62-
#' geometry object representing the area of interest. Use `parse_json()`
80+
#' geometry object representing the area of interest. Use `parse_geojson()`
6381
#' function to convert strings of GeoJSON geometries into an equivalent
6482
#' `list()` object.
6583
#'
6684
#' @param ids A list of item identifiers to filter the search results.
67-
#' Use `parse_str()` to convert a comma-separated string to character
68-
#' vector.
69-
#' @param collections A list of collection identifiers to filter the search results.
85+
#' Use `parse_str()` to convert a comma-separated string to a
86+
#' character vector
87+
#'
88+
#' @param collections A list of collection identifiers to filter the
89+
#' search results. Use `parse_str()` to convert a comma-separated
90+
#' string to a character vector.
91+
#'
7092
#' @param page The page number of the results when paginating.
93+
#'
7194
#' @param ... Additional arguments to be passed to the method-specific
7295
#' functions.
7396
#'
74-
#' @seealso \code{\link{create_stac}}, \code{\link{create_ogcapi}}
75-
#' Functions for creating STAC and OGC API objects, respectively.
97+
#' @return For API creation functions, returns a api object. For API
98+
#' handling functions, returns the document to return as response.
99+
#'
100+
#' @seealso
101+
#' [create_stac()], [create_ogcapi()]: Functions for creating STAC and
102+
#' OGC API objects, respectively.
103+
#'
104+
#' [parse_int()], [parse_dbl()], [parse_str()], [parse_datetime()],
105+
#' [parse_geojson()]: Functions to convert HTTP input strings
106+
#' into R data types.
76107
#'
77108
#' @references
78109
#' For more information about the STAC specification,
79110
#' see: \url{https://stacspec.org/}
111+
#'
80112
#' For more information about the OGC API specification,
81113
#' see: \url{http://www.opengis.net/doc/IS/ogcapi-features-1/1.0}
82114
#'
@@ -160,41 +192,3 @@ api_search <- function(api,
160192
page, ...) {
161193
UseMethod("api_search", api)
162194
}
163-
164-
# Based on https://github.com/rstudio/plumber/issues/66#issuecomment-418660334
165-
# HTTP CORS support
166-
#' @rdname api_helpers
167-
#' @export
168-
api_cors_handler <- function(req, res, origin = "*", methods = "*") {
169-
res$setHeader("Access-Control-Allow-Origin", origin)
170-
if (req$REQUEST_METHOD != "OPTIONS") {
171-
plumber::forward()
172-
} else {
173-
res$setHeader("Access-Control-Allow-Methods", methods)
174-
res$setHeader("Access-Control-Allow-Headers",
175-
req$HTTP_ACCESS_CONTROL_REQUEST_HEADERS)
176-
res$status <- 200
177-
return(list())
178-
}
179-
}
180-
#' @rdname api_helpers
181-
#' @export
182-
api_error_handler <- function(req, res, err) {
183-
if (is.null(err$status)) err$status <- 500
184-
if (is.null(err$message)) err$message <- "Internal server error"
185-
res$status <- err$status
186-
list(code = err$status, message = paste("Error:", err$message))
187-
}
188-
#' @rdname api_helpers
189-
#' @export
190-
api_stop <- function(status, ...) {
191-
stop(errorCondition(paste0(...), status = status))
192-
}
193-
#' @rdname api_helpers
194-
#' @export
195-
api_stopifnot <- function(expr, status, ...) {
196-
message <- paste0(...)
197-
if (!nzchar(message))
198-
message <- paste(deparse(substitute(expr)), "is not TRUE")
199-
if (!expr) api_stop(status, message)
200-
}

0 commit comments

Comments
 (0)