All Products
Search
Document Center

PolarDB:Path model

Last Updated:Oct 17, 2024

This topic describes the details and usage of the path model.

Introduction

Overview

Path model is a graph structure described by nodes and edges, which is used for path planning on a road network, path search and planning in GPS navigation on an electronic map, and routing. The path model is fully compatible with pgRouting functions and allows you to smoothly migrate existing applications. Path data is a geometric network diagram that consists of edges and nodes. It is used to build road and traffic networks.

GanosBase Networking is a spatio-temporal engine extension of PostgreSQL (PolarDB for PostgreSQL). GanosBase Networking provides a set of functions and stored procedures to find the fastest, shortest, or even optimal path based on the cost model. GanosBase Networking provides the geospatial routing feature and supports multiple algorithms for path analysis and network analysis. The path analysis and network analysis are supported for databases.

Features

GanosBase Networking provides a set of functions for path planning and network analysis:

  • Johnson's algorithm.

  • Floyd-Warshall algorithm.

  • A* or bidirectional A* pathfinding algorithm.

  • Dijkstra's or bidirectional Dijkstra's pathfinding algorithm.

  • The traveling salesman algorithm.

  • Prim's algorithm.

  • Kruskal's algorithm.

  • K shortest path algorithm.

  • Flow analysis.

  • Topology operations.

  • Component operations.

  • Contractions.

  • Turn restriction shortest path algorithm.

Some functions also allow you to set the cost or cost matrix for calculation.

Scenarios

GanosBase Networking can be used in the following scenarios:

  • Route planning

    In industries such as logistics, delivery services, and taxi services, GanosBase Networking can be used to calculate the shortest or fastest path between two points for route planning and optimization. For non-geographic paths, such as the connections between Internet nodes, you can use GanosBase Networking to obtain a better network topology.

  • Geospatial analysis

    GanosBase Networking can be used to perform analysis based on road network, such as nearest neighbor search and service zone analysis. These analysis tasks allows you to understand the distribution and relationships of geospatial data for further decision-making and planning.

  • Traffic flow management

    GanosBase Networking can be used to analyze traffic volume data, predict congestion, and provide optimization recommendations. This helps optimize applications such as urban traffic management and intelligent transportation systems.

Components

Graphs

A graph is an ordered pair G = (V ,E).

  • V represents a set of vertices. An element of V is called a vertex or a node.

  • E ⊆ {( u, v ) | u , v ∈ V }.

Graphs are classified into various types such as undirected graphs, undirected simple graphs, directed graphs, and directed simple graphs.

In GanosBase, graphs are presented in the following ways:

  • Graphs with costs.

  • Graphs with costs and reverse costs.

Graphs can be specified as directed or undirected for calculation.

Graphs with costs

A graph with costs contains the following columns in the database:

Column

Description

id

The unique identifier of an edge.

source

The starting node of an edge.

target

The ending node of an edge.

cost

The cost of an edge, which is the weight of the edge from the starting node to the ending node.

Graphs with costs and reverse costs

A graph with costs and reverse costs contains the following columns in the database:

Column

Description

id

The unique identifier of an edge.

source

The starting node of an edge.

target

The ending node of an edge.

cost

The cost of an edge, which is the weight of the edge from the starting node to the ending node.

reverse_cost

The reverse cost of an edge, which is the weight of the edge from the ending node to the starting node.

Structure of function body

GanosBase Networking is compatible with the pgRouting functions. See the following structure of function body:

pgr_<name>(Inner_queries, Parameters, [ Optional_parameters ])

Take note of the following parameters:

  • Inner_queries: internal queries, which are the SQL statements for creating the data required by the function.

  • Parameters: parameters that are required by the function.

  • Optional_parameters: optional parameters. These parameters have default values when they are not specified.

A function may have different overloads. See the following most common overloads:

  • One-to-one: navigates from one starting node to one ending node.

  • One-to-many: navigates from one starting node to multiple ending nodes.

  • Many-to-one: navigates from multiple starting nodes to one ending node.

  • Many-to-many: navigates from multiple starting nodes to multiple ending nodes.

  • Combination: navigates from multiple starting nodes to multiple ending nodes that are specified by using tuples. Each tuple specifies a starting node and an ending node.

