2

I'm trying to use jsonlite to flatten the results from google maps directions api.

The results are in json format and they have some sections like these here:

\"polyline\" : {\n                        \"points\"
 : \"xdyQtaqmJb@Ab@?|@AfBAtA?l@At@@D?F?D?\"\n        
             },\n                    
 \"start_location\" : {\n                       
 \"lat\" : -3.0831712,\n 


\"polyline\" : {\n                        \"points\"
 :
 \"b}yQ`iqmJFD@@?@@@?@?@?@?B?@?@CXAPAJATCZ?@?@?@?@@@?
 @@B@@@?@@@@@?@?@?@?bBH\"\n                     },\n 
                    \"start_location\" : {\n    

in most of then I have "\" inside the coding for points which in turn makes jsonlite to crash with the error

> fromJSON(out)
Error: lexical error: inside a string, '\' occurs before a character which it may not.
               "points" : "rsuQnzomJhBD\@lAF"                      }, 
                     (right here) ------^

I need some directions on how to double escape \ just inside the pair of double quotes after \"points\" : \

Here the code I use to get the json output

  origin="-3.06010901,-60.04375624"
  destination="-3.0876276,-60.06031519" 
  mode="walking"
  units="metric"
  language="en-EN"

  baseURL <- "https://maps.googleapis.com/maps/api/directions/json?"
  callURL <- paste0(baseURL,"origin=", origin, 
                             "&destination=", destination,
                             "&units=", tolower(units),
                             "&mode=", tolower(mode),
                             "&language=",language)

  tmout=10
  opts = RCurl::curlOptions(connecttimeout=tmout)
  out <- RCurl::getURL(callURL, .opts = opts)

Well, I still don't have an easy answer to flatten this output to a data frame, but with the examples from this post [A biased comparsion of JSON packages in R] I've got to retrive the output with RJSONIO::fromJSON(jsonOutput,unexpected.escape = "keep")

1

Thanks

jcarlos
  • 425
  • 7
  • 17
  • Can't you read this in a similar manner [to this](http://stackoverflow.com/questions/24183007/is-it-possible-to-read-geojson-or-topojson-file-in-r-to-draw-a-choropleth-map)? – Roman Luštrik Sep 07 '16 at 09:49
  • @RomanLuštrik unfortunately not. The json output just have those polylines encoded as a field, you need to decode it later and only then reach the coordinates.This json is not a spatial data file. I tried the solutions proposed there, but it didn't worked. thanks. – jcarlos Sep 07 '16 at 10:43
  • a `dput(out)` would help to reproduce the error. As it's hard to create a bad string from scratch. – Tensibai Sep 07 '16 at 11:17
  • @Tensibai Sorry for my ignorance, but do I attach, or better what is the "right" way to attach/bring the dput(out) here? – jcarlos Sep 07 '16 at 12:49
  • 1
    Did you run the command ? A copy paste here should do, I'm unsure it will be usable, but worth trying. That said, SymbolixAU answer sounds the way to go (calling fromJSON on the url) – Tensibai Sep 07 '16 at 12:52
  • @Tensibai I have edited the question to include the code for the json output. – jcarlos Sep 07 '16 at 13:14
  • 2
    You should note that while it's possible to make calls to Google's API without a key (for backwards compatibility), they now state that ["all Google Maps Directions API applications require authentication"](https://developers.google.com/maps/documentation/directions/get-api-key) – SymbolixAU Sep 07 '16 at 20:43

1 Answers1

2

If you're using Google Maps API then my googleway package handles this for you

library(googleway)

## your valid Google API key
key <- read.dcf("~/Documents/.googleAPI", fields = "GOOGLE_API_KEY")

directions <- google_directions(origin = "Melbourne International Airport, Melbourne, Austrlia",
                                destination = "MCG, Melbourne, Australia",
                                key = key)

## and to decode the polyline:
df_route <- decode_pl(directions$routes$overview_polyline$points)
head(df_route)
#         lat      lon
# 1 -37.67477 144.8494
# 2 -37.67473 144.8494
# 3 -37.67417 144.8493
# 4 -37.67411 144.8493
# 5 -37.67409 144.8494
# 6 -37.67409 144.8495

Alternatively, if you want to do this yourself, you're better off using the jsonlite package: jsonlite::fromJSON(your_url) to read the JSON directly.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • Thanks, but I've got to decode overview_polyline$points already, the issue came as I tried to flatten the directions output from google maps api to a data frame. And jsonlite gives the error displayed in the question "fromJSON(out) Error: lexical error: inside a string,". Apparently it doesn't handle the "unexpected scaped sequences", but RJSONIO does. – jcarlos Sep 07 '16 at 12:56
  • 1
    @jcarlos try `jsonlite::fromJSON(callUrl)` – Tensibai Sep 07 '16 at 13:26
  • @Tensibai Sorry for the ignorance, again. So should I grant this answer as the correct? – jcarlos Sep 07 '16 at 13:38
  • 1
    @jcarlos I think so – Tensibai Sep 07 '16 at 13:44