forked from SymbolixAU/googleway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyline.R
78 lines (61 loc) · 2.12 KB
/
polyline.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
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
#' Decode PL
#'
#' Decodes an encoded polyline into the series of lat/lon coordinates that specify the path
#'
#' @note
#' An encoded polyline is generated from google's polyline encoding algorithm (\url{https://developers.google.com/maps/documentation/utilities/polylinealgorithm}).
#'
#' @seealso \link{encode_pl}, \link{google_directions}
#'
#' @param encoded String. An encoded polyline
#' @return data.frame of lat/lon coordinates
#' @importFrom Rcpp evalCpp
#' @examples
#' ## polyline joining the capital cities of Australian states
#' pl <- "nnseFmpzsZgalNytrXetrG}krKsaif@kivIccvzAvvqfClp~uBlymzA~ocQ}_}iCthxo@srst@"
#'
#' df_polyline <- decode_pl(pl)
#' df_polyline
#' @export
decode_pl <- function(encoded){
if(!inherits(encoded, "character") | length(encoded) != 1)
stop("encoded must be a string of length 1")
tryCatch({
rcpp_decode_pl(encoded)
},
error = function(cond){
message("The encoded string could not be decoded. \nYou can manually check the encoded line at https://developers.google.com/maps/documentation/utilities/polylineutility \nIf the line can successfully be manually decoded, please file an issue: https://github.com/SymbolixAU/googleway/issues ")
})
}
#' Encode PL
#'
#' Encodes a series of lat/lon coordinates that specify a path into an encoded polyline
#'
#' @note
#' An encoded polyline is generated from google's polyline encoding algorithm (\url{https://developers.google.com/maps/documentation/utilities/polylinealgorithm}).
#'
#' @seealso \link{decode_pl}
#'
#' @param lat vector of latitude coordinates
#' @param lon vector of longitude coordinates
#'
#' @examples
#' encode_pl(lat = c(38.5, 40.7, 43.252), lon = c(-120.2, -120.95, -126.453))
#' ## "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
#'
#'
#' @return string encoded polyline
#'
#' @export
encode_pl <- function(lat, lon){
# if(!inherits(df, "data.frame"))
# stop("encoding algorithm only works on data.frames")
if(length(lat) != length(lon))
stop("lat and lon must be the same length")
tryCatch({
googlePolylines::encodeCoordinates(lon, lat)
},
error = function(cond){
message("The coordinates could not be encoded")
})
}