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

13

14
#' Add Path
15
#'
16
#' The Path Layer takes in lists of coordinate points and renders them as
17
#' extruded lines with mitering.
18
#'
19
#' @inheritParams add_polygon
20
#' @param stroke_width width of the stroke in meters. Default 1.
21
#' @param dash_size size of each dash, relative to the width of the stroke
22
#' @param dash_gap size of the gap between dashes, relative to the width of the stroke
23
#' @param offset The offset to draw each path with, relative to the width of the path.
24
#' Negative offset is to the left hand side, and positive offset is to the right hand side.
25
#' 0 extrudes the path so that it is centered at the specified coordinates.
26
#' @param billboard logical indicating if the path always faces the camera (TRUE) or
27
#' if it always faces up (FALSE)
28
#' @param width_units The units of the line width, one of 'meters', 'pixels'.
29
#' When zooming in and out, meter sizes scale with the base map, and pixel sizes
30
#' remain the same on screen.
31
#' @param width_scale The path width multiplier that multiplied to all paths.
32
#' @param width_min_pixels The minimum path width in pixels.
33
#' This can be used to prevent the path from getting too thin when zoomed out.
34
#' @param width_max_pixels The maximum path width in pixels.
35
#' his prop can be used to prevent the path from getting too thick when zoomed in.
36
#'
37
#' @inheritSection add_polygon data
38
#' @inheritSection add_arc legend
39
#' @inheritSection add_arc id
40
#'
41
#' @section transitions:
42
#'
43
#' The transitions argument lets you specify the time it will take for the shapes to transition
44
#' from one state to the next. Only works in an interactive environment (Shiny)
45
#' and on WebGL-2 supported browsers and hardware.
46
#'
47
#' The time is in milliseconds
48
#'
49
#' Available transitions for path
50
#'
51
#' list(
52
#' path = 0,
53
#' stroke_colour = 0,
54
#' stroke_width = 0
55
#' )
56
#'
57
#'
58
#' @examples
59
#' \donttest{
60
#'
61
#' ## You need a valid access token from Mapbox
62
#' key <- 'abc'
63
#' set_token( key )
64
#'
65
#' mapdeck(
66
#'   style = mapdeck_style("dark")
67
#'   , location = c(145, -37.8)
68
#'   , zoom = 10) %>%
69
#'   add_path(
70
#'     data = roads
71
#'     , stroke_colour = "RIGHT_LOC"
72
#'     , layer_id = "path_layer"
73
#'     , tooltip = "ROAD_NAME"
74
#'     , auto_highlight = TRUE
75
#'     , legend = T
76
#'   )
77
#' }
78
#'
79
#' @details
80
#'
81
#' \code{add_path} supports LINESTRING and MULTILINESTRING sf objects
82
#'
83
#' @export
84
add_path <- function(
85
	map,
86
	data = get_map_data(map),
87
	polyline = NULL,
88
	stroke_colour = NULL,
89
	stroke_width = NULL,
90
	stroke_opacity = NULL,
91
	dash_size = NULL,
92
	dash_gap = NULL,
93
	offset = NULL,
94
	width_units = c("meters","pixels"),
95
	width_min_pixels = NULL,
96
	width_max_pixels = NULL,
97
	width_scale = 1,
98
	tooltip = NULL,
99
	billboard = FALSE,
100
	layer_id = NULL,
101
	id = NULL,
102
	auto_highlight = FALSE,
103
	highlight_colour = "#AAFFFFFF",
104
	palette = "viridis",
105
	na_colour = "#808080FF",
106
	legend = FALSE,
107
	legend_options = NULL,
108
	legend_format = NULL,
109
	update_view = TRUE,
110
	focus_layer = FALSE,
111
	digits = 6,
112
	transitions = NULL,
113
	brush_radius = NULL
114
) {
115

116 1
	l <- list()
117 1
	l[["polyline"]] <- force( polyline )
118 1
	l[["stroke_colour"]] <- force( stroke_colour)
119 1
	l[["stroke_width"]] <- force( stroke_width )
120 1
	l[["stroke_opacity"]] <- resolve_opacity( stroke_opacity )
121 1
	l[["dash_size"]] <- force(dash_size)
122 1
	l[["dash_gap"]] <- force(dash_gap)
123 1
	l[["offset"]] <- force(offset)
124 1
	l[["tooltip"]] <- force(tooltip)
125 1
	l[["id"]] <- force(id)
126 1
	l[["na_colour"]] <- force(na_colour)
127

128 1
	l <- resolve_palette( l, palette )
129 1
	l <- resolve_legend( l, legend )
130 1
	l <- resolve_legend_options( l, legend_options )
131 1
	l <- resolve_data( data, l, c("LINESTRING") )
132

133 1
	bbox <- init_bbox()
134 1
	update_view <- force( update_view )
135 1
	focus_layer <- force( focus_layer )
136

137 1
	if ( !is.null(l[["data"]]) ) {
138 1
		data <- l[["data"]]
139 1
		l[["data"]] <- NULL
140
	}
141

142 1
	if( !is.null(l[["bbox"]] ) ) {
143 0
		bbox <- l[["bbox"]]
144 0
		l[["bbox"]] <- NULL
145
	}
146

147 1
	use_offset <- !is.null( offset )
148 1
	use_dash <- !is.null( dash_size ) | !is.null( dash_gap )
149

150 1
	layer_id <- layerId(layer_id, "path")
151 1
	checkHexAlpha( highlight_colour )
152

153 1
	map <- addDependency(map, mapdeckPathDependency())
154

155 1
	tp <- l[["data_type"]]
156 1
	l[["data_type"]] <- NULL
157

158 1
	if ( tp == "sf" ) {
159 0
		geometry_column <- c( "geometry" ) ## This is where we woudl also specify 'origin' or 'destination'
160 0
		shape <- rcpp_path_geojson( data, l, geometry_column, digits, "path" )
161 0
		jsfunc <- "add_path_geo"
162 1
	} else if ( tp == "sfencoded" ) {
163 1
		jsfunc <- "add_path_polyline"
164 1
		geometry_column <- "polyline"
165 1
		shape <- rcpp_path_polyline( data, l, geometry_column, "path" )
166
	}
167

168 1
	js_transitions <- resolve_transitions( transitions, "path" )
169 1
	if( inherits( legend, "json" ) ) {
170 0
		shape[["legend"]] <- legend
171
	} else {
172 1
		shape[["legend"]] <- resolve_legend_format( shape[["legend"]], legend_format )
173
	}
174

175 1
	invoke_method(
176 1
		map, jsfunc, map_type( map ), shape[["data"]], layer_id, auto_highlight,
177 1
		highlight_colour, shape[["legend"]], bbox, update_view, focus_layer,
178 1
		js_transitions, billboard, brush_radius, width_units, width_scale, width_min_pixels,
179 1
		width_max_pixels, use_offset, use_dash
180
		)
181
}
182

183

184
#' @rdname clear
185
#' @export
186
clear_path <- function( map, layer_id = NULL) {
187 1
	layer_id <- layerId(layer_id, "path")
188 1
	invoke_method(map, "md_layer_clear", map_type( map ), layer_id, "path" )
189
}
190

191

192

Read our documentation on viewing source code .

Loading