| file: | This file is part of the pgRouting project. |
|---|---|
| Copyright: | Copyright (c) 2013-2026 pgRouting developers |
| license: | Creative Commons Attribution-Share Alike 3.0 https://creativecommons.org/licenses/by-sa/3.0 |
Chapter Contents
While pgRouting functions provide a low-level interface, developing for a higher-level application requires these requirements to be represented directly in the SQL queries. As these SQL queries get more complex, it is desirable to store them in PostgreSQL stored procedures or functions. Stored procedures or functions are an effective way to wrap application logic, in this case, related to routing logic and requirements.
The function will wrap pgr_dijkstra.
The function will use:
vehicle_netvehicle_penalized_net
The function returns the following routing information:
seq- A unique identifier of the rowsid- The segment's identifierseconds- Number of seconds to traverse the segmentname- The segment's namelength- The segment's lengthazimuth- The azimuth of the segmentreadable- The geometry in human readable form.geom- The routing geometry
Design of the function
The function to be created wrk_dijkstra with the following input parameters and
output columns:
Input parameters
| Parameter | Type | Description |
|---|---|---|
source |
BIGINT | The identifier of the departure location. |
target |
BIGINT | The identifier of the destination location. |
Output columns
| Name | Type | Description |
|---|---|---|
seq |
INTEGER | A unique number for each result row. |
id |
BIGINT | The edge identifier. |
seconds |
FLOAT | The number of seconds it takes to traverse the segment. |
name |
TEXT | The name of the segment. |
length |
FLOAT | The length in meters of the segment. |
azimuth |
FLOAT | The azimuth of the segment. |
readable |
TEXT | The geometry in human readable form. |
geom |
geometry | The geometry of the segment in the correct direction. |
When the application needs additional information, like the name of the street,
JOIN the results with other tables.
Problem
- Create a function that gets all the required information
Solution
- The function returns the columns asked.
- Rename
pgr_dijkstraresults to application requirements names. LEFT JOINthe results withvehicle_netto get the additional information.LEFT JOINto include the row withid = -1because it does not exist onvehicle_net
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :force: :start-after: get_more_info.txt :end-before: get_read_geom.txt
Problem
Get the geometries of the route from |place_1| to |place_2|
Solution
- Select the column
geom.
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :start-after: get_geom.txt :end-before: get_azimuth.txt
.. collapse:: Query results .. literalinclude:: ../scripts/basic/sql_function/get_geom.txt
Problem
Get the geometries in readable form of the route from |place_1| to |place_2|
Solution
- Select the column
readable.
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :start-after: get_read_geom.txt :end-before: get_geom.txt
.. collapse:: Query results .. literalinclude:: ../scripts/basic/sql_function/get_read_geom.txt
There are many geometry functions in PostGIS, the workshop covers some
of them like ST_AsText, ST_Reverse, ST_EndPoint, ST_Azimuth.
Problem
Get the azimuth of the geometries of the route from |place_1| to |place_2|
Solution
- The function returns
azimuth.
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :start-after: get_azimuth.txt :end-before: wrong_directionality.txt
.. collapse:: results .. literalinclude:: ../scripts/basic/sql_function/get_azimuth.txt
Visually, with the route displayed with arrows, it can be found that there are arrows that do not match the directionality of the route.
To have correct directionality, the ending point of a geometry must match the starting point of the next geometry
- Inspecting the detail of the results of Exercise 3: Route geometry (human readable)
.. collapse:: Rows where criteria is not met
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql
:language: sql
:start-after: wrong_directionality.txt
:end-before: fix_directionality.txt
.. literalinclude:: ../scripts/basic/sql_function/wrong_directionality.txt
Problem
- Fix the directionality of the geometries and of the columns that depend on it.
geomreadableazimuth
Solution
To get the correct direction some geometries need to be reversed:
- Reversing a geometry will depend on the
nodecolumn of the query to Dijkstra. - A conditional
CASEstatement that returns:- The geometry when
nodeis thesourcecolumn. - The reversed geometry when
nodeis not thesourcecolumn.
- The geometry when
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :linenos: :force: :start-after: fix_directionality.txt :end-before: good_directionality.txt
Inspecting the problematic rows, the directionality has been fixed.
.. collapse:: Query: Rows where criteria is not met
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql
:language: sql
:start-after: good_directionality.txt
:end-before: final_function.txt
.. literalinclude:: ../scripts/basic/sql_function/good_directionality.txt
The final function using vehicle_penalized_net
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :force: :start-after: final_function.txt :end-before: using_fn1.txt
Try the function with a combination of the interesting places:
- |id_1| |place_1|
- |id_2| |place_2|
- |id_3| |place_3|
- |id_4| |place_4|
- |id_5| |place_5|
Names of the streets in the route
.. literalinclude:: ../scripts/basic/sql_function/sql_function.sql :language: sql :start-after: using_fn1.txt :end-before: vw_initial.txt
.. collapse:: Query results .. literalinclude:: ../scripts/basic/sql_function/using_fn1.txt





