-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
280 lines (184 loc) · 11.4 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[![R-CMD-check](https://github.com/hypertidy/whatarelief/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/hypertidy/whatarelief/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
# whatarelief
The goal of whatarelief is to obtain *raster* data, for exactly the map that you want. No need to open, crop, resize, aggregate, call out to command line or faff around with files or format.
There are raster sources, and whatarelief knows about online elevation and imagery so you don't have to, just specify what what you want. Any extent, any projection.
```{r want}
library(whatarelief)
## in global Mercator projection Australia's box extent is 7100km wide and 4500km high and its southwest
## point is 11700km east and 5500km south of the prime meridian and equator
p0 <- c(11700000, -5500000)
ex <- rep(p0, each = 2L) + c(0, 7100000, 0, 4500000)
## 1024 is a enough to make a picture
dm <- c(1024, 1024 * 4.5/7.1) ## set the aspect ratio so our dimensions match our extent proportions
img <- imagery(extent = ex, dimension = dm, projection = "EPSG:3857")
cst <- coastline(extent = ex, dimension = dm, projection = "EPSG:3857")
ximage::ximage(img, extent = ex, asp = 1)
lines(cst, col = "saddlebrown")
```
Please note that the hard part up there is get *six numbers*, these are *extent: xmin,xmax,ymin,ymax* and *dimension: ncol,nrow*. Normally we have these at hand, for example the state.center data set in R, and our device size:
```{r quakes}
sc <- datasets::state.center
ex <- c(range(sc$x), range(sc$y))
dm <- dev.size("px")
img <- imagery(extent = ex, dimension = dm, projection = "OGC:CRS84", resample = "bilinear")
ximage::ximage(img, extent = ex, asp = 1/cos(mean(ex[3:4]) * pi/180))
points(sc$x, sc$y, col = "firebrick", pch = 19)
```
We have this example upfront because it's the most important part, and we don't have helpers yet for choosing the right dimension. You are free to do what you want.
There is a world full of online data sources for bio/physical variables, elevation,
and imagery, and we of course use the GDAL warper app lib as the most general and convenient engine for working with raster sources. There is no grand catalogue of these sources or how to use them, so we left that for later, grabbed some sources and made some helpers.
Here you'll find functions with clear names about what they provide.
* [elevation()](https://hypertidy.github.io/whatarelief/reference/elevation.html) obtain GEBCO+-SRTM
* [imagery()](https://hypertidy.github.io/whatarelief/reference/imagery.html) obtain Virtual Earth imagery
* [streetmap()](https://hypertidy.github.io/whatarelief/reference/imagery.html) obtain OpenStreetmap imagery
* [satbox()](https://hypertidy.github.io/whatarelief/reference/imagery.html) obtain Mapbox Satellite/Aerial imagery
* [coastline()](https://hypertidy.github.io/whatarelief/reference/coastline.html) obtain coastline from GEBCO+-SRTM
They aren't really specific to their name, `elevation()` for example
can look up any raster data source (file, url, anything GDAL understands), just use the 'source' argument. This also means we don't have to use the online sources, just provide whatever you want. Generally, `elevation()` will get a matrix (a single band) of numeric raster data, and `imagery()` and `streetmap()` will get a matrix of colour data (usually from 3 or 4 bands).
`elevation()` defaults to global elevation data from GEBCO, and will include higher resolution SRTM for small regions.
`imagery()` and `streetmap()` default to satellite+aerial imagery, and drawings of street layers respectively.
`coastline()` generates coordinates from a elevation source at level = 0, to give a "coast line" of sorts.
All of these functions have arguments *extent*, *dimension*, *projection* - the three key components that make up a raster. Specify these three components to get what you want. Every single spatial object you use has an extent (xmin,xmax,ymin,ymax) and a projection (crs), you can pick anything for dimension (but 1024x1024 is enough to make a picture, for example).
We have been careful to align to the *raster orientation*, which in R is the
`rasterImage()` convention, this matches the order in which the data are
returned but still requires care because it's confusing.
We don't like that `rasterImage()` and its second cousin `image()` have a
different set of incomplete features with these different orientations, so we
use the helper [ximage()](httsp://github.com/hypertidy/whatarelief.git) so we
can work easily at a high level for visualization.
## Installation
You can install the development version of whatarelief from [Github](https://github.com/hypertidy/whatarelief) with:
``` r
remotes::install_github("hypertidy/whatarelief")
```
## Get elevation data
Whole planet by default, specify an extent to hone this, use 'dimension' and 'projection' for more custom options.
```{r elevation}
library(whatarelief)
im <- elevation() ## orientation is wrong for image() but correct for rasterImage()
image(t(im[nrow(im):1, ]))
```
We don't want this special handling, so use the [ximage](https://github.com/hypertidy/ximage) package functions `ximage()` and `xcontour()` 🚀.
```{r ximage}
library(ximage)
ximage(im, extent = c(-180, 180, -90, 90))
ex <- c(120, 160, 30, 50)
x0 <- elevation(extent = ex)
ximage(x0, col = hcl.colors(24), extent = ex)
xcontour(x0, add = TRUE, levels = 10, extent = ex)
ximage(elevation(extent = c(120, 160, 30, 50), dimension = dev.size("px")))
ximage(elevation(extent = c(120, 160, -50, -20), dimension = c(60, 85), resample = "near"))
ex <- c(-1, 1, -1, 1) * 5e6
ximage(elevation(extent = ex, projection = "+proj=lcc +lon_0=-85 +lat_0=-42 +lat_1=0 +lat_2=-30"), extent = ex)
```
Can use a raster object.
```{r rast}
elevation(terra::rast())
template <- terra::rast(terra::ext(c(-1, 1, -1, 1) * 5e6), crs = "+proj=lcc +lon_0=-85 +lat_0=-42 +lat_1=0 +lat_2=-30", ncols = 1024, nrows = 1024)
r <- elevation(template)
r
terra::plot(r)
```
Works down to quite high resolution.
```{r resolution}
pt <- c(151.2093, -33.8688)
ex <- c(-1, 1, -1, 1) * 0.01 + rep(pt, each = 2L)
elev <- elevation(extent = ex)
ximage(elev, zlim = c(0, max(elev)), extent = ex)
```
Similar example, but a bit more context so we can easily see that it's "correct", by respatializing!
```{r correct}
ex <- c(-1, 1, -1, 1) * 0.1 + rep(pt, each = 2L)
elev <- elevation(extent = ex)
ximage(elev, zlim = c(0, max(elev)), extent = ex)
xcontour(elev, extent = ex, levels = 5, add = TRUE)
```
We can "re-spatialize", but no special functionality is added we just use the available tools.
```{r spatial}
library(terra)
## note the data come out in rasterImage order, so the columns/rows are switched here
template <- terra::rast(terra::ext(ex), ncols = dim(elev)[2L], nrows = dim(elev)[1L], crs = "OGC:CRS84")
plot(setValues(template, elev))
plot(sf::st_cast(ozmaps::abs_ced, "MULTILINESTRING"), add = TRUE, col = "black")
```
Note that all the information we need is present, we provide the extent, and the
result has a dimension. (Longitude/latitude is assumed if sensible, else there's
a warning - but the code will run what you ask of it). Provide all the
information extent, dimension, projection to get a controlled result, or use a
raster or terra object to store these.
With a Mapbox API key set in 'MAPBOX_API_KEY' you have access to `satbox()`.
```{r satbox}
ex <- c(-1, 1, -1, 1) * 0.1 + rep(pt, each = 2L)
sat <- satbox(extent = ex, dimension = dev.size("px"))
ximage(sat, extent = ex)
```
## Custom sources
We can provide our own sources of elevation.
Use the 'source' argument to `elevation()`, you can input multiple sources so
that a higher resolution one has a fallback to a lower resolution one, note that
by default we have "GEBCO 2022", then "Copernicus GLO-30" in the sources. It
doesn't matter what projection or extent these have, but usually a higher
resolution one should be listed after lower.
For example, this string provides the Amazon compute S3 elevation tiles, an
'XYZ' image server that has geotiff elements but is not a georeferenced online
service (hence this small bit of XML to wrap it up).
```{r aws-custom}
aws <- "<GDAL_WMS><Service name=\"TMS\"><ServerUrl>https://s3.amazonaws.com/elevation-tiles-prod/geotiff/${z}/${x}/${y}.tif</ServerUrl></Service><DataWindow><UpperLeftX>-20037508.34</UpperLeftX><UpperLeftY>20037508.34</UpperLeftY><LowerRightX>20037508.34</LowerRightX><LowerRightY>-20037508.34</LowerRightY><TileLevel>14</TileLevel><TileCountX>1</TileCountX><TileCountY>1</TileCountY><YOrigin>top</YOrigin></DataWindow><Projection>EPSG:3857</Projection><BlockSizeX>512</BlockSizeX><BlockSizeY>512</BlockSizeY><BandsCount>1</BandsCount><DataType>Int16</DataType><ZeroBlockHttpCodes>403,404</ZeroBlockHttpCodes><DataValues><NoData>-32768</NoData></DataValues><Cache/></GDAL_WMS>"
```
Using this, we can obtain a global summary of the available data. Sadly, these data are not quite global.
```{r aws-global}
m <- elevation(source = aws)
## we can use the old ways, just get the orientation right :)
image(seq(-180, 180, length.out = ncol(m)), seq(-90, 90, length.out = nrow(m)), t(m[nrow(m):1, ]), asp = 1)
maps::map(add = TRUE)
abline(h = c(-90, 90), lwd = 2)
ex <- c(-1, 1, -1, 1) * 1e7
ximage(elevation(extent = ex, projection = "+proj=laea +lat_0=-90", source = aws), asp = 1, extent = ex)
```
But, we can fall back to our GEBCO 2021 source to fill the gap even with a custom source (note we can provide any number of sources, in any mix of extents and projections and resolutions).
```{r fallback}
gebco <- "/vsicurl/https://public.services.aad.gov.au/datasets/science/GEBCO_2021_GEOTIFF/GEBCO_2021.tif"
m <- elevation(source = c(gebco, aws))
ximage(m, extent = c(-180, 180, -90, 90), asp = 1)
maps::map(add = TRUE)
abline(h = c(-90, 90), lwd = 2)
ximage(elevation(extent = c(-1, 1, -1, 1) * 1e7, projection = "+proj=laea +lat_0=-90", source = c(gebco, aws)), extent = c(-1, 1, -1, 1) * 1e7)
```
## Get coastlines
```{r coastline}
cst <- coastline(extent = c(-180, 180, -90, 0))
plot(cst, type = "b", pch = ".")
```
## Properties of the sources in use
See vignette [elevation-sources](https://hypertidy.github.io/whatarelief/articles/elevation-sources.html).
Note that, we could use any raster data of any kind here as custom 'source's ... (we're figuring out how to frame this package in general terms, that aren't too "spatial").
## Experimental features
We are working out some ways of accessing data used by the raadtools project.
Please don't rely on them, not working ATM (very WIP in our raadtools project too).
```{r nsidc, eval=FALSE}
library(whatarelief)
files <- raad_source("nsidc_25km_seaice")
names(files)
idx <- which.max(files$date)
mat <- elevation(source = unlist(files[idx, c("north_vrt_dsn", "south_vrt_dsn")]))
brks <- quantile(mat[mat <= 250 & mat > 0], seq(0, 1, length.out = 16))
ximage(mat, col = grey.colors(length(brks) - 1), breaks = brks, main = files$date[idx])
```
## Code of Conduct
Please note that the whatarelief project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.