惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
博客园 - 司徒正美
A
About on SuperTechFans
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
H
Help Net Security
量子位
IT之家
IT之家

herrkaefer

"Vibe planning \u003e vibe coding" "Anything to Markdown" "Built anocus: anonymous commenting for static sites" "日记与小说 -- AI 续写小说欣赏" "Any-podcast: from newsletters to a podcast" "Made MicPipe: a simple voice input tool using ChatGPT dictation" "关于 Tools 和 Skills 的一点感想" "Realtime monitoring of ComEd hourly price" "Introducing SwiftEdgeTTS" "Thoughts on the philosophy of building AI-native apps" "jelly鼻屎" "等饭的人" "Use home assistant to motivate my kid to brush teeth" "Migrated Blog to Hugo and Cloudflare Pages" "Easy Aspen monitoring for Chicago parents" "Introducing HabitBuilder: A simple Telegram bot for habit tracking" "鼓捣" "Open folder or file with Sublime Text from Finder toolbar" "Python dev workflow on macOS" "Create new text file from Finder toolbar" "Uno reinvented for 3-year-old kids" "Uno变身儿童数字游戏" "自动转发Twitter到微博" "Handle annoying operations of objects in Realm DB" "Move Jekyll blog to Ubuntu VPS" "Introducing Mole" "Note taking without note taking app" "Deploy Python web application on Ubuntu server" "Setup Shadowsocks / VPN on Ubuntu Server" "Linode Notes - Basic Setup"
"pgRouting Notes"
"herrkaefer" · 2016-08-30 · via herrkaefer

Install PostGIS and pgRouting

On Mac

The following will install pgRouting and its dependencies including PostGIS, and PostgreSQL if not installed:

On Linux

(to add)

Install osm2pgrouting

osm2pgrouting is for importing OSM data into PostgreSQL database, and creating routable topology.

It is said that osm2pgrouting has limitation:

> Can’t process “large” files, continents, big countries, or very large states.

For large files, we can use osm2po.

Install via homebrew (Mac)




brew install osm2pgrouting

Note that a mapconfig.xml file is at: /usr/local/Cellar/osm2pgrouting/[version]/share/osm2pgrouting/mapconfig.xml

Manually install the lastest version

(The following notes are based on version 2.1. Now with homebrew you can already install the latest version.)

  1. Download code from repo

  2. Modify CMakeLists.txt to install app to /usr/local/bin and other files to /usr/local/share

  • line 5 —> set (SHARE_DIR “/usr/local/share/osm2pgrouting”)

  • line 36 —> RUNTIME DESTINATION “/usr/local/bin”

  1. Add the following headers to src/way.h (otherwise there will be errors)



#include



#include



#include
  1. Build and install



cmake -H. -Bbuild



cd build/



make



make install

Workflow

Prepare database

Create new database if needed:

For each database that postgis and pgrouting are used, we should install corresponding extensions:




psql routing -c "create extension postgis"



psql routing -c "create extension pgrouting"

For test of installation:




psql routing -c "SELECT postgis_full_version()"



psql routing -c "SELECT pgr_version()"

Import OpenStreetMap data

OSM map data can be downloaded in many places. We are interested in the OSM XML format.

To import map data to database:




osm2pgrouting --file beijing_china.osm \



--conf /usr/local/Cellar/osm2pgrouting/[version]/share/osm2pgrouting/mapconfig.xml \



--dbname routing \



--username [username] \



--clean

If you are adding data, do not use flag --clean.

We could browse the tables with psql or pgAdmin.

Notes for tables

Two main tables:

  • ways: stores the edges

  • ways_vertices_pgr: stores the end nodes of edges

Meanings of ways columns length, length_m, cost, reverse_cost, cost_s, reverse_cost_s. According to post here:

  • length is length of the segment in degree units

  • cost and reverse_cost is the length in degree units. (include the negative values for wrong way)

  • length_m is in meters (there is no cost_m or reverse_cost_m)

  • cost_s and reverse_cost_s is in time: seconds units (using the maxspeed value that is in km/hr transforming it to meters/second and using the length_m)

Do routings in SQL

Now we can calculate routes with pgRouting. For example, to calculate one-to-one shortest path:




SELECT * FROM pgr_dijkstra(



'SELECT gid as id, source, target, cost, reverse_cost FROM ways',



100, 200,



TRUE



);

pgRouting Manuals

Do routings in Python

I bet that You don’t want to write SQLs. Good news here if you like writing Python! I wrote a psycopgr Python module to encapsulate database connection (via psycopg2) and pgRouting callings. Here is a tutorial.