forked from osm-fr/bano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outils_communs_import.py
347 lines (335 loc) · 10.4 KB
/
outils_communs_import.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
# coding: UTF-8
import psycopg2
import os.path
from pg_connexion import get_pgc
from pg_connexion import get_pgc_layers
class Dicts:
def __init__(self):
self.lettre_a_lettre = {}
self.fantoir = {}
self.code_fantoir_vers_nom_fantoir = {}
self.osm_insee = {}
self.abrev_type_voie = {}
self.expand_titres = {}
self.abrev_titres = {}
self.chiffres = []
self.chiffres_romains = []
self.mot_a_blanc = []
self.abrev_titres = {}
self.noms_voies = {}
self.ways_osm = {}
def load_lettre_a_lettre(self):
self.lettre_a_lettre = {'A':[u'Â',u'À'],
'C':[u'Ç'],
'E':[u'È',u'Ê',u'É',u'Ë'],
'I':[u'Ï',u'Î'],
'O':[u'Ö',u'Ô'],
'U':[u'Û',u'Ü']}
def load_fantoir(self,insee):
str_query = ''' SELECT code_insee||id_voie||cle_rivoli,
nature_voie||' '||libelle_voie
FROM fantoir_voie
WHERE code_insee = \''''+insee+'''\' AND
caractere_annul NOT IN ('O','Q');'''
cur_fantoir = pgc.cursor()
cur_fantoir.execute(str_query)
for c in cur_fantoir:
self.code_fantoir_vers_nom_fantoir[c[0]] = c[1]
cle = ' '.join(c[1].replace('-',' ').split())
cle = normalize(cle)
self.fantoir[cle] = c[0]
# self.add_voie('fantoir',cle)
def load_addr_from_fantoir(self):
for k in self.fantoir:
adresses.add_fantoir(k,self.fantoir[k],'FANTOIR')
def load_chiffres(self):
self.chiffres = [ ['0','ZERO'],
['1','UN'],
['2','DEUX'],
['3','TROIS'],
['4','QUATRE'],
['5','CINQ'],
['6','SIX'],
['7','SEPT'],
['8','HUIT'],
['9','NEUF'],
[' DIX ',' UNZERO '],
[' ONZE ',' UNUN '],
[' DOUZE ',' UNDEUX '],
[' TREIZE ',' UNTROIS '],
[' QUATORZE ',' UNQUATRE ']]
def load_mot_a_blanc(self):
self.mot_a_blanc = ['DE LA',
'DU',
'DES',
'LE',
'LA',
'LES',
'DE',
'D']
def load_expand_titres(self):
self.expand_titres = [['CPT','CAPITAINE'],
['GEN','GENERAL']]
def load_abrev_titres(self):
self.abrev_titres = [['MARECHAL','MAL'],
['PRESIDENT','PDT'],
['GENERAL','GAL'],
['COMMANDANT','CDT'],
['CAPITAINE','CAP'],
['LIEUTENANT','LT'],
['REGIMENT','REGT'],
['PROFESSEUR','PROF'],
['JEAN-BAPTISTE','J BTE'],
['SAINTE','STE'],
['SAINT','ST']]
def load_chiffres_romains(self):
self.chiffres_romains = { 'XXIII':'DEUXTROIS',
'XXII' :'DEUXDEUX',
'XXI' :'DEUXUN',
'XX' :'DEUXZERO',
'XIX' :'UNNEUF',
'XVIII':'UNHUIT',
'XVII' :'UNSEPT',
'XVI' :'UNSIX',
'XV' :'UNCINQ',
'XIV' :'UNQUATRE',
'XIII' :'UNTROIS',
'XII' :'UNDEUX',
'XI' :'UNUN',
'X' :'UNZERO',
'IX' :'NEUF',
'VIII' :'HUIT',
'VII' :'SEPT',
'VI' :'SIX',
'V' :'CINQ',
'IV' :'QUATRE',
'III' :'TROIS',
'II' :'DEUX',
'I' :'UN'}
def load_abrev_type_voie(self):
fn = os.path.join(os.path.dirname(__file__), 'abrev_type_voie.txt')
f = open(fn)
for l in f:
c = (l.splitlines()[0]).split('\t')
self.abrev_type_voie[c[0]] = c[1]
f.close()
def load_osm_insee(self):
finsee_path = os.path.join(os.path.dirname(__file__),'osm_id_ref_insee.csv')
finsee = open(finsee_path,'r')
for e in finsee:
c = (e.splitlines()[0]).split(',')
self.osm_insee[str(c[1])] = int(c[0])
finsee.close()
def load_all(self,code_insee_commune):
self.load_lettre_a_lettre()
self.load_abrev_type_voie()
self.load_expand_titres()
self.load_abrev_titres()
self.load_chiffres()
self.load_chiffres_romains()
self.load_mot_a_blanc()
self.load_osm_insee()
self.load_fantoir(code_insee_commune)
def add_voie(self,origine,nom):
cle = normalize(nom)
if not cle in self.noms_voies:
self.noms_voies[cle] = {}
self.noms_voies[cle][origine] = nom
class Adresse:
def __init__(self,node,num,voie,fantoir):
self.node = node
self.numero = num
self.voie = voie
self.fantoir = fantoir
class Adresses:
def __init__(self):
self.a = {}
def register(self,voie):
cle = normalize(voie)
if not cle in self.a:
self.a[cle] = {'numeros':{},'voies':{},'fantoirs':{}}
def add_fantoir(self,cle,fantoir,source):
self.register(cle)
if len(fantoir) == 10:
self.a[cle]['fantoirs'][source] = fantoir
else:
print(u'Code Fantoir non conforme : {:s}'.format(fantoir))
def add_voie(self,voie,source):
cle = normalize(voie)
self.a[cle]['voies'][source] = voie
def add_adresse(self,ad,source):
""" une adresses est considérée dans la commune si sans Fantoir ou avec un Fantoir de la commune"""
if (ad.fantoir == '' or (is_valid_fantoir(ad.fantoir) and ad.fantoir[0:5] == code_insee)) and is_valid_housenumber(ad.numero):
cle = normalize(ad.voie)
self.add_voie(ad.voie,source)
self.a[cle]['numeros'][ad.numero] = ad
if ad.fantoir != '':
self.a[cle]['fantoirs']['OSM'] = ad.fantoir
else:
print(u'adresse rejetée : {:s} {:s}'.format(ad.numero,ad.fantoir))
def get_cle_by_fantoir(self,fantoir):
cle = ''
for c in self.a:
if 'fantoirs' in self.a[c]:
if 'OSM' in self.a[c]['fantoirs']:
if self.a[c]['fantoirs']['OSM'] == fantoir:
cle = c
break
if 'FANTOIR' in self.a[c]['fantoirs']:
if self.a[c]['fantoirs']['FANTOIR'] == fantoir:
cle = c
break
return cle
def get_part_debut(s,nb_parts):
resp = ''
if get_nb_parts(s) > nb_parts:
resp = ' '.join(s.split()[0:nb_parts])
return resp
def get_nb_parts(s):
return len(s.split())
def replace_type_voie(s,nb):
sp = s.split()
spd = ' '.join(sp[0:nb])
spf = ' '.join(sp[nb:len(sp)])
s = dicts.abrev_type_voie[spd]+' '+spf
return s
def is_valid_housenumber(hsnr):
is_valid = True
if len(hsnr.encode('utf8')) > 11:
is_valid = False
return is_valid
def normalize(s):
# print(s)
# s = s.encode('ascii','ignore')
s = s.upper() # tout en majuscules
s = s.replace('-',' ') # separateur espace
s = s.replace('\'',' ') # separateur espace
s = s.replace('/',' ') # separateur espace
s = s.replace(':',' ') # separateur deux points
s = ' '.join(s.split()) # separateur : 1 espace
for l in iter(dicts.lettre_a_lettre):
for ll in dicts.lettre_a_lettre[l]:
s = s.replace(ll,l)
s = s.encode('ascii','ignore')
# type de voie
abrev_trouvee = False
p = 0
while (not abrev_trouvee) and p < 3:
p+= 1
if get_part_debut(s,p) in dicts.abrev_type_voie:
s = replace_type_voie(s,p)
abrev_trouvee = True
# ordinal
s = s.replace(' EME ','EME ')
# chiffres
for c in dicts.chiffres:
s = s.replace(c[0],c[1])
# articles
for c in dicts.mot_a_blanc:
s = s.replace(' '+c+' ',' ')
# titres, etc.
for r in dicts.abrev_titres:
s = s.replace(' '+r[0]+' ',' '+r[1]+' ')
# chiffres romains
sp = s.split()
if sp[-1] in dicts.chiffres_romains:
sp[-1] = dicts.chiffres_romains[sp[-1]]
s = ' '.join(sp)
return s
def add_fantoir_to_hsnr():
for v in adresses.a:
if v in dicts.fantoir:
adresses.a[v]['fantoirs']['FANTOIR'] = dicts.fantoir[v]
adresses.a[v]['voies']['FANTOIR'] = dicts.code_fantoir_vers_nom_fantoir[dicts.fantoir[v]]
else:
if 'OSM' in adresses.a[v]['fantoirs']:
adresses.a[v]['voies']['FANTOIR'] = dicts.code_fantoir_vers_nom_fantoir[adresses.a[v]['fantoirs']['OSM']]
def get_data_from_pg(data_type,insee):
fq = open('sql/{:s}.sql'.format(data_type),'rb')
str_query = fq.read().replace('__com__',insee)
fq.close()
# print(str_query)
# pgcl = get_pgcl()
cur = pgcl.cursor()
cur.execute(str_query)
res = cur.fetchall()
cur.close()
return res
def load_highways_from_pg_osm(insee):
data = get_data_from_pg('highway_insee',insee)
for lt in data:
l = list(lt)
name = l[0].decode('utf8')
cle = normalize(name)
fantoir = ''
if len(l)>1:
if l[1] != None and l[1][0:5] == insee:
fantoir = l[1]
adresses.add_fantoir(cle,l[1],'OSM')
if len(l)>2 and fantoir == '':
if l[2] != None and l[2][0:5] == insee:
fantoir = l[2]
adresses.add_fantoir(cle,l[2],'OSM')
if len(l)>3 and fantoir == '':
if l[3] != None and l[3][0:5] == insee:
fantoir = l[3]
adresses.add_fantoir(cle,l[3],'OSM')
adresses.register(name)
adresses.add_voie(name,'OSM')
def get_code_cadastre_from_insee(insee):
str_query = 'SELECT cadastre_com FROM code_cadastre WHERE insee_com = \'{:s}\';'.format(insee)
pgc = get_pgc()
cur = pgc.cursor()
cur.execute(str_query)
for c in cur:
code_cadastre = c[0]
return code_cadastre
def load_to_db(adresses,code_insee,source,code_cadastre,code_dept):
sload = 'DELETE FROM cumul_adresses_dev WHERE insee_com = \'{:s}\' AND source = \'{:s}\';\n'.format(code_insee,source)
cur_insert = pgc.cursor()
cur_insert.execute(sload)
nb_rec = 0
for v in adresses.a:
print(v)
print(adresses.a[v])
sload = 'INSERT INTO cumul_adresses_dev (geometrie,numero,voie_cadastre,voie_osm,fantoir,insee_com,cadastre_com,dept,code_postal,source) VALUES'
a_values = []
if not adresses.a[v]['numeros']:
continue
# street_name = v.title().encode('utf8')
street_name_cadastre = ''
street_name_osm = ''
street_name_fantoir = ''
cle_fantoir = get_best_fantoir(v)
if 'OSM' in adresses.a[v]['voies']:
street_name_osm = adresses.a[v]['voies']['OSM'].encode('utf8')
if 'FANTOIR' in adresses.a[v]['voies']:
street_name_fantoir = adresses.a[v]['voies']['FANTOIR'].encode('utf8')
if 'CADASTRE' in adresses.a[v]['voies']:
street_name_fantoir = adresses.a[v]['voies']['CADASTRE'].encode('utf8')
if street_name_osm == '' and street_name_fantoir == '':
print('****** voies muettes '+v)
# nodes
for num in adresses.a[v]['numeros']:
numadresse = adresses.a[v]['numeros'][num]
a_values.append('(ST_PointFromText(\'POINT({:s} {:s})\', 4326),\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\')'.format(numadresse.node.attribs['lon'],numadresse.node.attribs['lat'],numadresse.numero.encode('utf8'),street_name_fantoir.replace("'","''"),street_name_osm.replace("'","''"),cle_fantoir,code_insee,code_cadastre,code_dept,'',source))
nb_rec +=1
sload = sload+','.join(a_values)+';COMMIT;'
cur_insert.execute(sload)
return(nb_rec)
def get_best_fantoir(cle):
res = ''
if 'FANTOIR' in adresses.a[cle]['fantoirs']:
res = adresses.a[cle]['fantoirs']['FANTOIR']
if 'OSM' in adresses.a[cle]['fantoirs']:
res = adresses.a[cle]['fantoirs']['OSM']
return res
global pgc,pgcl
pgc = get_pgc()
pgcl = get_pgc_layers()
# global dicts
# dicts = Dicts()
# dicts.load_all(code_insee)
global adresses
adresses = Adresses()