Skip to content

Commit

Permalink
stac query generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Apr 9, 2024
1 parent e324698 commit 654fa70
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sds
Title: Spatial Data Sources
Version: 0.0.1.9004
Version: 0.0.1.9005
Authors@R:
person("Michael", "Sumner", , "[email protected]", role = c("aut", "cre"))
Description: Functions for descriptions of spatial data sources that can be read
Expand All @@ -11,7 +11,7 @@ Description: Functions for descriptions of spatial data sources that can be read
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
URL: https://github.com/hypertidy/sds
BugReports: https://github.com/hypertidy/sds/issues
Imports:
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export(gebco22)
export(gebco23)
export(gebco23_bedrock)
export(ibcso)
export(list_address)
export(list_parcel)
export(mpc)
export(mursst)
export(mursst_time)
Expand All @@ -21,6 +23,7 @@ export(rema)
export(rema_v2)
export(sentinel2_wms)
export(srtm15)
export(stacit)
export(tas_dem)
export(usgs_seamless)
export(wms_ESA_worldcover_2020_tms)
Expand Down
60 changes: 60 additions & 0 deletions R/stacit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' Stacit query generator
#'
#' @param extent
#' @param date
#' @param collections
#' @param provider
#' @param asset
#' @param gdal_stacit
#'
#' @return
#' @export
#'
#' @examples
#' stacit(c(140, 145, -43, -40))
stacit <- function(extent, date = Sys.Date()- 1, collections = "sentinel-2-c1-l2a",
provider = c("https://earth-search.aws.element84.com/v1/search",
"https://planetarycomputer.microsoft.com/api/stac/v1/search"),
asset = c("visual"),
gdal_stacit = FALSE,
limit = 1000) {

provider <- provider[1L]
if (missing(extent)) stop("'extent' must be provided, a vector of 'c(xmin, xmax, ymin, ymax)' in longlat coords")

if (extent[2] > 180) {
bb <- c(paste0(c(extent[1], extent[3], 180, extent[4]), collapse = ","),
paste0(c(-180, extent[3], extent[2] - 360, extent[4]), collapse = ","))
} else {
bb <- paste0(extent[c(1, 3, 2, 4)], collapse = ",")
}


if (length(date) == 1L) {
if (nchar(date) == 7L && grepl("-", date)) {
date <- seq(as.Date(sprintf(c("%s-01"), date)), length.out = 2L, by = "1 month")
date[2] <- date[2] - 1
}
if (nchar(date) == 4L && !is.na(as.integer(date))) {
date <- sprintf(c("%s-01-01", "%s-12-31"), date)
}


date <- c(format(min(as.Date(date)), "%FT00:00:00Z"),
format(max(as.Date(date)), "%FT23:59:59Z"))
} else {
date <- range(as.POSIXct(date, tz = "UTC"))
}

date <- paste(date, collapse = "/")
collections <- paste(collections, collapse = ",")
asset <- paste(asset, collapse = ",")
base <- sprintf("%s?collections=%s&bbox=%s&datetime=%s", provider, collections, bb, date)

if (gdal_stacit) {
return(sprintf("STACIT:\"%s\":asset=%s", base, asset))
}

sprintf("%s&limit=%i", base, limit)

}
25 changes: 25 additions & 0 deletions man/list_address.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/list_parcel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/stacit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 654fa70

Please sign in to comment.