---
title: "get_data"
author: "Xiaohao Yang"
vignette: >
  %\VignetteIndexEntry{get data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Donwload multi-band data from Greenspace Seasonality Data Cube with specified area/point of inerest
Downloading data may take more than 5 minutes.

### By bounding box

```{r eval=FALSE}
gs <- greenSD::get_gsdc(bbox = c(-83.272828,42.343950,-83.218926,42.379719), year = 2022, mask = TRUE)
```

### By place name

```{r eval=FALSE}
gs <- greenSD::get_gsdc(place = 'Detroit', year = 2022)
```

### By coordinates (point)

```{r eval=FALSE}
gs <- greenSD::get_gsdc(location = c(-83.10215 42.38342), year = 2022)
```

### By UID and time range

```{r eval=FALSE}
# check UID 
greenSD::check_available_cities()
gs <- greenSD::get_gsdc(UID = 1825, year = 2022, time = c("03-01", "09-01"))
```

# Download ESA WorldCover 10m Annual Dataset

### Get NDVI data by place name 

```{r eval=FALSE}
ndvi <- greenSD::get_esa_wc(place = 'Detroit', datatype = "ndvi")
```

### Get land cover data from ESA WorldCover 10m dataset

```{r eval=FALSE}
lc <- greenSD::get_esa_wc(place = 'Detroit', datatype = "landcover")
```

# Retrieve Sentinel-2-l2a images and compute NDVI

```{r eval=FALSE}
ndvi <- greenSD::get_s2a_ndvi(bbox = c(-83.087174,42.333373,-83.042542,42.358748), 
                              datetime = c("2022-08-01", "2022-09-01"), 
                              cloud_cover = 5,
                              output_bands = NULL)
```

# Get the greenspace segmentation from map tiles

```{r eval=FALSE}
# from Esri.WorldImagery map tiles 
green <- greenSD::get_tile_green(bbox = c(-83.087174,42.333373,-83.042542,42.358748), 
                                 provider = "esri",
                                 zoom = 16)

# from Sentinel-2 cloudless mosaic tiles
greenspace2 <- greenSD::get_tile_green(bbox = c(-83.087174,42.333373,-83.042542,42.358748), 
                                      zoom = 17, 
                                      provider = "eox",
                                      year = 2022)
```

# Extract values from Greenspace Seasonality Data Cube with samples
You can extract seasonal greenspace values at multiple point locations 
within a city boundary.

```{r eval=FALSE}
boundary <- greenSD::check_urban_boundary(uid = 1825, plot = FALSE)
samples <- sf::st_sample(boundary, size = 50)
gs_samples <- greenSD::sample_values(samples, year = 2022)
```

# Visualize Seasonal Greenspace Dynamics as an Animated GIF
The `to_gif()` function converts a multi-band raster (e.g., greenspace bands 
across the growing season) into an animated GIF for quick visual exploration.

```{r eval=FALSE}
# Load example data (or use `gs` from previous step)
sample_data <- terra::rast(system.file("extdata", "detroit_gs.tif", package = "greenSD"))

# Generate GIF
gif <- greenSD::to_gif(
  r = sample_data,
  fps = 5,
  width = 600,
  height = 600,
  axes = FALSE,
  title_prefix = paste("greenspace - Day", 1:terra::nlyr(sample_data) * 10)
)

# Display in RStudio Viewer or save
print(gif)

# To save the GIF manually:
magick::image_write(gif, "greenspace_animation.gif")
```



