Skip to content

Commit

Permalink
Style
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemahoney218 committed Feb 29, 2024
1 parent bcb2a6a commit 7d8c40e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 54 deletions.
1 change: 0 additions & 1 deletion R/get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ complex_download <- function(items,


make_composite_bands <- function(downloaded_bands, composite_function, p) {

if (is.null(composite_function)) {
return(
list(
Expand Down
3 changes: 0 additions & 3 deletions R/query_and_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ default_query_function <- function(bbox,
end_date,
limit,
...) {

if (!is.null(start_date)) {
datetime <- paste0(start_date, "/", end_date)
} else {
Expand Down Expand Up @@ -107,5 +106,3 @@ sign_planetary_computer <- function(items,
)
}
}


10 changes: 6 additions & 4 deletions R/stack_rasters.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ stack_rasters <- function(rasters,

tryCatch(
check_type_and_length(rasters = list()),
error = function(e) check_type_and_length(
rasters = character(),
call = rlang::caller_env(4)
)
error = function(e) {
check_type_and_length(
rasters = character(),
call = rlang::caller_env(4)
)
}
)

out_dir <- dirname(output_filename)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `m
test_that("get_*_data works with mapply() (#17)", {
skip_on_cran()
skip_if_offline()
san_antonio = sf::st_point(c(-98.491142, 29.424349))
san_antonio = sf::st_sfc(san_antonio, crs = "EPSG:4326")
san_antonio = sf::st_buffer(sf::st_transform(san_antonio, "EPSG:3081"), 100)
san_antonio <- sf::st_point(c(-98.491142, 29.424349))
san_antonio <- sf::st_sfc(san_antonio, crs = "EPSG:4326")
san_antonio <- sf::st_buffer(sf::st_transform(san_antonio, "EPSG:3081"), 100)

expect_no_error(
mapply(
Expand Down
72 changes: 36 additions & 36 deletions vignettes/articles/Downloading-data-from-STAC-APIs-using-rsi.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Because the `RSTACQuery` object is a representation of a future query, we can us

```{r}
stac_source |>
rstac::collections() |>
rstac::collections() |>
rstac::get_request()
```

Expand All @@ -77,7 +77,7 @@ rstac::stac_search(
collections = "usgs-lcmap-conus-v13",
datetime = "2021-01-01/2021-12-31",
limit = 999
) |>
) |>
rstac::get_request()
```

Expand All @@ -90,7 +90,7 @@ rstac::stac_search(
q = stac_source,
collections = "landsat-c2-l2",
datetime = "2021-01-01/2021-12-31"
) |>
) |>
rstac::get_request()
```

Expand Down Expand Up @@ -132,14 +132,14 @@ ashe_lcpri
And a few seconds later, we've got our data! The output from `get_stac_data()` is a path to this data, saved as a raster somewhere on our computer; that means we can pass this object to `terra::rast()` and `terra::plot()` to load this data into R and visualize it:

```{r}
terra::rast(ashe_lcpri) |>
terra::rast(ashe_lcpri) |>
terra::plot()
```

If we draw our Ashe county polygon on top of this raster, we can see that `get_stac_data()` has only downloaded the portion of this asset that falls within the bounding box we provided:

```{r}
terra::rast(ashe_lcpri) |>
terra::rast(ashe_lcpri) |>
terra::plot()
terra::plot(terra::vect(ashe), add = TRUE, col = "red")
```
Expand All @@ -155,8 +155,8 @@ get_stac_data(
stac_source = "https://planetarycomputer.microsoft.com/api/stac/v1",
collection = "usgs-lcmap-conus-v13",
output_filename = tempfile(fileext = ".tif")
) |>
terra::rast() |>
) |>
terra::rast() |>
terra::plot()
```

Expand All @@ -175,8 +175,8 @@ get_stac_data(
stac_source = "https://planetarycomputer.microsoft.com/api/stac/v1",
collection = "usgs-lcmap-conus-v13",
output_filename = tempfile(fileext = ".tif")
) |>
terra::rast() |>
) |>
terra::rast() |>
terra::plot()
```

Expand All @@ -191,7 +191,7 @@ But that said, we'd probably want to do a number of other things, too. For insta
For that reason, rsi provides a function, `get_landsat_imagery()`, which wraps `get_stac_data()` and sets a number of arguments to helpful default values. We use the following block of code to see exactly what defaults have been changed:

```{r}
# Get the arguments, and default values, from
# Get the arguments, and default values, from
# get_stac_data and get_landsat_imagery
gsd_formals <- formals(get_stac_data)
gli_formals <- formals(get_landsat_imagery)
Expand Down Expand Up @@ -233,8 +233,8 @@ get_landsat_imagery(
start_date = "2021-06-01",
end_date = "2021-06-30",
output_filename = tempfile(fileext = ".tif")
) |>
terra::rast() |>
) |>
terra::rast() |>
terra::plot()
```

Expand Down Expand Up @@ -263,9 +263,9 @@ get_landsat_imagery(
output_filename = tempfile(fileext = ".tif"),
composite_function = NULL,
mask_function = NULL # otherwise half of these images are blank
) |>
lapply(terra::rast) |>
lapply(terra::plot) |>
) |>
lapply(terra::rast) |>
lapply(terra::plot) |>
invisible()
```

Expand All @@ -284,18 +284,18 @@ By the end of that tutorial, we had written relatively complex filters using rst
We could write a filter that looks something like this:

```{r}
geometry <- ashe |>
sf::st_transform(4326) |>
sf::st_bbox() |>
geometry <- ashe |>
sf::st_transform(4326) |>
sf::st_bbox() |>
rstac::cql2_bbox_as_geojson()
datetime <- rstac::cql2_interval("2021-06-01", "2021-06-30")
rstac::stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
rstac::ext_filter(
collection == "landsat-c2-l2" &&
t_intersects(datetime, {{datetime}}) &&
s_intersects(geometry, {{geometry}}) &&
platform == "landsat-8" &&
t_intersects(datetime, {{ datetime }}) &&
s_intersects(geometry, {{ geometry }}) &&
platform == "landsat-8" &&
`eo:cloud_cover` < 50
) |>
rstac::post_request()
Expand All @@ -314,25 +314,25 @@ This is a relatively straightforward function -- it composes a datetime from the
This works for most STAC APIs, but if we want to perform a more complicated query we can provide our own custom query function in its place. For instance, to perform the same CQL2 query as before, we can effectively copy and paste our code from above into a new query function and pass that to `get_landsat_imagery()`:

```{r}
custom_query_function <- function(bbox,
stac_source,
collection,
start_date,
end_date,
limit,
custom_query_function <- function(bbox,
stac_source,
collection,
start_date,
end_date,
limit,
...) {
# `bbox` is guaranteed to be in 4326 already
geometry <- rstac::cql2_bbox_as_geojson(bbox)
# `start_date` and `end_date` will be processed
geometry <- rstac::cql2_bbox_as_geojson(bbox)
# `start_date` and `end_date` will be processed
# and so hopefully will be in RFC-3339 formats
datetime <- rstac::cql2_interval(start_date, end_date)
request <- rstac::ext_filter(
rstac::stac(stac_source),
collection == {{collection}} && # I could have left this hard-coded!
t_intersects(datetime, {{datetime}}) &&
s_intersects(geometry, {{geometry}}) &&
platform == "landsat-8" &&
collection == {{ collection }} && # I could have left this hard-coded!
t_intersects(datetime, {{ datetime }}) &&
s_intersects(geometry, {{ geometry }}) &&
platform == "landsat-8" &&
`eo:cloud_cover` < 50
)
rstac::items_fetch(rstac::post_request(request))
Expand All @@ -344,8 +344,8 @@ get_landsat_imagery(
end_date = "2021-06-30",
output_filename = tempfile(fileext = ".tif"),
query_function = custom_query_function
) |>
terra::rast() |>
) |>
terra::rast() |>
terra::plot()
```

Expand Down
14 changes: 7 additions & 7 deletions vignettes/rsi.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ In order to download data from a STAC API, we're going to need to specify both a

```{r}
our_aoi <- sf::st_bbox(
c(xmin = 200000, ymin = 900000, xmax = 200100, ymax = 900100),
c(xmin = 200000, ymin = 900000, xmax = 200100, ymax = 900100),
crs = 26986
)
our_aoi <- sf::st_as_sf(sf::st_as_sfc(our_aoi))
Expand All @@ -52,7 +52,7 @@ If we wanted to, we could use `future::plan()` here to specify a parallelization
By default, `get_landsat_imagery()` will download a composite of all bands available in Landsat 8 and 9 imagery for our timeframe:

```{r}
terra::rast(our_imagery) |>
terra::rast(our_imagery) |>
terra::plot()
```

Expand All @@ -62,7 +62,7 @@ We're also able to use rsi to download other data sets -- for instance, we could

```{r}
our_dem <- get_dem(our_aoi)
terra::rast(our_dem) |>
terra::rast(our_dem) |>
terra::plot()
```

Expand All @@ -71,7 +71,7 @@ Under the hood, both of these functions (and their friends, `get_sentinel2_image
In addition to these STAC-focused data-downloading functions, rsi also has an interface to the [Awesome Spectral Indices](https://github.com/awesome-spectral-indices/awesome-spectral-indices) project via the `spectral_indices()` function:

```{r}
spectral_indices() |>
spectral_indices() |>
head()
```

Expand All @@ -80,7 +80,7 @@ This function attempts to grab the newest version of the spectral indices JSON f
There are also functions in rsi to sort through the ASI list of indices. For instance, the `filter_platforms()` function can be used to, well, filter the list to only indices that can be calculated from a given platform. For instance, to filter to only indices that can be calculated using data from Landsat's Operational Land Imager:

```{r}
filter_platforms(platforms = "Landsat-OLI") |>
filter_platforms(platforms = "Landsat-OLI") |>
head()
```

Expand All @@ -100,7 +100,7 @@ our_indices <- calculate_indices(
filter_bands(bands = names(terra::rast(our_imagery))),
"our_indices.tif"
)
terra::rast(our_indices) |>
terra::rast(our_indices) |>
terra::plot()
```

Expand Down Expand Up @@ -128,6 +128,6 @@ combined_layers <- stack_rasters(
tempfile(fileext = ".vrt")
)
terra::rast(combined_layers) |>
terra::rast(combined_layers) |>
terra::plot()
```

0 comments on commit 7d8c40e

Please sign in to comment.