#!/usr/bin/env python
"""
@file    findUTurns.py
@author  Michael.Behrisch@dlr.de
@date    2008-04-16
@version $Id: findUTurns.py 5407 2008-04-16 11:05:59Z behrisch $

Determines the number of U turns in a route file by comparing 
the edge ids of successive edges for "negation". This works with
most Elmar (Navteq) imported nets. Optional it can also remove
U turns at the start and the end of a route and output the 
modified routes.

Copyright (C) 2008 DLR/TS, Germany
All rights reserved
"""
import os, sys, re

from xml.sax import make_parser, handler
from optparse import OptionParser

class RouteReader(handler.ContentHandler):

    def __init__(self):
        self._vehAttrs = None
        self._routeString = ''
        self._routeCount = 0
        self._uTurnCount = 0
        self._uTurnFirst = 0
        self._uTurnLast = 0
        self._short = 0

    def startElement(self, name, attrs):
        if name == 'vehicle':
            self._vehAttrs = attrs
        elif name == 'route':
            self._routeString = ''
        elif name == 'routes':
            if options.repair:
                print '<routes>'
            print >> sys.stderr, "   Route  UTurn UTurnFirst UTurnLast  Short"
        else:
            if options.repair:
                print '<' + name,
                for key in attrs.keys():
                    print '%s="%s"' % (key, attrs[key]),
                print '/>'

    def characters(self, content):
        self._routeString += content

    def endElement(self, name):
        if name == 'route':
            self._routeCount += 1
            lastEdge = ' '
            route = self._routeString.split()
            routeStart = 0
            routeEnd = len(route)
            if len(route) == 2:
                self._short += 1
            for index, edge in enumerate(route):
                if (lastEdge[0] == '-' and edge == lastEdge[1:]) or\
                   (edge[0] == '-' and lastEdge == edge[1:]):
                    self._uTurnCount += 1
                    if index == 1:
                        self._uTurnFirst += 1
                        routeStart = 1
                    if index == len(route) - 1:
                        self._uTurnLast += 1
                        routeEnd -= 1
                lastEdge = edge
            if self._routeCount % 10000 == 0:
                print >> sys.stderr, "%8i %6i %10i %9i %6i"\
                                     % (self._routeCount, self._uTurnCount,
                                        self._uTurnFirst, self._uTurnLast,
                                        self._short), "\r",
            if routeEnd - routeStart > 1:
                if options.repair:
                    print '<vehicle',
                    for key in self._vehAttrs.keys():
                        print '%s="%s"' % (key, self._vehAttrs[key]),
                    print '>'
                    print '   <route>%s</route>' % ' '.join(route[routeStart:routeEnd])
                    print '</vehicle>'
        elif name == 'routes':
            if options.repair:
                print '</routes>'
            print >> sys.stderr, "%8i %6i %10i %9i %6i"\
                                 % (self._routeCount, self._uTurnCount,
                                    self._uTurnFirst, self._uTurnLast,
                                    self._short)

optParser = OptionParser(usage="usage: %prog [options] <routefile>+")
optParser.add_option("-r", "--repair", action="store_true", dest="repair",
                     default=False, help="remove U turns at start and end of the route")
(options, args) = optParser.parse_args()
if len(args) == 0:
    optParser.print_help()
    sys.exit()
for f in args:
    parser = make_parser()
    parser.setContentHandler(RouteReader())
    parser.parse(f)
