Google Maps routing for QGIS

Just a little post with something not totally related with Ecology, but nevertheless quite useful for the daily work with GIS. Some time ago i got hand on a nice Garmin GPS device (Etrex 30) and i am constantly playing around with the options and opportunities for it. Especially in the interaction framework of fieldwork and desktop-based GIS processing. Now i was at the lucky situation to have some time available to go birding in the area of Vestamager southwest of Copenhagen, Denmark. My intention was to bicycle there and use the GPS for orientation (although i perfectly know the route 🙂 ).

Thus i wrote this simple little R processing script (see a general introduction how to create R-scripts for processing here), which uses the route(…) function of the ggmap package to generate line layers from a point x to point y. Note, that by using this function you are agreeing to the Google Maps API Terms of Service and you are only allowed to send 2500 queries per day.

Generate the route from x to y and specify bicycle as output.

Generate the route from x to y and specify bicycle as output.

The generated output line is automatically loaded into QGIS after processing and has the total length and duration of the trip in its attribute-table.

Queried route for Copenhagen with googlemaps displayed on an OSM background in QGIS

Queried route for Copenhagen with googlemaps displayed on an OSM background in QGIS

To use the script, create a new one in the processing toolbox and copy the contents below into it. Then copy it into your “~/.qgis2/processing/rscripts” folder. I will also post the script in the QGIS scripts section here on this blog.

# GGmap routing script by Martin Jung
# Homepage:
##Vector processing=group
##x = string Copenhagen, Denmark
##y = string Berlin, Germany
##type= string driving
##output = output vector
if (!require(ggmap)){print("ggmap not installed. Will install it now");install.packages(ggmap, dependencies = TRUE)}

r <- route(from=x,to=y,mode=type,structure="route",output="simple",alternatives=F,messaging=F) # get the route
cs <- CRS("+proj=longlat +datum=WGS84 +no_defs") # WGS84 projection

l <- Lines(list(Line(r[c("lon","lat")])),ID=paste0(type,"_track"))
sl <- SpatialLines(list(l),cs)

data <- data.frame(from=x,to=y,type=type,length_km=sum(r$km,na.rm=T),duration_h=sum(r$hours,na.rm=T))
output <- SpatialLinesDataFrame(sl,data=data,match.ID=F)

To use existing shapefiles from within QGIS, users are advised to take a look at the pgrouting extension for Postgis. See a nice tutorial for installation and configuration on windows machines here.


Tags: , ,

About Martin Jung

PhD researcher at the University of Sussex. Interested in nature conservation, ecology and biodiversity as well as statistics, GIS and 'big data'
Sussex Research Hive

Supporting the research community at the University of Sussex

Small Pond Science

Research, teaching, and mentorship in the sciences

Landscape Ecology 2.0

intersecting landscape ecology, open science, and R


The Research Blog of IIASA

Jörg Steinkamps Blog

Mainly things about R, Linux and vegetation modeling

Amy Whitehead's Research

the ecological musings of a conservation biologist

Michael McCarthy's Research

School of BioSciences, The University of Melbourne

The Rostrum

science, statistics, policy and more


Environmental Change - Understand, Predict, Adapt

Dynamic Ecology

Multa novit vulpes


METeorological Visualisation Utilities using R for Science and Teaching

A Birder´s Blog

"Everybody loves what they know"

Trust Me, I'm a Geographer

Using Technology to Explore Our World

Duncan Golicher's weblog

Research, scripts and life in Chiapas

%d bloggers like this: