Using PostgreSQL and PostGIS as a Spatial Da

Spatial Databases: Lecture 6
DT249, DT21, DT228 Semester 2
2015-16
Pat Browne
Dijkstra’s Shortest Path, Routing, Location,
PL/pgSQL
Outline
• We will look at Dijkstra’s Shortest Path
Algorithm.
• The issues in real world way-finding.
• The additional algorithms available in
pgRouting
• OGC Location Services (OpenLS): Core Services
Permanent Labels
Temporary Labels
V the set of vertices in the graph, xpaths are next steps
F set of vertices whose shortest-path lengths remains to be
calculated
V – F (permanent labels) set of vertices whose shortest-path
lengths have been calculated
Each iteration moves one vertex from F to (V – F)
Dijkstra’s Shortest Path Algorithm
• Dijkstra’s algorithm (Breath First Search (BFS))
for finding the shortest from a to v, involves
assigning labels to vertices. Let L(v) denote
the label of vertex v. At any point, some
vertices have temporary labels and the rest
have permanent labels, these are shown in
red in the following slides. We let T denote
the set of vertices having temporary labels.
Example Graph G
2
b
c
2
1
2
a
3
4
z
4
d
1
6
7
3
f
e
5
g
Initialize Graph G with all weights 

2
b
c

2
1

a
4
3
2

z

4
d
1
6
7
3
f
e

5
g


After First Iteration Graph G
For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)}
2 b
2
c
T := set of all temporary vertices

2
1
Labels
L(a)=min(,0)
L(f) = min{,0+1} sorted
L(b) = min{,0+2}
z

4
 d
f
e
6
7
3
1
3
4
2
a
1
Where w(v,x)
is distance
from v to x.
5
g

Chose (v= f ) ∊ T as minimum L(v)

After Second Iteration Graph G(*)
For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)}
Where w(v,x)
2
2 b

is distance
c
T := set of all temporary vertices
from v to x.
2
1
2
a
Labels
L(a)=min(,0)
1
L(f) = min{,0+1}
L(b) = min{,0+2} sorted
L(d) = min{,0+1+3}
L(g) = min{,0+1+5}
z

4
4 d
f
e
6
7
3
1
3
4
5
g
6
Chose (v= b ) ∊ T as minimum L(v)

After Third Iteration(*)
For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)}
Where w(v,x)
2
2 b
4
is distance
c
T := set of all temporary vertices
from v to x.
2
1
2
a
Labels
L(a)=min(,0)
1
4
L(f) = min{,0+1}
L(b) = min{,0+2}
L(c) = min{,0+2+2} sorted
L(d) = min{,0+1+3}
L(d) = min{4,0+2+2} --dif. path
L(e) = min{,0+2+4}
L(g) = min{,0+1+5}
3
4
z
6
4
d
1
6
7
3
f
e
5
g
6
Chose (v= c ) ∊ T as minimum L(v)

Fourth Iteration(*)
For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)}
2
Where w(v,x)
2 b
4
c
T := set of all temporary vertices
is distance
2
1 from v to x.
2
a
Labels
L(a)=min(,0)
4
1
L(f) = min{,0+1}
L(b) = min{,0+2}
L(c) = min{,0+2+2}
L(d) = min{,0+1+3} sorted
L(d) = min{4,0+2+2} dif. Path
L(e) = min{,0+2+4}
L(z) = min{,0+2+2+1}
L(g) = min{,0+1+5}
L(e) = min{6,0+2+2+3} dif. Path
3
4
z
6
4
d
e
6
7
3
f
1
5
g
6
Chose (v= d ) ∊ T as minimum L(v)
5
Fourth Iteration(*)
For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)}
Where w(v,x)
2
2 b
4
is distance
c
T := set of all temporary vertices
from v to x.
2
1
a
2
Labels
L(a)=min(,0)
L(f) = min{,0+1}
1
4d
L(b) = min{,0+2}
L(c) = min{,0+2+2}
L(d) = min{4,0+2+2}
3
L(z) = min{,0+2+2+1} sorted
L(e) = min{,0+2+4}
L(g) = min{,0+1+5}
f
L(e) = min{6,0+2+2+3} dif. Path
1
b c e
3
4
z
6
4
e
6
7
5
5
g
6
Chose (v= z ) ∊ T as minimum L(v)
Fourth Iteration(*)
For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)}
2
T := set of all temporary vertices
2
b
4
c
2
a
Where w(v,x)
is distance
from v to x.
1
2
1
Labels
4d
L(a)=min(,0)
L(f) = min{,0+1}
L(b) = min{,0+2}
3
L(c) = min{,0+2+2}
L(d) = min{,0+1+3}
L(d) = min{4,0+2+2} dif. Path
f
L(z) = min{,0+2+2+1}
1
L(e) = min{,0+2+4} sorted
L(g) = min{,0+1+5}
L(e) = min{6,0+2+2+3} dif. Path
3
4
z
6
4
e
6
7
5
5
g
6
Terminate zT
Path to Z shorter than any unexplored path
Another Example: 1
a.2
b
c 
3
2
2
0
1
a
z 
1
Labels
L(b) = min{,0+2}
L(d) = min{,0+1}
2
d
a,1
1
e

