首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取ggmap路径数据以跟踪路径?

如何获取ggmap路径数据以跟踪路径?
EN

Stack Overflow用户
提问于 2017-06-01 13:11:06
回答 1查看 669关注 0票数 9

我使用ggmap路由函数来计算和可视化使用D.C.CapitalBikeshare数据的数百条路线。我成功地完成了一个小问题,路径不跟随道路,特别是弯曲的道路(见下面的截图)。是否有一种方法可以将我的代码传递给所有人以获得更详细的路径?

代码语言:javascript
复制
library(tidyverse)
library(ggmap)

# Example dataset
feb_14 <- read.csv('https://raw.githubusercontent.com/smitty1788/Personal-Website/master/dl/CaBi_Feb_2017.csv', stringsAsFactors = FALSE)

# Subset first 300 rows, keep start and end Lat/Long strings
start<-c(feb_14[1:300, 14])
dest<-c(feb_14[1:300, 15])

# df of individual routes
routes <- tibble(
  start,
  dest)

# Function to calculate route
calculationroute <- function(startingpoint, stoppoint) {
  route(from = startingpoint,
        to = stoppoint,
        mode = 'bicycling',
        structure = "route")}

# Calculate route path for all individual trips
calculatedroutes <- mapply(calculationroute,
                           startingpoint = routes$start,
                           stoppoint = routes$dest,
                           SIMPLIFY = FALSE)

# Unlist and merge in single dataframe
do.call(rbind.data.frame, lapply(names(calculatedroutes), function(x) {
  cbind.data.frame(route=x, calculatedroutes[[x]], stringsAsFactors=FALSE)
})) -> long_routes


# create map with routes
basicmap <- get_map(location = 'washingtondc',
                    zoom = 13,
                    maptype = "toner-background",
                    source = "google",
                    color = "bw") 
basicmap <- ggmap(basicmap)


basicmap + geom_path(data=long_routes, 
                     aes(x=lon, y=lat, group=route), color = "red",
                     size=1, alpha = .4, lineend = "round")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-12 14:18:20

答案是将decodeLine函数放置到do.call中,以创建长路径数据

代码语言:javascript
复制
decodeLine <- function(encoded){
  require(bitops)

  vlen <- nchar(encoded)
  vindex <- 0
  varray <- NULL
  vlat <- 0
  vlng <- 0

  while(vindex < vlen){
    vb <- NULL
    vshift <- 0
    vresult <- 0
    repeat{
      if(vindex + 1 <= vlen){
        vindex <- vindex + 1
        vb <- as.integer(charToRaw(substr(encoded, vindex, vindex))) - 63  
      }

      vresult <- bitOr(vresult, bitShiftL(bitAnd(vb, 31), vshift))
      vshift <- vshift + 5
      if(vb < 32) break
    }

    dlat <- ifelse(
      bitAnd(vresult, 1)
      , -(bitShiftR(vresult, 1)+1)
      , bitShiftR(vresult, 1)
    )
    vlat <- vlat + dlat

    vshift <- 0
    vresult <- 0
    repeat{
      if(vindex + 1 <= vlen) {
        vindex <- vindex+1
        vb <- as.integer(charToRaw(substr(encoded, vindex, vindex))) - 63        

      }

      vresult <- bitOr(vresult, bitShiftL(bitAnd(vb, 31), vshift))
      vshift <- vshift + 5
      if(vb < 32) break
    }

    dlng <- ifelse(
      bitAnd(vresult, 1)
      , -(bitShiftR(vresult, 1)+1)
      , bitShiftR(vresult, 1)
    )
    vlng <- vlng + dlng

    varray <- rbind(varray, c(vlat * 1e-5, vlng * 1e-5))
  }
  coords <- data.frame(varray)
  names(coords) <- c("lat", "lon")
  coords
}

calculatedroutes <- mapply(calculationroute,
                           startingpoint = routes$start,
                           stoppoint = routes$dest,
                           SIMPLIFY = FALSE)

do.call(rbind.data.frame, lapply(names(calculatedroutes), function(x) {
  cbind.data.frame(route = x, decodeLine(calculatedroutes[[x]]$routes[[1]]$overview_polyline$points), stringsAsFactors=FALSE)
})) -> long_routes
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44308508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档