File: rpcity.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 (107 lines) | stat: -rw-r--r-- 3,270 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
# -*- coding: iso-8859-1 -*-
#
# Code for the City class
#
# Copyright (C) 1996-2003 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, rpdbase
from rpcountry import expand_country

invalidformat = 'rpcity.invalidformat'

class City:
    def __init__(self, data=None):
        if data:
            items = data[0].split(', ')
            self.city = items[0]
            if len(items) > 1:
                self.state = items[1]
            else:
                self.state = ''

            # Kludges
            if self.state == 'Quebec':
                self.state = 'Qubec'.decode('iso-8859-1')
            
            if len(data) > 4:
                self.country = data[4]
            else:
                self.country = rpdbase.autodetect_country(self.state)

            if data[1]:
                self.intersection = 1
            else:
                self.intersection = 0
            self.longitude = float(data[2])
            self.latitude = float(data[3])
            
            if self.state:
                self._printable = self.city + ', ' + self.state
            elif self.country:
                self._printable = self.city + ', ' + expand_country(self.country)
            else:
                self._printable = self.city
                
        else:
            self.city = self.state = self.country = ''
            self.longitude = self.latitude = 0.0
            self.intersection = 0
            self._printable = '(Null)'
        
        self.roads = []

    def update_info(self, city, state, country='us', longitude=0.0,
                    latitude=0.0, intersection=0):
        self.city = city
        self.state = state
        self.country = country
        self.intersection = intersection
        self.longitude = float(longitude)
        self.latitude = float(latitude)
        if state:
            self._printable = city + ', ' + state
        else:
            self._printable = city + ', ' + expand_country(country)

    def __repr__(self):
        interchange = ''
        if self.intersection: interchange = 'I'

        if self.state:
            city = self.city+u', '+self.state
        else:
            city = self.city

        city = city.encode('UTF-8')
        return '%s|%s|%.3f|%.3f|%s' % (city, interchange, self.longitude,
                                       self.latitude, self.country)
    def utf8(self):
        return self.__repr__()

    def __str__(self):
        return self._printable.encode('iso-8859-1')

    def __unicode__(self):
        return self._printable

    def __cmp__(self, other):
        return cmp(self.city, other.city) or cmp(self.state, other.state) or \
               cmp(self.country, other.country)
    
    def __hash__(self):
        return id(self)

    def __del__(self):
        self.roads = []

    def printable(self, include_country=0):
        if not self.state:
            return '%s, %s' % (self.city, expand_country(self.country))
        elif include_country:
            return '%s, %s' % (self._printable, expand_country(self.country))
        else:
            return self._printable
# End of City