SymbolixAU / mapdeck
1
mapdeckTerrainDependency <- function() {
2 0
	list(
3 0
		createHtmlDependency(
4 0
			name = "terrain",
5 0
			version = "1.0.0",
6 0
			src = system.file("htmlwidgets/lib/terrain", package = "mapdeck"),
7 0
			script = c("terrain.js"),
8 0
			all_files = FALSE
9
		)
10
	)
11
}
12

13

14
#' Add terrain
15
#'
16
#' Adds mesh surfaces from height map images
17
#'
18
#' @inheritParams add_arc
19
#' @param elevation_data Image URL that encodes height data. When \code{elevation_data}
20
#' is a URL template, i.e. a string containing '{x}' and '{y}', it loads terrain tiles on demand
21
#' and renders a mesh for each tile. If \code{elevation_data} is an absolute URL, as ingle mesh is used,
22
#' and the \code{bounds} argument is required to position it into the world space.
23
#' @param texture Image URL to use as the texture
24
#' @param elevation_decoder Four value used to convert a pixel to elevation in metres. The
25
#' values correspond to rScale, gScale, bScale, offset. See details
26
#' @param bounds Four values (\code{ c(left, bottom, right, top) }. bounds of the
27
#' image to fit in x,y coordinates into. \code{left} and \code{right} referes to the world
28
#' longitude/x at the corresponding side of the image. \code{top} and \code{bottom} refers to
29
#' the world latitude/y at the corresponding side of the image. Must be supplied when using
30
#' non-tiled \code{elevation_data}
31
#' @param max_error Martini error tolerance in metres, smaller number results in more detailed mesh.
32
#'
33
#' @details
34
#'
35
#' The \code{elevation_decoder} contains four values representing
36
#' \itemize{
37
#'   \item{rScale - Multiplier of the red channel}
38
#'   \item{gScale - Multiplier of the green channel}
39
#'   \item{bScale - Multiplier of the blue channel}
40
#'   \item{offset - translation of the sum}
41
#' }
42
#'
43
#' Each colour channel is a number between [0, 255].
44
#'
45
#'
46
#' @examples
47
#' \donttest{
48
#'
49
#' set_token( "MAPBOX_TOKEN" )
50
#' ## Digital elevation model from https://www.usgs.gov/
51
#' elevation <- 'https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/terrain.png'
52
#' texture <- 'https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/terrain-mask.png'
53
#' bounds <- c(-122.5233, 37.6493, -122.3566, 37.8159)
54
#'
55
#' mapdeck() %>%
56
#'   add_terrain(
57
#'     , elevation_data = elevation
58
#'     , elevation_decoder = c(1,0,0,0)
59
#'     , texture = texture
60
#'     , bounds = bounds
61
#'     , max_error = 1
62
#'   )
63
#'
64
#' }
65
#'
66
#' @export
67
add_terrain <- function(
68
	map,
69
	layer_id = NULL,
70
	elevation_data,
71
	texture = NULL,
72
	elevation_decoder = c(1,0,0,0),
73
	bounds = NULL,
74
	max_error = 4,
75
	update_view = TRUE,
76
	focus_layer = FALSE
77
	) {
78

79 0
	experimental_layer("terrain")
80

81 0
	bbox <- init_bbox()
82 0
	if( !is.null( bounds ) ) {
83 0
		bbox[[1]] <- bounds[1:2]
84 0
		bbox[[2]] <- bounds[3:4]
85
	}
86

87 0
	map <- addDependency( map, mapdeckTerrainDependency() )
88 0
	layer_id <- layerId( layer_id, "terrain" )
89

90 0
	invoke_method(
91 0
		map, "add_terrain", map_type( map ), layer_id, elevation_data, texture, elevation_decoder,
92 0
		max_error, update_view, bounds, bbox, focus_layer
93
	)
94
}
95

96

97
#' @rdname clear
98
#' @export
99
clear_terrain <- function( map, layer_id = NULL) {
100 0
	layer_id <- layerId(layer_id, "terrain")
101 0
	invoke_method(map, "md_layer_clear", layer_id )
102
}
103

104

Read our documentation on viewing source code .

Loading