Update 14/03/2017
I've issued a fix to the development version so that google_elevation()
now accepts an encoded polyline as an argument. And it will also issue a warning if the URL length is greater than 8192 characters.
## install the development version
devtools::install_github("SymbolixAU/googleway")
library(googleway)
api_key <- "your_api_key"
map_key <- "your_map_key" ## for plotting the lines
pl1 <- "q{|aHknlv@n@Mt@IxAGb@B\Eb@AZGRG^W`AyAPQF?NHDJFZBz@FVNPn@D`@C|@DjACv@@h@Dh@Ll@Zj@^\`@fAx@j@r@d@n@\ZJPNd@NbANzAFhANb@JNLHD?PKHMRGPSr@UPMXGDGJELMFCPAbBWROpAm@h@YZe@dAaC@Of@aAb@a@t@iALg@LWNQROf@IjAJnFbAv@Dr@Aj@BpBx@fFdCd@HlAHd@Pp@b@|@d@~@j@n@R~@n@\NTCXBNEZCZKbB]rAKpDK~BO\?NCd@?`DMz@SLO`@URCR@XLRRRVHd@NXNP\RXf@JHXN\Xn@ZD@Z?\NvArA~BxA"
pl2 <- "uipaHmmcv@iBoFm@{AKQy@}B}AoDyA_CASEMGKKI]y@i@y@EOi@q@kAw@KEMAu@M]?[@WDkAZiB~@]H_@PUF]TUL_@XkAn@}CzAyAx@e@P_@JSNGROPa@Fu@[UOc@IOAu@_@WIUMIIWGu@]YEk@SoBiAc@_@gAeAg@{@]mAMq@YmBA]Oq@YuBKYUg@UQa@QiAOI@QIm@EiA[w@BSFYZ_@h@]t@o@bAOb@}@zAWn@_@jAGf@SbAKbAKXSpAS`@ICa@FSHICQ@gAHq@KSAa@I[CQ@k@KOGMKYg@aBkEs@u@k@g@_@a@{@o@W]IAYQYAsDIODc@LW\GRKn@Ch@WtAO`BCzAIj@Wh@SRc@NS@a@KaAg@kCgAm@]iBo@w@_@UQc@S}@Ww@e@MCa@Sm@Sk@WYI{@g@IIMAMIIIm@QuBaAIGu@_@SQiAqA_AmASSYIUEqAKQBm@Im@Qe@i@k@UQBSCe@NO?[FKFYBSH_ADa@GQIw@OOSKF[b@M@gAl@i@b@{@b@OLwAj@KLWRI@yBvAuAd@m@F_@@qAEk@Kc@Oi@_@_@e@oAeBc@u@SSi@]i@We@Im@Q}@k@w@_@i@a@w@a@SQ[Qy@OeCSUGg@WSOa@e@W]MK_@i@CMMWIEMWa@m@[QEIICK@QHUTe@n@ELKFa@l@u@t@WNYLYXO?IBGHc@Tc@Ja@@{@Q{@]s@a@kBq@YOcAYs@IU?_@NU@GAu@^a@LSPg@X]^YJc@h@]PKHI@w@n@]h@AXEJMTCZc@|AS`@OVg@j@_Av@{@bA"
df_elevation <- google_elevation(polyline = pl2, key = api_key)
head(df_elevation$results)
# elevation location.lat location.lng resolution
# 1 418.8341 47.60235 9.03399 19.0879
# 2 420.8099 47.60288 9.03519 19.0879
# 3 421.0847 47.60311 9.03565 19.0879
# 4 421.1900 47.60317 9.03574 19.0879
# 5 422.5887 47.60346 9.03637 19.0879
# 6 445.3051 47.60393 9.03725 19.0879
And seeing as we're on the development version we can plot the lines on a google map
df <- data.frame(pl = c(pl1, pl2))
google_map(key = map_key) %>%
add_polylines(data = df, polyline = "pl")
Old answer
You have inadvertently found the limits of the amount of data you can pass into the API
The Google API documentation says
You may pass any number of multiple coordinates within an array or encoded polyline, as long as you don't exceed the service quotas, while still constructing a valid URL
So it seems you've found that limit.
If you reduce the amount of lat/lons you send from the second polyline you get results returned. In your example, the limit is 239 pairs of coordinates.
# head(google_elevation(decode_pl(pl2)[1:239,], key=gmap_key)$results)
# elevation location.lat location.lng resolution
# 1 418.8218 47.60235 9.033990 19.0879
# 2 420.8087 47.60288 9.035190 19.0879
# 3 421.0829 47.60311 9.035649 19.0879
# 4 421.1866 47.60317 9.035740 19.0879
# 5 422.5816 47.60346 9.036369 19.0879
# 6 445.2881 47.60393 9.037250 19.0879
google_elevation(decode_pl(pl2)[1:240,], key=gmap_key)
# Error in open.connection(con, "rb") : HTTP error 400.
I've filed this as an issue so that in the next update this will be handled with either a more helpful error, or it will automatically break up the data.frame
.
For now the solution will be to decode the polylines first, then find the elevation on subsets of the data.frame at a time.
I've gone back to the Google Elevation API documentaition and it appears you can also send an encoded polyline to the API; if you can wait a few days I'll issue a fix to the development version so you can send the polyline directly
disclaimer: I'm the googleway author