Franz Hahn Franz Hahn - 9 months ago 67
R Question

HTTP error 400 on google_elevation() call

I am using the R-package googleway to decode polylines using

decode_pl
and 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.

Answer Source

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