Choose v ∊ T with minimum L(v)=d
Another Example: 2
a,2
b
c 
3
2
2
0
1
a
z 
1
Labels
L(b) = min{,0+2}
L(d) = min{,0+1}
L(e)=min{,0+1+1}
2
d
a.1
1
e
d.2
Choose v ∊ T with minimum L(v)=b
Another Example: 3
a,2
b
3
c b,5
2
2
0
1
a
z 
1
Labels
L(b) = min{,0+2}
L(d) = min{,0+1}
L(e)=min{,0+1+1}
L(c)=min{,0+2+3}
L(e)=min{2,0+2+1} diff path
2
d
a.1
1
e
d.2
Choose v ∊ T with minimum L(v) =e
Another Example: 4
a,2
b
3
c b,5
2
2
0
1
a
z e,4
1
Labels
L(b) = min{,0+2}
L(d) = min{,0+1}
L(e)=min{,0+1+1}
L(c)=min{,0+2+3}
L(e)=min{2,0+2+1} diff path
L(z)=min{,0+1+1+2}
Each node is labelled with its
source node and the distance
from the start node
2
d
1
e
a.1
d.2
Shortest path: a,d,e,z
Choose v ∊ T with minimum L(v)=z
Find shortest path a-f
7
d
b
2
10
5
2
4
3
e
a
6
7
c
f
Find shortest path a-f

7
d
b

2
10
5
2
4
3
a
e
a(0,a)
6
7
c


f

Find shortest path a-f
7
d
b
2
10
5
2
4
3
e
a(0,a)
a
6
7
c
f
Labels
L(b) = min{,0+5}
L(c) = min{,0+6}
Each node is labelled with its
source node and the distance
from the start node
Find shortest path a-f
7
d
b
2
10
5
2
4
3
e
a(0,a)
a
6
7
c
f
Labels
L(b) = min{,0+5}
L(c) = min{,0+6}
L(d) = min{,0+5+7}
L(f) = min{,0+5+10}
L(e) = min{,0+5+2}
L(c) = min{,0+5+3}
Each node is labelled with its
source node and the distance
from the start node
Find shortest path a-f
7
d
b
2
10
5
2
4
3
e
a(0,a)
a
6
7
c
f
Labels
L(b) = min{,0+5}
L(c) = min{,0+6}
L(d) = min{,0+5+7}
L(f) = min{,0+5+10}
L(e) = min{,0+5+2}
L(c) = min{,0+5+3}
Each node is labelled with its
source node and the distance
from the start node
Find shortest path a-f
7
d
b
2
10
5
2
4
3
e
a(0,a)
a
6
7
c
f
Labels
L(b) = min{,0+5}
L(c) = min{,0+6}
L(d) = min{,0+5+7}
L(f) = min{,0+5+10}
L(e) = min{,0+5+2}
L(c) = min{,0+5+3}
L(e) = min{,0+6+7}
Each node is labelled with its
source node and the distance
from the start node
Find shortest path a-f
7
d
b
Minimum
Paths
2
10
5
2
4
3
a
f
e
a(0,a)
6
7
c
Shortest path: a,b,e,f
Length of path =11
Labels
L(b) = min{,0+5} –from a
L(c) = min{,0+6} –from a
L(d) = min{,0+5+7} –from b
L(f) = min{,0+5+10} – from b
L(e) = min{,0+5+2} –from b
L(c) = min{6,0+5+3} –from b
L(c) = min{6,0+5+2 +7} –from e
L(f) = min{, 0+5+2 +4} – from e
d not explored because (5+2+4)<12
Shortest Distance from A to all other vertices.
Shortest Distance from A to all other vertices.
Path from A to F
= <A,D,E,F>
Distance = 6
Step
Vertex
to be
marked
Distance to vertex
Unmarked vertices
A
B
C
D
E
F
A, B,C,D,E,F
0
A
0





