Skip to main content

A Python wrapper of pgRouting for routing from nodes to nodes on real map.

Project description

    ____  _______  ___________  ____  ____ ______
   / __ \/ ___/ / / / ___/ __ \/ __ \/ __ `/ ___/
  / /_/ (__  ) /_/ / /__/ /_/ / /_/ / /_/ / /
 / .___/____/\__, /\___/\____/ .___/\__, /_/
/_/         /____/          /_/    /____/

PyPI PyPI - License PyPI - Python Version

psycopgr is a Python wrapper of pgRouting with one purpose:

Computing routes on real map for humans.

Tested with

  • Python 3.6.5
  • PostgreSQL 11.2
  • PostGIS 2.5.2
  • pgRouting 2.6.2
  • osm2pgrouting 2.3.6

Preparation

  • Install PostgreSQL, PostGIS, and pgRouting
  • Create database to store map data
  • Import OpenStreet map data into database

A step by step note can be found here.

Installation

pip install psycopgr

or

pipenv install psycopgr

Routing with Python!

First,

from psycopgr import PgrNode, PGRouting

Create an PGRouting instance with database connection:

pgr = PGRouting(database='mydb', user='user')

Adjust meta datas of tables including the edge table properies if they are different from the default (only the different properties needs to be set), e.g.:

pgr.set_meta_data(cost='cost_s', reverse_cost='reverse_cost_s', directed=true)

This is the default meta data:

{
    'table': 'ways',
    'id': 'gid',
    'source': 'source',
    'target': 'target',
    'cost': 'cost_s', # driving time in second
    'reverse_cost': 'reverse_cost_s', # reverse driving time in second
    'x1': 'x1',
    'y1': 'y1',
    'x2': 'x2',
    'y2': 'y2',
    'geometry': 'the_geom',
    'has_reverse_cost': True,
    'directed': True,
    'srid': 4326
}

Nodes are points on map which are represented by PgrNode namedtuple with geographic coordinates (longitude and latitude) rather than vague vertex id (vid) in the tables. PgrNodes is defined as:

PgrNode = namedtuple('PgrNode', ['id', 'lon', 'lat'])

in which id could be None or self-defined value, and lon and lat are double precision values.

For example:

nodes = [PgrNode(None, 116.30150, 40.05500),
         PgrNode(None, 116.36577, 40.00253),
         PgrNode(None, 116.30560, 39.95458),
         PgrNode(None, 116.46806, 39.99857)]

Now we can do routings! This is really straightforward:

# many-to-many
routings = pgr.get_routes(nodes, nodes, end_speed=5.0, pgx_file='r.pgx')
# one-to-one
routings = pgr.get_routes(nodes[0], nodes[1])
# one-to-many
routings = pgr.get_routes(nodes[0], nodes)
# many-to-one
routings = pgr.get_routes(nodes, node[2])
  • end_speed: speed from node to nearest vertices on ways in unit km/h.
  • gpx_file: set it to output paths to a gpx file.

The returned is a dict of dict: {(start_node, end_node): {'path': [PgrNode], 'cost': cost}

By default, cost is traveling time along the path in unit second. It depends on the means of columns of the edge table that you set as cost and reverse_cost. You can assign the relations by set_meta_data function.

We can also get only costs without detailed paths returned:

costs = pgr.get_costs(nodes, nodes)

The returned is also a dict: {(start_node, end_node): cost}

Low-level wrapper of pgRouting functions

psycopgr function pgRouting function
dijkstra pgr_dijkstra
dijkstra_cost pgr_dijkstraCost
astar pgr_astar

These are direct wrappings of pgRouting functions. For example, dijkstra takes vertex ids as input. This list may be extended in the future.

Tutorial

Here is a tutorial.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psycopgr-1.0.6.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

psycopgr-1.0.6-py2.py3-none-any.whl (8.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file psycopgr-1.0.6.tar.gz.

File metadata

  • Download URL: psycopgr-1.0.6.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for psycopgr-1.0.6.tar.gz
Algorithm Hash digest
SHA256 108e4d0235a79cc50416fdc1f575b9730221c82c0f91a169e2c008af0cd2f93c
MD5 53373279bbed2424083b4fcdb1c270f3
BLAKE2b-256 ee7207bc65cc0d3bd230490d4930bde635bb90f1ad89b0cab940967c634d2f7c

See more details on using hashes here.

File details

Details for the file psycopgr-1.0.6-py2.py3-none-any.whl.

File metadata

  • Download URL: psycopgr-1.0.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for psycopgr-1.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6b57cfd427627ad78c0f1bedc62d9b9202794719841a4a8c5faf13111ed3135d
MD5 bc0cdba8c074bfb17ffa320c6a4aa9fb
BLAKE2b-256 5f3b2ead8c7a6e27b912d15d91524fbf24bc587dba62942c190dec96afb20e1c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page