---
title: "Testing - Aux layers"
author: "Martijn Tennekes"
date: "`r Sys.Date()`"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 9)
```

# Instructions

Open the tmap repository as RStudio project.
Set Knit Directory (via drop-down menu of the Knit button in RStudio) to Project Directory.

```{r echo=FALSE,message=FALSE,fig.height = 2}
library(devtools)
load_all()

library(stars)
library(terra)

data(land)

landsat_stars = stars::read_stars(system.file("raster/landsat.tif", package = "spDataLarge"))
landsat_terra = terra::rast(system.file("raster/landsat.tif", package = "spDataLarge"))

tif_stars = system.file("tif/L7_ETMs.tif", package = "stars") %>% read_stars()
tif_terra = system.file("tif/L7_ETMs.tif", package = "stars") %>% rast()

prec = stars::read_ncdf(system.file("nc/test_stageiv_xyt.nc", package = "stars"), curvilinear = c("lon", "lat"), ignore_bounds = TRUE)
nc = sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"), "nc.gpkg") %>%
	sf::st_transform(sf::st_crs(prec))
prec_nc = aggregate(prec, by = nc, FUN = max)

weather = stars::read_ncdf(system.file('nc/bcsd_obs_1999.nc', package = 'stars'))
weather1 = stars::st_set_dimensions(merge(weather), names = c('longitude','latitude','time','attributes'))
weather2 = split(weather1, 'time')


tmapV = ifelse(packageVersion("tmap") >= "3.4", "4", "3")
txt = function(x) print(grid::grid.text(x, gp = gpar(cex = 3)))
v3 = function(e) {
	if (tmapV == "3") {
		print(e)
	} else {
		txt("Only for tmap 3")
	}
	invisible(NULL)
}
v4 = function(e) {
	if (tmapV == "4") {
		print(e)
	} else {
		txt("Only for tmap 4")
	}
	invisible(NULL)
}
txt(paste("Loaded tmap version", tmapV))
```

```{r}
tm_shape(land) +
	tm_raster()
```

```{r}
tm_shape(landsat_stars) +
	tm_raster()
```



```{r}
tm_shape(tif_stars) +
	tm_raster()
```


```{r}
tm_shape(prec) +
	tm_raster()
```

```{r}
tmap_options(max.raster = 50000)
tm_shape(prec) +
	tm_raster()
```


```{r}
tm_shape(prec) +
	tm_raster(col.scale = tm_scale_intervals(style = "kmeans"))
```

```{r}
tm_shape(prec_nc) +
	tm_polygons("Total_precipitation_surface_1_Hour_Accumulation")
```



```{r}
tm_shape(weather) +
	tm_raster()
```



```{r}
tm_shape(weather1) +
	tm_raster()
```



```{r}
tm_shape(weather2) +
	tm_raster()
```

```{r}
#todo s1_rearrage_faetgs # 83 86
tm_shape(weather2) +
	tm_raster() +
	tm_facets_grid(rows = "attributes")
```


```{r}
# testing non-made names
tm_shape(weather2) +
	tm_raster(c("1999-02-28", "1999-03-31", "1999-04-30"))
```

```{r}
# testing made names
tm_shape(weather2) +
	tm_raster(c("X1999.02.28", "X1999.03.31", "X1999.04.30"))
```



```{r}
tm_shape(landsat_stars) +
	tm_raster()
```

```{r}
tmap_options(max.raster = 40)
tm_shape(landsat_stars) +
	tm_raster()
```


```{r}
tm_shape(landsat_terra) +
	tm_raster()
```

