GRASS logo

Note: A new GRASS GIS stable version has been released: GRASS GIS 7. Go directly to the new manual page here

NAME - Finds shortest path on vector network.


vector, network, shortest path

SYNOPSIS help [-gs] input=name output=name [type=string[,string,...]] [alayer=integer] [nlayer=integer] [file=name] [afcolumn=string] [abcolumn=string] [ncolumn=string] [dmax=float] [--overwrite] [--verbose] [--quiet]


Use geodesic calculation for longitude-latitude locations
Write output as original input segments, not each path as one line.
Allow output files to overwrite existing files
Verbose module output
Quiet module output


Name of input vector map
Name for output vector map
Arc type
Options: line,boundary
Default: line,boundary
Layer number
Arc layer
Default: 1
Layer number
Node layer
Default: 2
Name of file containing start and end points. If not given, read from stdin
Arc forward/both direction(s) cost column
Arc backward direction cost column
Node cost column
Maximum distance to the network
If start/end are given as coordinates. If start/end point is outside this threshold, the path is not found and error message is printed. To speed up the process, keep this value as low as possible.
Default: 1000

DESCRIPTION determines least costly, e.g. shortest or fastest path(s) on a vector network.

Costs may be either line lengths, or attributes saved in a database table. These attribute values are taken as costs of whole segments, not as costs to traverse a length unit (e.g. meter) of the segment. For example, if the speed limit is 100 km / h, the cost to traverse a 10 km long road segment must be calculated as
length / speed = 10 km / (100 km/h) = 0.1 h.
Supported are cost assignments for both arcs and nodes, and also different costs for both directions of a vector line. For areas, costs will be calculated along boundary lines.

The input vector needs to be prepared with operation=connect in order to connect points representing center nodes to the network.

Nodes and arcs can be closed using cost = -1.

Least cost paths are written to the output vector map with an attached attribute table.

Nodes can be

The syntax is as follows:
id start_point_category end_point_category
(Example: 1 1 2)


id start_point_x start_point_y end_point_x end_point_y

Points specified by category must be exactly on network nodes, and the input vector map needs to be prepared with operation=connect.

When specifying coordinates, the next network node to a given coordinate pair is used.

The attribute table will contain the following attributes:


Nodes and arcs can be closed using cost = -1.

If the cost columns 'afcol', 'abcol' and 'ncol' are not specified, the length of network segments is measured and zero costs are assumed for nodes.

When using attributes, the length of segments is not used. To get accurate results, the line length must be taken into account when assigning costs as attributes. For example, to get the fastest path, the columns 'max_speed' and 'length' are required. The correct fastest path can then be found by specifying afcol=length/max_speed. If not yet existing, the column containing the line length ("length") has to added to the attributes table using


Shortest (red) and fastest (blue) path between two digitized nodes (Spearfish): example

# Spearfish

echo "1|601955.1|4916944.9|start
2|594385.6|4921565.2|end" | in=- cat=1 x=2 y=3 out=startend col="cat integer, \
                         east double precision, north double precision, label varchar(6)" startend

g.copy vect=roads,myroads

# create lines map connecting points to network myroads points=startend out=myroads_net op=connect thresh=500 alayer=1 nlayer=2

# set up costs

# create unique categories for each road in layer 3
v.category in=myroads_net out=myroads_net_time opt=add cat=1 layer=3 type=line

# add new table for layer 3
v.db.addtable myroads_net_time layer=3 col="cat integer,label varchar(43),length double precision,speed double precision,cost double precision,bcost double precision"

# copy road type to layer 3 myroads_net_time layer=3 qlayer=1 opt=query qcolumn=label columns=label

# upload road length in miles myroads_net_time layer=3 type=line option=length col=length unit=miles

# set speed limits in miles / hour
v.db.update myroads_net_time layer=3 col=speed val="5.0"
v.db.update myroads_net_time layer=3 col=speed val="75.0" where="label='interstate'"
v.db.update myroads_net_time layer=3 col=speed val="75.0" where="label='primary highway, hard surface'"
v.db.update myroads_net_time layer=3 col=speed val="50.0" where="label='secondary highway, hard surface'"
v.db.update myroads_net_time layer=3 col=speed val="25.0" where="label='light-duty road, improved surface'"
v.db.update myroads_net_time layer=3 col=speed val="5.0" where="label='unimproved road'"

# define traveling costs as traveling time in minutes:

# set forward costs
v.db.update myroads_net_time layer=3 col=cost val="length / speed * 60"
# set backward costs
v.db.update myroads_net_time layer=3 col=bcost val="length / speed * 60"

# ... the 'start' and 'end' nodes have category number 1 and 2

# Shortest path: ID as first number, then cat1 and cat2
echo "1 1 2" | myroads_net_time alayer=3 nlayer=2 out=mypath

# Fastest path: ID as first number, then cat1 and cat2
echo "1 1 2" | myroads_net_time alayer=3 nlayer=2 afcol=cost abcol=bcost out=mypath_time
To display the result, run for example:
g.region vect=myroads_net
d.mon x0
d.vect myroads_net
# show shortest path
d.vect mypath col=red width=2
# show fastest path
d.vect mypath_time col=blue width=2

# start and end point
d.vect myroads_net icon=basic/triangle fcol=green size=12 layer=2
d.font font=Vera
d.vect startend disp=cat type=point lsize=14 layer=2




Radim Blazek, ITC-Irst, Trento, Italy
Documentation: Markus Neteler, Markus Metz

Last changed: $Date: 2013-05-23 13:01:55 -0700 (Thu, 23 May 2013) $

Main index - vector index - Full index

© 2003-2016 GRASS Development Team