Skip to content

Commit

Permalink
Adding shortest path demo
Browse files Browse the repository at this point in the history
  • Loading branch information
smmaurer committed May 26, 2020
1 parent 3ebbaad commit 4d37b25
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/shortest_path_example.py
@@ -0,0 +1,55 @@
"""
This is a simple test of pandana functionality. If it runs with no errors
you should see output something like:
> python demos/example.py
Generating contraction hierarchies with 4 threads.
Setting CH node vector of size 1498
Setting CH edge vector of size 1702
[info src/contraction_hierarchies/src/libch.cpp:205] Range graph removed 1900 edges of 3404
. 10% . 20% . 30% . 40% . 50% . 60% . 70% . 80% . 90% . 100%
100%
Depending on whether your installed copy of pandana was built with OpenMP
support it may be run with multiple threads or only 1.
"""
from __future__ import print_function

import os.path
import sys

import numpy as np
import pandas as pd
import pandana.network as pdna

if len(sys.argv) > 1:
# allow test file to be passed as an argument
storef = sys.argv[1]
else:
# if no argument provided look for it in the test data
storef = os.path.normpath(os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'../pandana/tests/osm_sample.h5'))

if not os.path.isfile(storef):
raise IOError('Could not find test input file: {!r}'.format(storef))

print('Building network from file: {!r}'.format(storef))

store = pd.HDFStore(storef, "r")
nodes, edges = store.nodes, store.edges
net = pdna.Network(nodes.x, nodes.y, edges["from"], edges.to,
edges[["weight"]])
store.close()
print()

# Demonstrate shortest path code
print('Shortest path from first to last node:')
a = nodes.index[0]
print(a)
b = nodes.index[-1]
print(b)
# Note that the 'weight' is 1.0 for each link, so results aren't very interesting
print(net.shortest_path(a,b))
print(net.shortest_path_length(a,b,'weight'))

0 comments on commit 4d37b25

Please sign in to comment.