Franz Hahn Franz Hahn - 1 year ago 171
R Question

HTTP error 400 on google_elevation() call

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

and subsequently getting the elevation data at the corresponding lat/lon coordinates using
. I get the polylines from Strava, using the rStrava package.

Sample polylines:

pl1 <- "q{|[email protected]@[email protected]@B\\[email protected]^W`[email protected]@D`@C|@[email protected]@[email protected]@[email protected]@^\\`@[email protected]@[email protected]@[email protected]\\[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]|@[email protected][email protected]@[email protected][email protected]@\\NTCXBNEZCZKbB]rAKpDK~BO\\[email protected]?`[email protected]`@[email protected]@NXNP\\[email protected]\\[email protected]@Z?\\NvArA~BxA"
pl2 <- "[email protected]@{[email protected]}B}AoDyA_CASEMGKKI][email protected]@[email protected]@[email protected]@[email protected]]?[@[email protected]][email protected]][email protected]@}[email protected]@[email protected]@[email protected][[email protected]@[email protected]@][email protected]@[email protected]@{@][email protected]][email protected]@[email protected]@[email protected][[email protected][email protected]@][email protected]@[email protected]}@[email protected][email protected]@SbAKbAKXSpAS`@[email protected]@[email protected]@I[[email protected]@[email protected]@[email protected]@[email protected][email protected]@{@[email protected]][email protected]\\[email protected]@WtAO`[email protected]@[email protected]@[email protected]@[email protected]][email protected]@[email protected]@S}@[email protected]@[email protected]@[email protected]{@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@NO?[[email protected]@OOSKF[[email protected]@[email protected]@[email protected]{@[email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]][email protected]@[email protected]}@[email protected]@[email protected]@[email protected]@[email protected][[email protected]@[email protected]@W][email protected]@[email protected]@[[email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@{@Q{@][email protected]@[email protected]@[email protected]@[email protected]^[email protected]@X]^[email protected]@][email protected]@[email protected]][email protected]@|AS`@[email protected]@[email protected]{@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
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.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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download