2017-06-01 12 views
9

का पालन करने के लिए ggmap मार्ग डेटा कैसे प्राप्त करूं मैं डीजीएम कैपिटल बिकशेयर डेटा का उपयोग करके सैकड़ों मार्गों की गणना और कल्पना करने के लिए जीजीएमएपी रूट फ़ंक्शन का उपयोग कर रहा हूं। मैं इसे एक मामूली समस्या के साथ सफलतापूर्वक करने में सक्षम हूं, मार्ग पथ सड़कों का पालन नहीं करता है, विशेष रूप से घुमावदार सड़कों (नीचे स्क्रीनशॉट देखें)। क्या मेरे कोड को अधिक विस्तृत पथों के लिए सभी को ट्वीक करने का कोई तरीका है?मैं रोड पथ

enter image description here

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") 
+0

'उत्पादन के साथ की कोशिश =" सभी "' 'अंदर मार्ग (था = शुरुआती बिंदु से, = = स्टॉपपॉइंट, मोड = 'साइकिल चलाना', संरचना = "मार्ग", आउटपुट = "सब") ' – SymbolixAU

+0

यह डू .all (rbind ... इस त्रुटि को फेंकने का कारण बनता है। त्रुटि (फ़ंक्शन (..., row.name एस = नल, check.rows = गलत, check.names = TRUE, तर्क पंक्तियों की भिन्न संख्या को इंगित करते हैं: 1, 0 –

+0

मैपज़न वाल्हल्ला एक वेब सेवा है जो मार्ग के बिंदु से मार्ग प्रदान करने के लिए मार्ग से बिंदु प्रदान कर सकती है। https://mapzen.com/blog/valhalla-intro/। GeoJSONio पैकेज स्पष्ट रूप से इसका उपभोग कर सकता है। –

उत्तर

1

जवाब do.call में decodeLine समारोह जगह बनाने के लिए लंबे समय से मार्गों dataframe

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