Network analysis using four interfaces
Using convenient GUI wrapper
After opening GRASS GIS, add vector map named streets_wake. In Map Display, find toolbar icon which says Analyze map and in the submenu, select Vector network analysis tool.


Using a module directly through GUI
Behind most features in the GRASS GIS GUI, there is a GRASS GIS module which does the actual analysis and processing. Not all the modules are wrapped in a specialized GUI, mostly because it is not needed. Instead, all modules have standardized interface which looks similar for all the modules. You can open the the module through the main menu or the Search modules tab. The shortest path can be found in Vector → Network analysis → Shortest path. If you need to search for the module, use the search in the Search modules tab. On the other hand, if you already know the name of the module, you can use the Command console tab type the name of the module and press Enter. The module we are using is called v.net.path.
When you have the GUI, you can select an existing vector map as input.
You can see all the vector maps which are in the GRASS GIS Spatial Database.
We will use streets_wake.
The coordinates are specified in a text file but for our convenience, the GUI offers an option to edit the file directly. However, first we need to get the coordinates of our starting and end point. In the Map Display, we can use right mouse click to get coordinates of the current position. We copy the coordinates for both points to the large input box for the file parameter.
The file format requires that coordinates for both points are on a single line (one line is a one pair of points) and that coordinates separated by a space, so we need to replace commas by spaces. We also need to add number one (or any other number) at the beginning of the line to mark our pair of points (different numbers would be entered for different lines if we had more pairs of points).
To understand which format is required for the input file, you can referrer
to the manual. The module GUI has a Help button which takes you
to the Manual tab where you can learn all the details about
certain behavior of the module.
Using command line
v.net.path module accepts a file with a list of points to connect by shortest path. The syntax of the file is explained in the v.net.path manual. We have just one pair of points, so we need to create a file like this:
1 622880.2 228097.3 666860.6 236729.0
You can use any (plain) text editor to create the file. Name it
for example points.txt
.
You will need to specify the full path to the created file in the command
line. Depending on your operating system and workflow, you may want to
copy the path the the clipboard or change the current directory in
the command line to the directory with the file so that you avoid typing
manually the whole path.
Now we can run the v.net.path module from command line:
Commands can be executed in the Command console tab or the
system command line which was opened with GRASS GIS.
It's worth noting that this is the way how are the examples in the manual
usually presented. To reproduce the examples using the GUI, just find
the given option by its name, which is the portion in front of the equal
sign and fill whatever is after the equal sign.
v.net.path input=streets_wake output=shortest_path_3 file=/path/to/points.txt
Alternatively, if you have Bash or similar command line available, you can provide the parameters to v.net.path in the command line. For the list of points, you can use standard input where v.net.path expects the list of points if no file name was provided.
To provide the standard input on the command line, we can use
here document
syntax (<<EOF...EOF
):
v.net.path input=streets_wake output=shortest_path_3 <<EOF
1 622880.2 228097.3 666860.6 236729.0
EOF
Using Python
In Python, we will use the fact that v.net.path reads the list of points from the standard input when no file name is provided. The functionwrite_command
from the module grass.script
which is available in GRASS GIS feeds the standard input to the module.
The content, our list of points, is provided in the stdin
parameter.
# do the import
import grass.script as gs
# set the variables
network = "streets_wake"
path = "shortest_path_4"
points = "1 622880.2 228097.3 666860.6 236729.0"
# run the module
gs.write_command('v.net.path', input=network, output=path, stdin=points)
You can paste and run the lines above one by one to the Python tab
in GRASS GIS GUI or your can use Paste special right click function
to paste all lines at once if it is available in your version.
Alternatively, you can use a Python editor to create a Python script.
Since version 7.2 GRASS GIS comes with an integrated Python editor,
or you can use any Python or text editor you want.
To run the script, you can use File → Launch script
in the Layer Manager window, or if you are using the Python editor
integrated in GRASS GIS, just press the run (play) button in the toolbar.