B,C,D,E,F
1
B
0
2

3


C,D,E,F
2
D
0
2
3
3
6

C,E,F
3
C
0
2
3
3
5

E,F
4
E
0
2
3
3
5
8
F
5
F
0
2
3
3
5
6
None
6
None
0
2
3
3
5
6
Focus on sortest distance rather than path.
Shortest Path Example

v3 
5
v2
3
6
3
0
1
v1
v6 
2
1
6
3
v4

4
v5


v3 
5
v2
3
6
3
1
0 v1
v6 
2
1
6
3

v4
4
v5 
Find the shortest distance from v1 to all other vertices
The shortest distance to each vertex can be read off the bottom row.
Find path a to f
Yet another example
Each node is
labelled with total
distance and
predecessor node
Find the shortest path from a to f in the graph G
Yet another example
A
Find the shortest path from a to A to all other vertices the graph G
UML for Dijkstra
Computer Graph Representation 1
 Adjacency matrix
 Rows and columns
are labeled with
ordered vertices
write a 1 if there is an
edge between the
row vertex and the
column vertex and 0
if no edge exists
between them
v
w
x
y
v
0
1
0
1
w
1
0
1
1
x
0
1
0
1
y
1
1
1
0
Computer Graph Representation 2
e
f
g h
j
1
1
0 0
0
w 1
0
1 0
1
x
0
0
0 1
1
y
0
1
1 1
0
• Incidence matrix
– Label rows with vertices
– Label columns with edges
– 1 if an edge is incident to a
vertex, 0 otherwise
v
All paths example
Find the shortest distance from A to all other vertices
The shortest distance to each vertex can be read off the bottom row.
Issues when implementing networks and route
finding in GIS
• Real world networks (e.g. roads) tend to have many
intermediate non-branching vertices (e.g. polylines) which
contribute to large adjacency matrices.
• Networks often multi leveled (e.g walk, bus , train, bus,
ferry, car).
• In some cases shortest path problems are subject to
additional ‘thematic’ or ‘semantic’ constraints. (e.g. road
type or quality of transmission line) Constrained Shortest
Path Problem (CSPP).
Issues when implementing networks and route
finding in GIS(*)
• Basic Dijkstra-type algorithms provide inconsistent
run-times on real-world networks, principally
because such networks have very limited vertex
connectivity (their adjacency matrices are very
sparse)1.
• Most network algorithms are 2-D, route on map
maybe be in 3-D, (e.g. cross mountains).
Issues when implementing networks and route
finding in GIS
• The OGC Simple Feature (SFSQL) standard used in PostGIS
does not include any network representation, therefore an
additional Network ADT must be provided, which must
integrate with the existing standard. Ideally we would want to
avoid two distinct non-compatible ADTs. The existing OGC
data structures and algorithms for 2-dimensional objects
contain polygons, edges, and vertices and must be augmented
with connectivity information. Also, the OGC SFSQL must
integrate with the 1-D route finding algorithms and network
data structures. The Geographic Markup Language (GML1)
and topology extensions for PostGIS provide network support.
Issues when using networks and route
finding in GIS(*)
• The user must be able to understand how to
use the Network ADT (e.g. pgRouting).
• User must make sure that the data in the
appropriate format for the network ADT. This
may require manual or software conversion of
the data.
pgRouting: preparation
• A simple line graph needs to be transformed to include network topology,
with connective nodes and edges, by issuing this command in the SQL
Shell or pgAdmin SQL Query tool:
SELECT assign_vertex_id('edges', 0.001, 'the_geom', 'gid');
•
is a function included with the pgRouting extension. This is
explained in the tutorial.
assign_vertex_id()
• In general, we need to update a table by adding a numerical value to a
column that we previously created when issued the CREATE TABLE statement.
UPDATE edges SET length = length(the_geom);
• This creates a length field to hold the length value of network edges. This
can be a cost. We would also use the same statement to update a “cost”
column.
pgRouting
• Now we can use any of the GIS functions
provided by PostGIS and pgRouting.
• Dijkstra’s algorithm is a good single-source
routing algorithm provided by pgRouting.
Methods for Analysis
• We can run a routing query and get an answer
in Well-Known-Text (WKT):
SELECT gid, AsText(the_geom) AS the_geom
FROM dijkstra_sp('edges', 1, 8);
Starting node = 1
Destination node = 8
‘edges’ = the table containing the network
Results
• Dijkstra’s results expressed in WKT:
• The “gid” is synonymous with “FID” in ArcGIS, and “the_geom” in this case
is expressed in WKT. The numbers shown represent coordinate pairs that
make up points along a polyline.
• WKT is an OGC (Open Geospatial Consortium) standard.
• In order to view the result in OpenJump we usually include the additional
steps of making a result table or using an SQL view. Details are in the labs.
Dijkstra, A*, Shooting*
Anton Patrushev Shooting * includes turn restrictions, not covered on this course.
Shooting Star
• Shooting* is edge-based, so it goes from edge
to edge while A* and Dijkstra go from vertex
to vertex. Thus you need a data structure
which keeps all adjacent edges for every edge
a stored graph. It can be also done by making
a line graph out of a road network.
Shooting Star
• Then an edge-to-edge passage cost can be
computed as a special attribute of adjacent
edges structure or as a cost of the line graph.
These attributes represents any kind of
limitations or penalties for going from one
edge to another - such as turn restrictions in a
case of turns or any other kind of restrictions
like traffic lights.
Dijkstra, A*, Shooting*
Anton Patrushev (Orkney, Inc.)
Dijkstra, A*, Shooting*
Anton Patrushev (Orkney, Inc.)
A heuristic algorithm
• A heuristic algorithm is an algorithm that is
able to produce an acceptable solution to a
problem in many practical scenarios, but for
which there is no formal proof of its
correctness. The algorithm uses a “rule of
thumb” or “educated guess” to help find a
solution.
A-Star a heuristic algorithm
• A* takes uses the distance so far plus cost
heuristic.
• Two distinctions between Dijkstra and A*.
– A* uses a heuristic e.g. remaining distance.
– A* uses the distance already traveled and not
simply the local cost from the previously
expanded node. This in contrast to Dijkstra which
is a greedy algorithm. Dijkstra can be used when
the estimate to the goal is difficult or impossible
to calculate.
A-Star a heuristic algorithm
• A* takes the distance already traveled into
account and not simply the local cost from the
previously expanded node. For each node x
traversed, it maintains 3 values:
– g(x) : the actual shortest distance traveled from
initial node to current node x.
– h(x) : the estimated (or "heuristic") distance
from current node to goal
– f(x) : the sum of g(x) and h(x).
A-Star a heuristic algorithm
• The heuristics h(x) must not be an
overestimate of the distance to the goal. The
Euclidean (or straight line) distance is always
less than or equal to the road distance, hence
it is often used as a heuristic.
pgRouting data structure
For further details of the data pgRouting structures see
http://pgrouting.postlbs.org/wiki/WorkshopFOSS4G2007
http://pgrouting.postlbs.org/wiki
Computational complexity1
• Computational complexity theory deals with
the costs of computing a solution given
problem. Costs can be measured in terms of
time (e.g. number of iterations), space( e.g.
memory required), best, average, and worst
cases.
• CC can be applied to a wide range of
algorithms including shortest path (e.g.
Dijkstra and A*).
Complexity of
Dijkstra
MathWorld--A Wolfram Web Resource
• The time complexity for the Dijkstra algorithm on a
graph with n nodes and m edges is O(n2) for node
selection and O(m) for distance updates. It can find
the shortest paths from a source node s to all other
nodes in the graph. The complexity can be improved
significantly for sparse graphs (a graph in which the
actual number of edges is much less than the
possible number of edges).
OGC location based services
OGC OpenLS
• Standards with respect to LBSs have been set up by the
International Standard Organisation (ISO) and by the Open
Geospatial Consortium (OGC). The OGC has released the Open
Location Services (OpenLS - Open Geospatial Consortium
2005). OpenLS defines core services, their access and abstract
data types which form together a framework for a open
service platform, the so called GeoMobility server. The server
acts as application server and should process and answer core
service requests. Service requests to a GeoMobility server can
be send from a mobile user, from Internet users and also from
other application servers.
Applications of LBS
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Traffic Information: there is a traffic queue, so turn right.
Emergency Services: help, I'm having a heart attack!
Roadside Emergency; “help, my car has broken down!
Law Enforcement: what is the speed limit on this road
Classified Advertising; where are nearby sales featuring antiques
Object visualization: where is the historic parcel boundary
Underground Object Visualization: “where is the water main?”
Public Safety Vehicle Mgt: who is closest to that emergency?”
Location-Based Billing; free calls on your phone, in a location
Leisure Information: How do we get to the Club from here
Road Service Information: Where is the nearest petrol station
Directions: I'm lost, where is nearest Metro station
Vehicle Navigation; how do I get back to the Interstate from here?
Vehicle Theft Detection: my car has been stolen, where is it
Child Safety: tell me if my child strays beyond the neighborhood.
Location Utilities Service
• The OpenLS utilities specification provides two
services, geocoding and reverse geocoding, and an
abstract data type named as Address. The geocoder
service is a network-accessible service that
transforms a description of a location into a
normalized description of the location with point
geometry. The reverse geocoder service maps a given
position into a normalized description of a feature
location.
PL/pgSQL
•
•
•
•
•
•
•
•
PL/pgSQL is a loadable procedural language for the PostgreSQL database system.
The design goals of PL/pgSQL were to create a loadable procedural language that
can be used to create functions and trigger procedures,
adds control structures to the SQL language,
can perform complex computations,
inherits all user-defined types, functions, and operators,
can be defined to be trusted by the server,
a method of creating stored procedures or SQL-procedures
Functions created with PL/pgSQL can be used anywhere that built-in functions
could be used. For example, it is possible to create complex conditional
computation functions and later use them to define operators or use them in
index expressions.
Advantages of Using PL/pgSQL
• SQL is the language PostgreSQL and most other relational
databases use as query language. It's portable and easy to
learn. But every SQL statement must be executed individually
by the database server.
• That means that client application must send each query to
the database server, wait for it to be processed, receive and
process the results, do some computation, then send further
queries to the server. All this incurs inter-process
communication and will also incur network overhead if your
client is on a different machine than the database server.
Advantages of Using PL/pgSQL
• With PL/pgSQL you can group a block of computation and a series of
queries inside the database server, thus having the power of a procedural
language and the ease of use of SQL, but with considerable savings of
client/server communication overhead.
• Extra round trips between client and server are eliminated
• Intermediate results that the client does not need do not have to be
marshalled or transferred between server and client
• Multiple rounds of query parsing can be avoided
• This can result in a considerable performance increase as compared to an
application that does not use stored functions.
• Also, with PL/pgSQL you can use all the data types, operators and
functions of SQL.
Further reading
• Paper
• Navigation Systems: A Spatial Database Perspective by Shashi
Shekhar, Ranga Raju Vatsavai, Xiaobin Ma, and Jin Soung Yoo.
http://www-users.cs.umn.edu/~shekhar/research/lbsChapter.pdf
• Book: Location-Based Services: by Schiller & Voisard
• Web
•
•
•
•
•
•
http://www.opengeospatial.org/standards/ols
http://pgrouting.postlbs.org/
http://www.openrouteservice.org/
http://www.davidgis.fr/documentation/pgrouting1.02/http://www.utdallas.edu/~ama054000/rt_tutorial.html
http://www.directionsmag.com/article.php?article_id=2876&trv=1
http://www.geographie.uni-bonn.de/karto/