From 53dc7e3cac97a593312cbade39c38ec97610fd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Antol=C3=ADn?= Date: Wed, 15 Mar 2023 10:36:34 +0100 Subject: [PATCH] Create a simple parser for route script and document script --- osrmcpy/examples/README.md | 25 +++++++++++ osrmcpy/examples/osrm_python3_route.py | 61 +++++++++++++++++++------- 2 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 osrmcpy/examples/README.md diff --git a/osrmcpy/examples/README.md b/osrmcpy/examples/README.md new file mode 100644 index 0000000..46c9dc4 --- /dev/null +++ b/osrmcpy/examples/README.md @@ -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 +``` diff --git a/osrmcpy/examples/osrm_python3_route.py b/osrmcpy/examples/osrm_python3_route.py index ffd895d..a73086f 100644 --- a/osrmcpy/examples/osrm_python3_route.py +++ b/osrmcpy/examples/osrm_python3_route.py @@ -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'))