Data structure on which internal queries depend

You need to build internal queries to pass the graph structure to the function model. Internal queries can be classified into the following types by requirement type:

  • Edge SQL.

  • General edge SQL: applicable to Dijkstra's or bidirectional Dijkstra's pathfinding algorithm.

  • General edge SQL without ID: applicable to all-pairs algorithm.

  • General edge SQL with x and y values: applicable to A* or bidirectional A* pathfinding algorithm.

  • Combinations SQL.

  • Restrictions SQL.

  • Points SQL.

General edge SQL

Column

Type

Default value

Description

id

integer

No default value

The unique identifier of an edge.

source

integer

No default value

The starting node of an edge.

target

integer

No default value

The ending node of an edge.

cost

numeric

No default value

The cost of an edge, which is the weight of the edge from the starting node to the ending node.

reverse_cost

numeric

-1

The reverse cost of an edge, which is the weight of the edge from the ending node to the starting node.

When the value of this parameter is negative, edge \((target \rightarrow source)\) is not part of the graph.

General edge SQL without ID

Column

Type

Default value

Description

source

integer

No default value

The starting node of an edge.

target

integer

No default value

The ending node of an edge.

cost

numeric

No default value

The cost of an edge, which is the weight of the edge from the starting node to the ending node.

reverse_cost

numeric

-1

The reverse cost of an edge, which is the weight of the edge from the ending node to the starting node.

When the value of this parameter is negative, edge \((target \rightarrow source)\) is not part of the graph.

General edge SQL with x and y values

Column

Type

Default value

Description

source

integer

No default value

The starting node of an edge.

target

integer

No default value

The ending node of an edge.

cost

numeric

No default value

The cost of an edge, which is the weight of the edge from the starting node to the ending node.

When the value of this parameter is negative, edge \((source \rightarrow target)\) is not part of the graph.

reverse_cost

numeric

-1

The reverse cost of an edge, which is the weight of the edge from the ending node to the starting node.

When the value of this parameter is negative, edge \((target \rightarrow source)\) is not part of the graph.

x1

numeric

No default value

The x coordinate of the starting node of an edge.

y1

numeric

No default value

The y coordinate of the starting node of an edge.

x2

numeric

No default value

The x coordinate of the ending node of an edge.

y2

numeric

No default value

The y coordinate of the ending node of an edge.

Restrictions SQL

Column

Type

Default value

Description

path

integer array

No default value

A sequence of edge IDs that form a path that is not allowed to be taken.

cost

numeric

No default value

The cost of taking the path that is not allowed to be taken.

Points SQL

Column

Type

Default value

Description

pid

integer

auto value

The unique identifier of a node.

edge_id

integer

No default value

The unique identifier of the nearest edge of the node.

fraction

numeric

No default value

The relative position of the node on the edge. Valid value ranges from 0 to 1.

side

char

b

The position of the current node. Valid values: b (both sides), r (right), and l (left). The default value is b.

Data structure of result columns

Different columns are returned for different functions.

Result columns for a path

Column

Type

Description

seq

integer

The sequential value that starts from 1.

path_seq

integer

The relative position in the path. The sequential value starts from 1.

[start_vid]

big integer

The unique identifier of the starting vertex. The value is returned only when the query contains multiple starting vertices.

[end_vid]

big integer

The unique identifier of the ending vertex. The value is returned only when the query contains multiple ending vertices.

node

big integer

The identifier of a node in the path from "start_vid" to "end_vid".

edge

big integer

The identifier of the edge from the current node to the next node in the path sequence. -1 indicates the last node of the path.

cost

float

The cost from the current node to the next node in the path sequence.

agg_cost

float

The total cost from "start_vid" to "node".

The pgr_withPoints function returns the following columns:

Column

Type

Description

seq

integer

The sequential value starts from 1.

path_seq

integer

The relative position in the path. The sequential value starts from 1.

[start_vid]

big integer

The unique identifier of the starting vertex. The value is returned only when the query contains multiple starting vertices.

  • The identifier of a starting vertex is a positive number.

  • The identifier of an ending vertex is a negative number.

[end_vid]

big integer

The unique identifier of the ending vertex. The value is returned only when the query contains multiple ending vertices.

  • The identifier of an ending vertex is a positive number.

  • The identifier of a starting vertex is a negative number.

