Created
November 26, 2017 20:50
-
-
Save markusloecher/31aca11bd8e25e63a9948fe62afdbab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#borrow from | |
#http://s4rdd.blogspot.de/2012/12/google-maps-api-decoding-polylines-for.html | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment