-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcle.py
205 lines (199 loc) · 6.25 KB
/
cle.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: ts=4 sw=4 et
import csv
from locale import atoi
salph = "ABCDEFGHJKLMNPRSTUVWXYZ"
# without I. O and Q
alph = "0123456789ABCDEFGHJKLMNPRSTUVWXYZ"
# full
# alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
insee2dir = dict()
with open("insee2dir.csv", "r") as insee2dirf:
r = csv.reader(insee2dirf, delimiter=";")
for row in r:
insee2dir[row[0]] = atoi(row[1])
def compute_cle(code):
# print("input={}".format(code))
if code[7:9] == "2A":
dpt = 3
elif code[7:9] == "2B":
dpt = 4
else:
dpt = atoi(code[7:9])
ins = atoi(code[9:12])
direction = insee2dir.get(code[7:12], 0)
comm = dpt * 10000 + direction * 1000 + ins
riv = code[12:16]
if riv == " ":
ordre = comm % 23
else:
numv = atoi(riv[1:4])
if riv[0] == "I":
code = alph.index("A")
elif riv[0] == "O":
code = alph.index("B")
elif riv[0] == "Q":
code = alph.index("C")
else:
code = alph.index(riv[0])
# print('numv={}, riv[0] = {} -> index = {}'.format(numv, riv[0], code))
ordre = ((19 * comm) + (11 * code) + numv) % 23
# print("dpt={}, ins={}, commune={}, code riv='{}' -> ordre={}".format(dpt,ins,comm, riv, ordre))
# print("salph[0]={}".format(salph[0]))
return salph[ordre]
tests = (
{
"code_topo": "991008415258 ",
"code_fantoir": "150258 Z",
}, # dept 15: commune de VIC-SUR-CERE
{
"code_topo": "991009313002 ",
"code_fantoir": "131002 U",
}, # dept 13: commune d'ALLAUCH (direction 1)
{
"code_topo": "9910084014570690",
"code_fantoir": "0104570690K",
}, # dept 01: RTE DE NAMARY
{
"code_topo": "9910093132101299",
"code_fantoir": "1312101299J",
}, # dept 13: IMP BONNAUD (direction 2)
{
"code_topo": "991008415001A030",
"code_fantoir": "150001A030Z",
}, # dept 15: CITEFROMENTS
{
"code_topo": "99100942A092B180",
"code_fantoir": "2A0092B180T",
}, # dept 2A: LAMAGHIONE
{
"code_topo": "99100942A142B221",
"code_fantoir": "2A0142B221V",
}, # dept 2A: FILAGHIONE
{
"code_topo": "99100942B002 ",
"code_fantoir": "2B0002 F",
}, # dept 2B: commune d'AGHIONE
{
"code_topo": "99100942B002B001",
"code_fantoir": "2B0002B001L",
}, # dept 2B: AGHIONE
{
"code_topo": "991002814061I975",
"code_fantoir": "140061I975U",
}, # dept 14: LES FARCIERES
{
"code_topo": "991005249331I874",
"code_fantoir": "490331I874W",
}, # dept 49: GRANDE FROMENTRIE
{
"code_topo": "991002850041I996",
"code_fantoir": "500041I996L",
}, # dept 50: LA BISSONNERIE
{
"code_topo": "991002850041J998",
"code_fantoir": "500041J998J",
}, # dept 50: LA RIGNOLETTERIE VAUVILLE
{
"code_topo": "991002814061J030",
"code_fantoir": "140061J030M",
}, # dept 14: LA GRANDE BINOTIERE
{
"code_topo": "991002850041K421",
"code_fantoir": "500041K421U",
}, # dept 50: LE SEMAPHORE DE JARDEHEU
{
"code_topo": "991005249092L222",
"code_fantoir": "490092L222W",
}, # dept 49: CHANTELEVENT
{
"code_topo": "991005329024M008",
"code_fantoir": "290024M008L",
}, # dept 29: KERGALET VRAZ
{
"code_topo": "991005356026N001",
"code_fantoir": "560026N001U",
}, # dept 56: BAS DE KERFAVEN
{
"code_topo": "991005322185O090",
"code_fantoir": "220185O090D",
}, # dept 22: LES PALMIERS
{
"code_topo": "991007540185O068",
"code_fantoir": "400185O068W",
}, # dept 40: LAHITTE
{
"code_topo": "991002445252O912",
"code_fantoir": "450252O912F",
}, # dept 45: PCE ST EXUPERY
{
"code_topo": "991005285194P759",
"code_fantoir": "850194P759E",
}, # dept 85: LE DEGAST
{
"code_topo": "991005322093P710",
"code_fantoir": "220093P710W",
}, # dept 22: LES BERGEONS MESLIN
{
"code_topo": "991005322093Q032",
"code_fantoir": "220093Q032C",
}, # dept 22: CLOTURE JARNOT
{
"code_topo": "991005285194Q844",
"code_fantoir": "850194Q844R",
}, # dept 85: PARC D ACTILONNE
{
"code_topo": "991005285194Q843",
"code_fantoir": "850194Q843P",
}, # dept 85: LA FONSSAUCE
{
"code_topo": "991005285202R493",
"code_fantoir": "850202R493T",
}, # dept 85: LES GROIES
{
"code_topo": "991007519130S110",
"code_fantoir": "190130S110S",
}, # dept 19: ROTONDES FONFREYDE
{
"code_topo": "991003262126T349",
"code_fantoir": "620126T349R",
}, # dept 62: L ESTRACELLE
{
"code_topo": "991009313103V029",
"code_fantoir": "132103V029F",
}, # dept 13: RES VERT BOCAGE (direction 2)
{
"code_topo": "991009313001V110",
"code_fantoir": "132001V110M",
}, # dept 13: CHATEAU DE LUYNES NORD (direction 2)
{
"code_topo": "991002827467W109",
"code_fantoir": "270467W109X",
}, # dept 27: LA SENTE MAILLERAIE
{
"code_topo": "991008401053X022",
"code_fantoir": "010053X022Z",
}, # dept 01: PKG DES BONS ENFANTS
{
"code_topo": "991008401288X006",
"code_fantoir": "010288X006K",
}, # dept 01: PKG DE L'ETRAZ
{
"code_topo": "991008415014X004",
"code_fantoir": "150014X004D",
}, # dept 15: CANAL DE PEYROLLES
# ces 2 voies avec Y comme première lettre du code rivoli ne se retrouvent plus avec le meme code rivoli dans TOPO -> changement de zone ?
# fantoir: 160102Y004ECITELE CLOS GIRARDIN
# topo: 991007516102102514;CITELE CLOS GIRARDIN
# fantoir: 110069Y046K DE STE MARIE
# topo: 991007611069423814;CHE DE STE MARIE
)
if __name__ == "__main__":
for t in tests:
cle = compute_cle(t["code_topo"])
if t["code_fantoir"][-1] != cle:
print(f"{t['code_topo']} expects {t['code_fantoir'][-1]} returns {cle}")
else:
print(f"{t['code_topo']} key matches")
# assert compute_cle(t['code_topo']) == t['code_fantoir'][-1]