-
Notifications
You must be signed in to change notification settings - Fork 23
/
recog.js
130 lines (101 loc) · 3.83 KB
/
recog.js
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
const {groupBy, keyBy, maxBy, minBy, uniq, flattenDeep} = require('lodash')
const historiqueCommunes = require('@etalab/decoupage-administratif/data/historique-communes.json')
const arrondissementsMunicipaux = require('@etalab/decoupage-administratif/data/communes.json')
.filter(c => c.type === 'arrondissement-municipal')
.map(c => ({code: c.code, nom: c.nom, type: 'COM'}))
function connectGraph(historiqueCommunes) {
const byId = keyBy(historiqueCommunes, 'id')
historiqueCommunes.forEach(h => {
if (h.successeur) {
h.successeur = byId[h.successeur]
}
if (h.predecesseur) {
h.predecesseur = byId[h.predecesseur]
}
if (h.pole) {
h.pole = byId[h.pole]
}
if (h.membres) {
h.membres = h.membres.map(m => byId[m])
}
})
}
connectGraph(historiqueCommunes)
const byCodeCommune = groupBy(historiqueCommunes.concat(arrondissementsMunicipaux), h => `${h.type}${h.code}`)
function getCodeDepartement(codeCommune) {
return codeCommune.startsWith('97') ? codeCommune.substr(0, 3) : codeCommune.substr(0, 2)
}
function isValidAt(communeEntry, dateValeur) {
return (!communeEntry.dateDebut || communeEntry.dateDebut <= dateValeur) && (!communeEntry.dateFin || communeEntry.dateFin > dateValeur)
}
const COM = {
97123: {code: '97123', nom: 'Saint-Barthelemy', type: 'COM'},
97127: {code: '97127', nom: 'Saint-Martin', type: 'COM'}
}
function getCodesMembres(commune) {
return uniq([
commune.code,
...flattenDeep((commune.membres || []).map(getCodesMembres)),
...flattenDeep(commune.predecesseur ? getCodesMembres(commune.predecesseur) : [commune.code])
])
}
function getCommuneFromCadastre(codeCommune, prefixeSection) {
if (codeCommune in COM) {
return COM[codeCommune]
}
if (prefixeSection === '000') {
return getOlderCommune(codeCommune)
}
const code1 = `${codeCommune.substr(0, 2)}${prefixeSection}`
const commune1 = getOlderCommune(code1)
if (commune1) {
return commune1
}
const codes2 = getCodesMembres(getMostRecentCommune(codeCommune))
return getOlderCommune(codes2.find(c => c.endsWith(prefixeSection)))
}
function getMostRecentCommune(codeCommune) {
return maxBy(byCodeCommune[`COM${codeCommune}`], c => c.dateFin || '9999-99-99')
}
function getOlderCommune(codeCommune) {
return minBy(byCodeCommune[`COM${codeCommune}`], c => c.dateDebut || '0000-00-00')
}
function getCommune(codeCommune, dateValeur, types = ['COM', 'COMD', 'COMA', 'COMP']) {
if (codeCommune in COM) {
return COM[codeCommune]
}
if (types.length === 0) {
throw new Error(`Commune inconnue : ${codeCommune}. Date de valeur : ${dateValeur}`)
}
const [type] = types
const candidates = byCodeCommune[`${type}${codeCommune}`]
if (!candidates) {
return getCommune(codeCommune, dateValeur, types.slice(1))
}
const commune = dateValeur ?
candidates.find(c => isValidAt(c, dateValeur)) :
maxBy(candidates, c => c.dateFin || '9999-99-99')
if (!commune) {
// Récupération des communes ayant changé de département
if (type === 'COM') {
const plusRecente = maxBy(candidates, 'dateFin')
if (plusRecente.successeur && plusRecente.successeur.code !== plusRecente.code) {
return getCommune(plusRecente.successeur.code, dateValeur, ['COM', 'COMD', 'COMA', 'COMP'])
}
}
return getCommune(codeCommune, dateValeur, types.slice(1))
}
return commune
}
function getCommuneActuelle(communeEntry, dateValeur) {
if (isValidAt(communeEntry, dateValeur) && communeEntry.type === 'COM') {
return communeEntry
}
if (isValidAt(communeEntry, dateValeur)) {
return getCommuneActuelle(communeEntry.pole, dateValeur)
}
if (communeEntry.successeur) {
return getCommuneActuelle(communeEntry.successeur, dateValeur)
}
}
module.exports = {getCodeDepartement, getCommune, isValidAt, getCommuneActuelle, getCommuneFromCadastre}