All Projects → herrkaefer → psycopgr

herrkaefer / psycopgr

Licence: MIT license
A Python wrapper of pgRouting for routing from nodes to nodes on real map.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to psycopgr

Valhalla
Open Source Routing Engine for OpenStreetMap
Stars: ✭ 1,794 (+7375%)
Mutual labels:  openstreetmap, routing, astar, dijkstra
LightOSM.jl
A Julia package for downloading and analysing geospatial data from OpenStreetMap APIs.
Stars: ✭ 32 (+33.33%)
Mutual labels:  openstreetmap, astar, dijkstra
Graphhopper
Open source routing engine for OpenStreetMap. Use it as Java library or standalone web server.
Stars: ✭ 3,457 (+14304.17%)
Mutual labels:  openstreetmap, astar, dijkstra
Baremaps
Custom vector tiles from OpenStreetMap and other data sources.
Stars: ✭ 100 (+316.67%)
Mutual labels:  openstreetmap, postgis
Osm2pgsql
OpenStreetMap data to PostgreSQL converter
Stars: ✭ 1,042 (+4241.67%)
Mutual labels:  openstreetmap, postgis
Openrouteservice R
🌐 R package to query openrouteservice.org
Stars: ✭ 57 (+137.5%)
Mutual labels:  openstreetmap, routing
Terrain Classic
World-wide CartoCSS port of Stamen's classic terrain style
Stars: ✭ 110 (+358.33%)
Mutual labels:  openstreetmap, postgis
Mapbox Directions Swift
Traffic-aware directions and map matching in Swift on iOS, macOS, tvOS, watchOS, and Linux
Stars: ✭ 115 (+379.17%)
Mutual labels:  openstreetmap, routing
Mapwarper
free open source public map georeferencer, georectifier and warper
Stars: ✭ 152 (+533.33%)
Mutual labels:  openstreetmap, postgis
Openrouteservice App
🚙 The open source route planner app with plenty of features.
Stars: ✭ 187 (+679.17%)
Mutual labels:  openstreetmap, routing
Docker Osm
A docker compose project to setup an OSM PostGIS database with automatic updates from OSM periodically
Stars: ✭ 172 (+616.67%)
Mutual labels:  openstreetmap, postgis
Atlasr
Atlasr is a truly open-source and free map browser.
Stars: ✭ 196 (+716.67%)
Mutual labels:  openstreetmap, routing
Openrouteservice
🌍 The open source route planner api with plenty of features.
Stars: ✭ 614 (+2458.33%)
Mutual labels:  openstreetmap, routing
Osrm Backend
Open Source Routing Machine - C++ backend
Stars: ✭ 4,716 (+19550%)
Mutual labels:  openstreetmap, routing
Dijkstra Cartography
Using Dijkstra's algorithm ("finding the shortest paths between nodes in a graph") to draw maps 🌍.
Stars: ✭ 1,112 (+4533.33%)
Mutual labels:  openstreetmap, dijkstra
Tasking Manager
Tasking Manager - The tool to team up for mapping in OpenStreetMap
Stars: ✭ 328 (+1266.67%)
Mutual labels:  openstreetmap, postgis
tilekiln
No description or website provided.
Stars: ✭ 3 (-87.5%)
Mutual labels:  openstreetmap, postgis
cloud-tileserver
Serve mapbox vectortiles via AWS stack
Stars: ✭ 48 (+100%)
Mutual labels:  openstreetmap, postgis
organicmaps
🍃 Organic Maps is a free Android & iOS offline maps app for travelers, tourists, hikers, and cyclists. It uses crowd-sourced OpenStreetMap data and is developed with love by MapsWithMe (MapsMe) founders and our community. No ads, no tracking, no data collection, no crapware. Your donations and positive reviews motivate and inspire our small team!
Stars: ✭ 3,689 (+15270.83%)
Mutual labels:  openstreetmap, routing
Libosmscout
Libosmscout is a C++ library for offline map rendering, routing and location lookup based on OpenStreetMap data
Stars: ✭ 159 (+562.5%)
Mutual labels:  openstreetmap, routing
    ____  _______  ___________  ____  ____ ______
   / __ \/ ___/ / / / ___/ __ \/ __ \/ __ `/ ___/
  / /_/ (__  ) /_/ / /__/ /_/ / /_/ / /_/ / /
 / .___/____/\__, /\___/\____/ .___/\__, /_/
/_/         /____/          /_/    /____/

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, create an PGRouting instance with database connection. Arguments are same as that psycopg2.connect() takes.

from psycopgr import PgrNode, PGRouting
pgr = PGRouting(dbname='mydb', user='user', password='secret',
                host='localhost', port='5432')

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].