node

big integer

The identifier of a node in the path from "start_vid" to "end_vid".

  • The identifier of a vertex is a positive number.

  • The identifier of a node is a negative number.

edge

big integer

The identifier of the edge from the current node to the next node in the path sequence. -1 indicates the last node of the path.

cost

float

The cost from the current node to the next node in the path sequence.

agg_cost

float

The total cost from "start_vid" to "node". 0 indicates the first record of the path.

The pgr_dijkstraNear function returns the following columns:

Column

Type

Description

seq

integer

The sequential value that starts from 1.

path_seq

integer

The relative position in the path. The sequential value starts from 1.

start_vid

big integer

The unique identifier of the starting vertex of the current path.

end_vid

big integer

The unique identifier of the ending vertex of the current path.

node

big integer

The identifier of a node in the path from "start_vid" to "end_vid".

edge

big integer

The identifier of the edge from the current node to the next node in the path sequence. -1 indicates the last node of the path.

cost

float

The cost from the current node to the next node in the path sequence.

agg_cost

float

The total cost from "start_vid" to "node".

Result columns for multiple paths

The following columns are returned for functions that are selective for multiple paths:

Column

Type

Description

seq

integer

The sequential value that starts from 1.

path_id

integer

The unique identifier of the path.

The ID of the first path from "start_vid" to the "end_vid" is 1.

path_seq

integer

The relative position in the path. The sequential value starts from 1.

[start_vid]

big integer

The unique identifier of the starting vertex. The value is returned only when the query contains multiple ending vertices.

[end_vid]

big integer

The unique identifier of the ending vertex. The value is returned only when the query contains multiple ending vertices.

node

big integer

The identifier of a node in the path from "start_vid" to "end_vid".

edge

big integer

The identifier of the edge from the current node to the next node in the path sequence. -1 indicates the last node of the path.

cost

float

The cost from the current node to the next node in the path sequence.

agg_cost

float

The total cost from "start_vid" to "node".

The following columns are returned for functions that are not selective for multiple paths:

Column

Type

Description

seq

integer

The sequential value that starts from 1.

path_id

integer

The unique identifier of the path.

The ID of the first path from "start_vid" to the "end_vid" is 1.

path_seq

integer

The relative position in the path. The sequential value starts from 1.

start_vid

big integer

The unique identifier of the starting vertex.

end_vid

big integer

The unique identifier of the ending vertex.

node

big integer

The identifier of a node in the path from "start_vid" to "end_vid".

edge

big integer

The identifier of the edge from the current node to the next node in the path sequence. -1 indicates the last node of the path.

cost

float

The cost from the current node to the next node in the path sequence.

agg_cost

float

The total cost from "start_vid" to "node".

Result columns for cost functions

The following columns are returned for cost or cost matrix functions:

Column

Type

Description

start_vid

big integer

The unique identifier of the starting vertex.

end_vid

big integer

The unique identifier of the ending vertex.

agg_cost

float

The total cost of the path from "start_vid" to "end_vid".

Quick start

Overview

This section describes the basic usage of the GanosBase Networking engine, including extended creation, table creation, data insertion, attribute update, topology creation, and path query.

