File: rproute.py

package info (click to toggle)
routeplanner 0.17
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,836 kB
  • ctags: 264
  • sloc: python: 3,282; makefile: 58; sh: 40
file content (121 lines) | stat: -rw-r--r-- 4,011 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Code for the Route class
#
# Copyright (C) 1996-2001 Chris Lawrence
# This file may be freely distributed under the terms of the RoutePlanner
# license.  A copy should appear as 'LICENSE' in the archive that this
# file was included in.

#import string
import rpunits, bisect
from rpunits import UNITS_METRIC

class Route:
    def __init__(self, data=None, cities=None):
        if data and cities:
            citynames = data[0:2]

            self.exits = data[5:7]
            # Lower city name comes first; this will help provide a
            # uniform file format for patching
            if citynames[0].lower() > citynames[1].lower():
                citynames.reverse()
                self.exits.reverse()
            
            stuff = data[2].split()
            self.distance = rpunits.Distance(float(stuff[0]), stuff[1])
            self.i_distance = float(self.distance.AsUnit(UNITS_METRIC))
            self.speed = int(data[3])
            self.speedcode = int(data[4])

            self.time = None
            if self.speedcode == 0:
                self.time = 60.0 * stuff[0] / self.speed
            elif self.speedcode == 1:
                self.time = self.speed
            
            if self.speedcode == 10:
                self.speedcode = 2
                if stuff[1] == UNITS_METRIC:
                    self.speed = -16
                else:
                    self.speed = -10

            self.name = data[7]
            self.flagmap = {}
            self.flags = ''
            if len(data) > 8:
                # Flag extension: either a string or dictionary
                if isinstance(data[8], basestring):
                    self.flags = data[8]
                    self.make_flagmap()
                elif isinstance(data[8], dict):
                    self.flagmap = data[8]
                    self.make_flag_string()
            else:
                self.flags = ''
                self.flagmap = {}
            
            self.city = [ cities[citynames[0]][0], cities[citynames[1]][0] ]

            bisect.insort(self.city[0].roads, (self, 0, self.city[1]) )
            bisect.insort(self.city[1].roads, (self, 1, self.city[0]) )
        else:
            self.city = [None]*2
            self.exits = [None]*2
            self.distance = self.speed = self.speedcode = \
                            self.name = self.flags = None
            self._distance = 0.0

    def __repr__(self):
        x = (unicode(self.city[0]).encode('UTF-8'))+'|'+\
            (unicode(self.city[1]).encode('UTF-8'))+'|'+\
            str(self.distance)+'|'+str(self.speed)+'|'+\
            str(self.speedcode)+'|'+self.exits[0]+'|'+self.exits[1]+'|'+\
            self.name
        if self.flags:
            return x+"|"+self.flags
        else:
            return x

    def utf8(self):
        return self.__repr__()

    def __str__(self):
        return self.name

    def __cmp__(self, other):
        return ( cmp(self.city[0], other.city[0]) or
                 cmp(self.city[1], other.city[1]) or
                 cmp(self.distance, other.distance) or
                 cmp(self.name.lower(), other.name.lower()) )
    
    def __hash__(self):
        return id(self)

    def __del__(self):
        try:
            if self.city:
                del self.city
        except:
            pass

    def make_flagmap(self):
        self.flagmap = {}
        bits = self.flags.split(',')
        for bit in bits:
            if bit in ('ferry', 'toll', 'scenic'):
                self.flagmap[bit] = True
            elif bit.startswith('height='):
                # Height restriction
                self.flagmap['height'] = float(bit[7:])

    def make_flag_string(self):
        flags = []
        for flag, value in self.flagmap.iteritems():
            if flag != 'height':
                if value:
                    flags.append(flag)
            else:
                flags.append('%s=%.2f' % value)
        self.flags = ','.join(flags)
# End of Route