Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

# -*- coding: UTF-8 -*- 

# Copyright 2010-2014 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

 

""" 

DEPRECATED : use eesti.py instead! 

 

Imports file :file:`sihtnumbrid.csv` which you can obtain from 

`Estonian Post office 

<https://www.omniva.ee/ari/kiri/noudmiseni_sihtnumbrid>`_ and which is 

expected to have the following structure: 

 

  MAAKOND;VALD;LINN/ ALEV/ ALEVIK/ 

  KÜLA;TÄNAV/TALU;AADRESSILIIK;MAJAALGUS;MAJALOPP;SIHTNUMBER 

 

A copy of file :file:`sihtnumbrid.csv` was accidentally published here 

between June 2010 and May 2012, until we realized that this wasn't 

allowed due to copyright restrictions. 

 

You must download the file yourself and place it into your 

:attr:`project directory <ad.Site.project_dir>`. 

 

""" 

 

import csv 

import codecs 

 

from django.conf import settings 

 

from lino.utils.instantiator import Instantiator 

 

from lino.modlib.countries.models import PlaceTypes 

 

from lino.api import rt 

 

if True: 

 

    # http://www.python.org/doc/current/library/csv.html#module-csv 

    def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): 

        # csv.py doesn't do Unicode; encode temporarily as UTF-8: 

        csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), 

                                dialect=dialect, **kwargs) 

        for row in csv_reader: 

            # decode UTF-8 back to Unicode, cell by cell: 

            yield [unicode(cell, 'utf-8') for cell in row] 

 

    def utf_8_encoder(unicode_csv_data): 

        for line in unicode_csv_data: 

            yield line.encode('utf-8') 

 

 

CITY_TYPES = { 

    u'küla': PlaceTypes.village, 

    u'linn': PlaceTypes.town, 

    u'alev': PlaceTypes.borough, 

    u'alevik': PlaceTypes.smallborough, 

    u'asum': PlaceTypes.township, 

    #~ u'aiandusühistu' : PlaceTypes.quarter, 

    u'aiandusühistu': None,  # ignore them 

} 

 

 

#~ input_file = os.path.join( 

  #~ os.path.dirname(__file__), 

  #~ 'sihtnumbrid.csv') 

 

# input_file = os.path.join( 

#     settings.SITE.project_dir, 

#     'sihtnumbrid.csv') 

 

 

def objects(): 

    city = Instantiator('countries.Place', country='EE').build 

    input_file = rt.find_config_file('sihtnumbrid.csv') 

    settings.SITE.logger.info("Importing Estonian places from %s", input_file) 

    f = codecs.open(input_file, 'r', 'latin-1', 'replace') 

    #~ f = codecs.open(input_file,'r','utf-8','replace') 

    f.readline() 

    r = unicode_csv_reader(f, delimiter=';') 

    #~ r = UnicodeReader(f,delimiter=';') 

    # r.next() 

    maakonnad = dict() 

    mk_names_dict = dict() 

    #~ vallad = dict() 

    #~ laakid = dict() 

    #~ names = set() 

    for ln in r: 

        # print repr(ln) 

        if len(ln) > 2: 

            mk = maakonnad.get(ln[0]) 

            if mk is None: 

                mk = city(name=ln[0], type=PlaceTypes.county) 

                yield mk 

                #~ print "20120822 maakond", mk, mk.pk 

                maakonnad[ln[0]] = mk 

                mk_names_dict[ln[0]] = dict() 

 

            names = mk_names_dict[ln[0]] 

 

            if ln[1]: 

                vald = names.get(ln[1]) 

                if vald is None: 

                    #~ ct = CITY_TYPES[ln[4]] 

                    vald = city(name=ln[1], 

                                type=PlaceTypes.municipality, 

                                parent=mk, zip_code=ln[7]) 

                    yield vald 

                    #~ if ct != PlaceTypes.municipality: 

                        #~ print "20120822", vald, "expected municipality, found", ct 

                    #~ else: 

                    #~ print "20120822 vald", vald, vald.pk 

                    names[ln[1]] = vald 

                else: 

                    vald.zip_code = '' 

                    vald.save() 

 

            else: 

                vald = None 

 

            laak = names.get(ln[2]) 

            if laak is None: 

                #~ ct = CITY_TYPES.get(ln[4]) 

                ct = CITY_TYPES[ln[4]] 

                if ct is None: 

                    #~ print "20120822 ignored addressiliik", ln[4] 

                    continue 

                elif vald is None: 

                    laak = city(name=ln[2], type=ct, parent=mk, zip_code=ln[7]) 

                else: 

                    laak = city(name=ln[2], type=ct, 

                                parent=vald, zip_code=ln[7]) 

                yield laak 

                #~ print "20120822", laak.type, laak, laak.pk 

                names[ln[2]] = laak 

                #~ else: 

                    #~ print "20120822 pole vald ega Tallinn:", ln 

                    #~ names.add(ln[2]) 

            else: 

                laak.zip_code = '' 

                laak.save() 

    f.close() 

    # print len(names), "Estonian cities" 

    #~ for name in names: 

        #~ if name: 

            #~ yield city(name=name)