Skip to content

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

3 messages · Franckx Laurent, David Winsemius

#
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
#
On Jul 2, 2013, at 1:59 AM, Franckx Laurent wrote:

            
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
long_or <- 4.384575
lat_des <-  51.13156
long_des <- 4.387118
[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.
#
Dear David

Thank you for the suggestion.
The following works fine:

        or_dest <- paste0("origin=",lat_or,",",long_or,"&destination=",lat_des,",",long_des)
        url_to_google <- paste( "http://maps.googleapis.com/maps/api/directions/json?",or_dest,"&sensor=false&mode=transit&departure_time=1372665319",sep="" )
        route <- url(url_to_google)

Problem solved, thus.

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: dinsdag 2 juli 2013 15:56
To: Franckx Laurent
Cc: 'r-help'
Subject: Re: [R] passing of longitude and lattitude arguments to read URL in Google Maps and extract routes
On Jul 2, 2013, at 1:59 AM, Franckx Laurent wrote:

            
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
long_or <- 4.384575
lat_des <-  51.13156
long_des <- 4.387118
[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.
--

David Winsemius
Alameda, CA, USA

[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