Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

r - HTTP error 400 on google_elevation() call

I am using the R-package googleway to decode polylines using decode_pland subsequently getting the elevation data at the corresponding lat/lon coordinates using google_elevation. I get the polylines from Strava, using the rStrava package.

Sample polylines:

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"

Decoding works fine:

decoded_pl1 <- decode_pl(pl1)
decoded_pl2 <- decode_pl(pl2)

But when I try to get the elevation, I get an error on the second polyline, and I can't make real sense of it:

# This one works fine
google_elevation(decode_pl(pl1), key=gmap_key)

# This one gives HTTP error 400
google_elevation(decode_pl(pl2), key=gmap_key)

I checked the lat/lon data coming out of the decode_pl and they seems fine. I can't really make sense of it...

I make this exact same call on a whole set of coordinates, and this is the only one that errors.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...