-
Notifications
You must be signed in to change notification settings - Fork 41
/
map_layer_sf.R
36 lines (32 loc) · 1.11 KB
/
map_layer_sf.R
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
#' Add sf
#'
#' Adds an sf object to the map.
#'
#' @inheritParams add_polygon
#' @param ... other argumetns passed to one of the plotting layers. See details
#'
#' @details
#' The plotting layer is determined by the type of sf geometries.
#'
#' \itemize{
#' \item{POINT and MULTIPOINT objects will call \link{add_scatterplot}}
#' \item{LINESTRING and MULTILINESTRING objects will call \link{add_path}}
#' \item{POLYGON and MULTIPOLYGON objects will call \link{add_polygon}}
#' \item{GEOMETRY objects will call \link{add_geojson}}
#' }
#'
#' @export
add_sf <- function( map, data = get_map_data(map), ... ) {
if (!inherits(data, "sf")) stop("mapdeck - expecting sf object")
geom <- data[[ attr( data, "sf_column" ) ]]
cls <- substr( class(geom)[1], 5, nchar(class(geom)[1]))
if( cls %in% c("POINT","MULTIPOINT")) {
add_scatterplot( map = map, data = data, ... )
} else if ( cls %in% c("LINESTRING", "MULTILINESTRING")) {
add_path( map = map, data = data, ... )
} else if ( cls %in% c("POLYGON", "MULTIPOLYGON")) {
add_polygon( map = map, data = data, ... )
} else {
add_geojson( map = map, data = data, ... )
}
}