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.
Open network analysis tool
In the Parameters check that you have selected Shortest path and the input is set to streets_wake. Then switch to Points tab. Select a point in the list and click the mouse cursor button which says Insert points from Map Display. Click somewhere in the Map Display where you want one end of the path. Click again to place the second end of the path.
Pick two points in the Map Display
To start the analysis, click the run (play) button in the toolbar at the top. You can view the textual output of for the analysis in Output tab. Once the analysis is finished, you can see the result in the Map Display. Using a button in the toolbar you can save the result permanently as a vector map.
Resulting path and textual output from the module

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

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.

Select the network from existing vector maps
We will enter manually the name of the newly created vector map which will show the shortest path. We can type, for example shortest_path.
Enter name of the new vector map
In case you use a name of the existing map, the module will not overwrite existing data and will tell you that the vector map already exists. If you wish to replace the old data by the new output, you can specify this by checking the check box which says Allow output files to overwrite existing files.

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.

Pick position in the Map Display and copy the coordinates with right click
Enter two points to the module dialog
Find the format specification of the input points in the manual

Using command line module accepts a file with a list of points to connect by shortest path. The syntax of the file is explained in the 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 module from command line: input=streets_wake output=shortest_path_3 file=/path/to/points.txt
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.

Alternatively, if you have Bash or similar command line available, you can provide the parameters to in the command line. For the list of points, you can use standard input where 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): input=streets_wake output=shortest_path_3 <<EOF
1 622880.2 228097.3 666860.6 236729.0

Using Python

In Python, we will use the fact that reads the list of points from the standard input when no file name is provided. The function write_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('', 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.

Learn more