pgRoutingを試してみた

pgRoutingをはじめて使ってみた」を、そのまま試させて頂いた

C:\Users\ebata>docker start -a ebata_db_1 ← pgRoutingで作ったDockerのDB
C:\Users\ebata>docker container exec -it ebata_db_1 bash ← ログインできる
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
routing | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# CREATE DATABASE d1; ← d1というログインできる
CREATE DATABASE
postgres=# \c d1 ← d1に接続
You are now connected to database "d1" as user "postgres".
d1=# CREATE EXTENSION postgis;
CREATE EXTENSION
d1=# CREATE EXTENSION pgrouting;
CREATE EXTENSION
d1=# CREATE TABLE edges (id serial, source int, target int, cost int); ← テーブルを作成する
CREATE TABLE
d1=# INSERT INTO edges (source, target, cost) VALUES (1, 2, 7), (1, 3, 9), (1, 6, 14), (2, 3, 10), (2, 4, 15), (3, 4, 11), (3, 6, 2), (4, 5, 6), (5, 6, 9); ← 端点ノード1、端点ノード2、コスト
INSERT 0 9
d1=# SELECT seq, node, edge, cost FROM pgr_dijkstra('SELECT id, source, target, cost FROM edges', 1, 5, directed:=false); ← ノード1からノード5までの最短コストを算出する
seq | node | edge | cost
-----+------+------+------
1 | 1 | 2 | 9
2 | 3 | 7 | 2
3 | 6 | 9 | 9
4 | 5 | -1 | 0
(4 rows)
1->3->6->5の最短コストが出てくる。

2020/05,江端さんの技術メモ

Posted by ebata