Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a simple parser for route script and document script #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions osrmcpy/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Point to point routes

The point-to-point script is a simple command line script that needs the start and end point longitude and latitude coordinates of the route, and the path to the OSRM database:

In case you need help on how to use the script you can use the `-h/--help` flag:
```bash
$ python ./osrm_python3_route.py --help
usage: Point-to-Point route. [-h] -s START START -e END END -O OSRM

Computes a point to point route optimization.

optional arguments:
-h, --help show this help message and exit
-s START START, --start START START
Route start point as coordinates (longitude, latitude)
tuple.
-e END END, --end END END
Route end point as coordinates (longitude, latitude)
tuple.
-O OSRM, --osrm OSRM OSRM data base path.
```
## Example of usage:
```bash
python ./osrm_python3_route.py --start -8.9 53.3 --end -9.1 53.2 --osrm <Path_to_OSRM_database>
```
61 changes: 45 additions & 16 deletions osrmcpy/examples/osrm_python3_route.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
import sys
'''Script to calculate a point-to-point route.'''
import argparse
import pathlib

import pandas as pd

from osrmcpy import OSRM, Coordinate


# Example User Code
def parse_command():
"""Create a parser for this script."""
parser = argparse.ArgumentParser(
prog='Point-to-Point route.',
description='Computes a point to point route optimization.',
)

parser.add_argument(
'-s', '--start',
required=True,
nargs=2,
type=float,
help='Route start point as coordinates (longitude, latitude) tuple.',
)
parser.add_argument(
'-e', '--end',
required=True,
nargs=2,
type=float,
help='Route end point as coordinates (longitude, latitude) tuple.',
)
parser.add_argument(
'-O', '--osrm',
required=True,
type=pathlib.Path,
help='OSRM data base path.'
)

return parser


def main():
if '--help' in sys.argv or '-h' in sys.argv:
sys.exit('Usage: {} [OSRM data base path]'.format(sys.argv[0]))

osrm = OSRM(sys.argv[1].encode('utf-8') if len(sys.argv) >= 2 else None, True)

# Berlin
# start = Coordinate(id=None, longitude=13.14117, latitude=52.41445)
# end = Coordinate(id=None, longitude=13.55747, latitude=52.61437)
# Ireland
# start = Coordinate(id=None, longitude=-6.346509195699211, latitude=53.36407603954265)
# end = Coordinate(id=None, longitude=-6.35272995922493, latitude=53.283447477339756)
start = Coordinate(id=None, longitude=-6.278496849370723, latitude=53.321071624603135)
end = Coordinate(id=None, longitude=-6.462316552050708, latitude=53.31210678760515)
parser = parse_command()
args = parser.parse_args()

osrm_path = str(args.osrm.resolve())
route_start = args.start
route_end = args.end

osrm = OSRM(osrm_path.encode('utf-8'), True)

start = Coordinate(id=None, longitude=route_start[0], latitude=route_start[1])
end = Coordinate(id=None, longitude=route_end[0], latitude=route_end[1])

csv_path = "geometries_output.csv"
route = osrm.route([start, end], csv_path=csv_path.encode('utf-8'))
Expand Down