Syntax

  • Create the extension.

     CREATE Extension Ganos_Networking cascade;
    Note

    Install the extension into the public schema to avoid possible permission issues.

    CREATE extension Ganos_Networking WITH schema public cascade;
  • Create a table.

     CREATE TABLE edge_table (
         id BIGSERIAL,
         dir character varying,
         source BIGINT,
         target BIGINT,
         cost FLOAT,
         reverse_cost FLOAT,
         capacity BIGINT,
         reverse_capacity BIGINT,
         category_id INTEGER,
         reverse_category_id INTEGER,
         x1 FLOAT,
         y1 FLOAT,
         x2 FLOAT,
         y2 FLOAT,
         the_geom geometry
     );
  • Inserted records.

     INSERT INTO edge_table (
         category_id, reverse_category_id,
         cost, reverse_cost,
         capacity, reverse_capacity,
         x1, y1,
         x2, y2) VALUES
     (3, 1,    1,  1,  80, 130,   2,   0,    2, 1),
     (3, 2,   -1,  1,  -1, 100,   2,   1,    3, 1),
     (2, 1,   -1,  1,  -1, 130,   3,   1,    4, 1),
     (2, 4,    1,  1, 100,  50,   2,   1,    2, 2),
     (1, 4,    1, -1, 130,  -1,   3,   1,    3, 2),
     (4, 2,    1,  1,  50, 100,   0,   2,    1, 2),
     (4, 1,    1,  1,  50, 130,   1,   2,    2, 2),
     (2, 1,    1,  1, 100, 130,   2,   2,    3, 2),
     (1, 3,    1,  1, 130,  80,   3,   2,    4, 2),
     (1, 4,    1,  1, 130,  50,   2,   2,    2, 3),
     (1, 2,    1, -1, 130,  -1,   3,   2,    3, 3),
     (2, 3,    1, -1, 100,  -1,   2,   3,    3, 3),
     (2, 4,    1, -1, 100,  -1,   3,   3,    4, 3),
     (3, 1,    1,  1,  80, 130,   2,   3,    2, 4),
     (3, 4,    1,  1,  80,  50,   4,   2,    4, 3),
     (3, 3,    1,  1,  80,  80,   4,   1,    4, 2),
     (1, 2,    1,  1, 130, 100,   0.5, 3.5,  1.999999999999,3.5),
     (4, 1,    1,  1,  50, 130,   3.5, 2.3,  3.5,4);
  • Update table attributes.

     UPDATE edge_table SET the_geom = st_makeline(st_point(x1,y1),st_point(x2,y2)),
     dir = CASE WHEN (cost>0 AND reverse_cost>0) THEN 'B'   -- both ways
                WHEN (cost>0 AND reverse_cost<0) THEN 'FT'  -- direction of the LINESSTRING
                WHEN (cost<0 AND reverse_cost>0) THEN 'TF'  -- reverse direction of the LINESTRING
                ELSE '' END;
  • Create a topology.

     SELECT pgr_createTopology('edge_table',0.001);
  • Query the shortest path.

    -- Use the Dijkstra's pathfinding algorithm.
     SELECT * FROM pgr_dijkstra(
         'SELECT id, source, target, cost, reverse_cost FROM edge_table',
         2, 3
     );
    
      seq | path_seq | node | edge | cost | agg_cost
     -----+----------+------+------+------+----------
        1 |        1 |    2 |    4 |    1 |        0
        2 |        2 |    5 |    8 |    1 |        1
        3 |        3 |    6 |    9 |    1 |        2
        4 |        4 |    9 |   16 |    1 |        3
        5 |        5 |    4 |    3 |    1 |        4
        6 |        6 |    3 |   -1 |    0 |        5
     (6 rows)
    
     -- Use the A* pathfinding algorithm.
     SELECT * FROM pgr_astar(
         'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edge_table',
         2, 12,
         directed := false, heuristic := 2);
    
      seq | path_seq | node | edge | cost | agg_cost
     -----+----------+------+------+------+----------
        1 |        1 |    2 |    2 |    1 |        0
        2 |        2 |    3 |    3 |    1 |        1
        3 |        3 |    4 |   16 |    1 |        2
        4 |        4 |    9 |   15 |    1 |        3
        5 |        5 |   12 |   -1 |    0 |        4
     (5 rows)
    
     -- Use the turn restricted shortest path (TRSP) algorithm.
     SELECT * FROM pgr_trsp(
             'SELECT id::INTEGER, source::INTEGER, target::INTEGER, cost FROM edge_table',
             2, 7, false, false,
             'SELECT to_cost, target_id::int4,
             from_edge || coalesce('','' || via_path, '''') AS via_path
             FROM restrictions'
         );
    
      seq | id1 | id2 | cost
     -----+-----+-----+------
        0 |   2 |   4 |    1
        1 |   5 |  10 |    1
        2 |  10 |  12 |    1
        3 |  11 |  11 |    1
        4 |   6 |   8 |    1
        5 |   5 |   7 |    1
        6 |   8 |   6 |    1
        7 |   7 |  -1 |    0
     (8 rows)
  • Delete the extension (optional)

     Drop Extension Ganos_Networking cascade;

SQL statements

For more information about SQL statements, see the official pgRouting documentation.