#!/usr/bin/env python

"""
author: Lu LIU
created at: 2014-07-25
Description:
    a sample of how to use pymmrouting to find multimodal optimal paths in
    transportation networks
"""

from pymmrouting.routeplanner import MultimodalRoutePlanner
from pymmrouting.inferenceengine import RoutingPlanInferer
from termcolor import colored
import datetime
import argparse
import json

parser = argparse.ArgumentParser()
parser.add_argument("ROUTING_OPTIONS_FILE",
                    help="User-defined options about travelling")
parser.add_argument("APP_CONFIG", default="config.json",
                    help="config for client application")
args = parser.parse_args()
ROUTING_OPTIONS_FILE = args.ROUTING_OPTIONS_FILE
CONFIG_FILE = args.APP_CONFIG

with open(ROUTING_OPTIONS_FILE) as f:
    routing_options = json.load(f)

# For multimodal routing, a bunch of options are necessary other than routing
# origin and destination. The acceptable format of multimodal routing options
# are stored in a JSON file.
print "Generating multimodal routing plans... ",
inferer = RoutingPlanInferer()
inferer.load_routing_options_from_file(ROUTING_OPTIONS_FILE)
# Routing plans of multimodal paths calculation can be generated by the
# inference engine with the routing options as inputs
routing_plans = inferer.generate_routing_plan()
print colored("done!", "green")

route_planner = MultimodalRoutePlanner()
# A multimodal network data model is necessary for multimodal path finding. It
# loads network dataset from external sources, e.g. PostgreSQL database, plain
# text file, etc.
print "Connecting to multimodal data source... ",
# In this sample, the multimodal graph data set is stored in PostgreSQL
# database

with (open(CONFIG_FILE, 'r')) as conf_file:
    DATABASE = json.load(conf_file)["datasource"]["database"]
ds_conn_str = "dbname = '" + str(DATABASE['database']) + \
    "' user = '" + str(DATABASE['username']) + \
    "' password = '" + str(DATABASE['password']) + "'"
route_planner.open_datasource(DATABASE['drivername'], ds_conn_str)
print colored("done!", "green")
# A multimodal network is assembled on-the-fly according to a concrete routing
# plan
print "Routing from " + \
    colored(str(routing_options['source']['value']['x']) + ',' +
            str(routing_options['source']['value']['y']), 'red') + " to " + \
    colored(str(routing_options['target']['value']['x']) + ',' +
            str(routing_options['target']['value']['y']), 'red')
print "Feasible routing plans are: "
for p in routing_plans:
    print str(routing_plans.index(p)+1) + ". " + p.description
rough_results = route_planner.batch_find_path(routing_plans)
final_results = route_planner.refine_results(rough_results)
print colored("Finish doing routing plan!", "green")
print "Final refined routing results are: "
for r in final_results:
    print "== " + str(final_results.index(r)+1) + ". " + r.description + " =="
    print "Total distance: ",
    print colored(str(r.length), "red"),
    print " meters"
    print "Total time (estimated): ",
    print colored(str(datetime.timedelta(minutes=r.time)), "red")
    print "Total walking distance: ",
    print colored(str(r.walking_length), "red"),
    print " meters"
    print "Total walking time (estimated): ",
    print colored(str(datetime.timedelta(minutes=r.walking_time)), "red")
    r.output_path_info()
route_planner.close_datasource()

@@ -539,20 +539,28 @@
 gument(%22
+-c%22, %22--
 APP
-_
+-
 CONFIG%22,

add a cmdline argument APP-CONFIG including database conn info, default to 'config.json' in current working dir