[R] passing of longitude and lattitude arguments to read URL in Google Maps and extract routes

Franckx Laurent laurent.franckx at vito.be
Tue Jul 2 10:59:41 CEST 2013


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&region=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&region=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".

Does anyone have a suggestion on how to circumvent the problem?




Laurent Franckx, PhD
VITO NV
Boeretang 200, 2400 MOL, Belgium
Tel. + 32 14 33 58 22
Skype: laurent.franckx
laurent.franckx at vito.be
Visit our website: www.vito.be/english and http://www.vito.be/transport











[http://www.vito.be/e-maildisclaimer/vito.png]


Ontdek hoe VITO de transitie naar een duurzame maatschappij op gang trekt: www.vito.be/duurzaamheidsverslag2012<http://www.vito.be/duurzaamheidsverslag2012>
Discover how VITO initiates the transition towards a sustainable society: www.vito.be/sustainabilityreport2012<http://www.vito.be/sustainabilityreport2012>


VITO Disclaimer: http://www.vito.be/e-maildisclaimer



More information about the R-help mailing list