On Jul 2, 2013, at 1:59 AM, Franckx Laurent wrote: > Dear all > > I try to use Google Maps to calculate travel times per transit between an > origin an destination. > > The guidelines for the search can be found at: > https://developers.google.com/maps/documentation/directions/#TravelModes > > When I submit the latitude and the longitude of the origin and destination as > literals, things work fine. > > For instance, the following code executes correctly and we obtain the > distance and trip duration (the output of the search is in JSON format and is > converted to an R object with fromJSON) > > library(rjson) > library(gooJSON) > route <- url('http://maps.googleapis.com/maps/api/directions/json? > > origin=51.13854,4.384575&destination=51.13156,4.387118®ion=be&sensor=false&mode=transit&departure_time=1372665319') > route_file <- file("route_file.json") > L <- readLines(route,-1) > writeLines(L, route_file) > close(route) > routesR_zone1_to_zone20 <- fromJSON( file = route_file ) > routesR_zone1_to_zone20$routes[[1]][[3]][[1]]$distance$value/1000 > routesR_zone1_to_zone20$routes[[1]][[3]][[1]]$duration$value/60 > > > However, what I am really interested in is to repeat this operation for > thousands of origin-destination pairs. The longitude and the latitude of the > origins and destinations then become variables. > > For instance: > >> lat_or > [1] 51.13854 >> long_or > [1] 4.384575 >> lat_des > [1] 51.13156 >> long_des > [1] 4.387118 >> route <- url('http://maps.googleapis.com/maps/api/directions/json? >> >> origin=lat_or,long_or&destination=lat_des,long_des®ion=be&sensor=false&mode=transit&departure_time=1372665319') >> route_file <- file("route_file.json") >> L <- readLines(route,-1) >> writeLines(L, route_file) >> close(route) >> routesR_zone1_to_zone20 <- fromJSON( file = route_file ) >> routesR_zone1_to_zone20 > $routes > list() > > $status > [1] "NOT_FOUND" > > Thus, although the coordinates are the same as in the previous example, this > time, no route is found. > > I suppose that the problem is that, when the url is accessed, lat_or etc are > not "translated" in the corresponding numeric values, and that Google tries > to calculate the route between the literals " lat_or,long_or" and " > lat_des,long_des".
Yes. That seems likely. R would not interpret a text literal. I don't understand why you are not using the ordinary text handling function 'paste0'. ?paste0 > lat_or <- 51.13854 long_or <- 4.384575 lat_des <- 51.13156 long_des <- 4.387118 > paste0("origin=",lat_or,",",long_or,"&destination=",lat_des,",",long_des) [1] "origin=51.13854,4.384575&destination=51.13156,4.387118" (Also tested on Mac with R 3.0.0 RC using Google JSON Data Interpreter for R, Version: 1.0.01.) -- David. > > Does anyone have a suggestion on how to circumvent the problem? > > > > > Laurent Franckx, PhD > VITO NV > Boeretang 200, 2400 MOL, Belgium > -- David Winsemius Alameda, CA, USA ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.