我有一个包含数千条折线的矢量,我已使用Google Directions API检索到这些折线。我正在尝试使用传单包绘制所有折线。我想知道是否有更简洁的方法来绘制所有折线而不使用数千个addPolylines()
函数?
以下是四条折线的示例:
polylines <- c(
"ksktDdp|dQJmB|@uEp@eDr@_D`@yB`@uBRm@LUtAkCZu@Pk@Nu@Hs@DwABoB@u@Gg@WDkCh@_@TaLfDiD~@yUfF{GvAqAX]NkBb@_A\\q@RiAZgAZe@Pu@^g@^o@n@o@z@_AdBqB`EwBrDiDfFIZ_@f@kHzJWZoBzCaCbEi@bAsHfNmGbLqA~B{HjNqEhIiFnJaB~CGf@Sh@iArC]pAQlACh@AjAFbARrAV~@f@fA^h@l@n@`@\\fClBRPFPr@f@hHhFdFtDzCzBzIrGNNNNb@^nD`DtEdEZ`@xAvBZl@r@fB^nA\\dB`@`FDfCF|AXhIJpERlHXzJh@|KL~DDpD?fIBrGbAzYxAbc@VlSVxMRdOJlTF~KH~CX|IDjKAxGDxDHbXR|FZpEpAvO~@rJh@vGRtFL|G^`SZvQ@hEQ`HM`Cg@xFm@tEe@bDi@dF[xEQlFCjED~P@zCKl@?|AFtBPtDZdEANAt@MbAa@fAU^k@j@IF_AZ_ADo@BGBy@A]AM?{BOeEc@g@QmCg@kDs@EEcGaAmFs@aD_@o@CeHs@yB[P|JLjFLxDHpEPxGNrGLbEFl@PhAvBbJnA`Ff@tB_A?mDBsC@?gAi@BEAEECwA@KFGHCP@",
"ksktDdp|dQJmB|@uEdBeI`@yB`@uBRm@LUtAkCl@aBNu@Hs@HgE@u@Gg@WDkCh@_@TkQfFyUfF{GvAqAX]NkBb@qBp@iAZmBl@u@^g@^o@n@o@z@qDfHwBrDiDfFIZ_@f@kHzJgCvDaCbEi@bAsHfNmGbL_Rt\\kInOmBjE}A~EeB~Fy@nC}@rCcAtC{BjFkA|B}D~HaIpO{EhJYb@oA~AcA`AoBlAcBl@uB`@uCRq@@cSBaBAe@C}@MoAYmHaC{DoAaAe@uAaAoAwAcAkBs@eB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95.34172
3 29.71937 -95.34065
4 29.71912 -95.33982
5 29.71886 -95.33902
6 29.71869 -95.33841
绘制单个折线时,我使用以下内容:
gepaf::decodePolyline(polylines[1]) -> SOF_sample
leaflet(SOF_sample) %>%
addProviderTiles("CartoDB.DarkMatter") %>%
addPolylines(lng = SOF_sample$lon, lat = SOF_sample$lat)
以下是结果的屏幕截图:SOF_sample
我想弄清楚的是如何绘制所有折线而不添加多个addPolylines()
函数,如下所示:
leaflet(SOF_sample) %>%
addProviderTiles("CartoDB.DarkMatter") %>%
#first polyline
addPolylines(lng = SOF_sample$lon, lat = SOF_sample$lat) %>%
#second polyline
addPolylines(lng = gepaf::decodePolyline(polylines[2])$lon,
lat = gepaf::decodePolyline(polylines[2])$lat) %>%
#third polyline
addPolylines(lng = gepaf::decodePolyline(polylines[3])$lon,
lat = gepaf::decodePolyline(polylines[3])$lat) %>%
#fourth polyline
addPolylines(lng = gepaf::decodePolyline(polylines[4])$lon,
lat = gepaf::decodePolyline(polylines[4])$lat)
添加数千addPolylines()
会给我the result i want。我想知道是否有一种更有效/更清洁的方式,因为我有一种感觉,我上面编写的内容不可能是最好的方法。
谢谢!
答案 0 :(得分:4)
一种方法是将折线转换为SpatialLines
对象,然后绘制。如果您不熟悉空间物体,我建议您查看它们,因为它们对绘图非常有用。
library(leaflet)
library(sp)
library(data.table)
library(googleway) ## disclosure: this is my package
## (and you can also access the google directions API
## through the google_directions() function)
lst <- lapply(polylines, function(x) googleway::decode_pl(x))
## I like working with data.tables
dt <- rbindlist(lst, idcol = "id")
lst_lines <- lapply(unique(dt$id), function(x){
## the order of the 'lon' and 'lat' fields is important
Lines(Line(dt[id == x, .(lon, lat)]), ID = x)
})
## spatial lines object
spl_lst <- SpatialLines(lst_lines)
## can now plot the spatial lines
leaflet(spl_lst) %>%
addProviderTiles("CartoDB.DarkMatterNoLabels") %>%
addPolylines(opacity = 0.4, weight = 3, color = "#ffff00")
截至googleway的第2版,编码折线可以使用我的googleway软件包直接绘制
library(googleway)
## You need a Google API key to use their maps
mapKey <- 'your_api_key'
## the lines need to be in a data.frame
df <- data.frame(polylines = polylines)
google_map(key = mapKey) %>%
add_polylines(data = df, polyline = "polylines")