-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquebec_regions_mapping.py
342 lines (290 loc) · 12.9 KB
/
quebec_regions_mapping.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
from unidecode import unidecode
REGION_MAPPING = {
'MONTREAL': [
# Core Montreal with variations
'Montréal', 'Montreal', 'Ville de Montréal', 'City of Montreal',
# Boroughs
'Ahuntsic-Cartierville',
'Anjou',
'Côte-des-Neiges–Notre-Dame-de-Grâce', 'Côte-des-Neiges-NDG', 'CDN-NDG',
'Le Plateau-Mont-Royal', 'Le Plateau', 'Plateau-Mont-Royal',
'Le Sud-Ouest', 'South West',
'L\'Île-Bizard–Sainte-Geneviève', 'Ile-Bizard-Sainte-Genevieve',
'Mercier–Hochelaga-Maisonneuve', 'MHM',
'Montréal-Nord', 'Montreal North',
'Outremont',
'Pierrefonds-Roxboro',
'Rivière-des-Prairies–Pointe-aux-Trembles', 'RDP-PAT',
'Rosemont–La Petite-Patrie', 'RPP',
'Saint-Laurent', 'St-Laurent',
'Saint-Léonard', 'St-Leonard',
'Verdun',
'Ville-Marie',
'Villeray–Saint-Michel–Parc-Extension', 'VSMPE',
# Demerged cities
'Baie-D\'Urfé', 'Baie-Durfe',
'Beaconsfield',
'Côte-Saint-Luc', 'Cote-Saint-Luc',
'Dollard-des-Ormeaux', 'DDO',
'Dorval',
'Hampstead',
'Kirkland',
'Mont-Royal', 'Mount Royal', 'TMR',
'Montréal-Est', 'Montreal East',
'Montréal-Ouest', 'Montreal West',
'Pointe-Claire',
'Sainte-Anne-de-Bellevue',
'Senneville',
'Westmount',
'MTL'
],
'MONTEREGIE': [
# MRC Acton
'Acton Vale', 'Béthanie', 'Roxton', 'Roxton Falls', 'Saint-Nazaire-d\'Acton',
'Saint-Théodore-d\'Acton', 'Sainte-Christine', 'Upton',
# MRC Beauharnois-Salaberry
'Beauharnois', 'Saint-Étienne-de-Beauharnois', 'Saint-Louis-de-Gonzague',
'Saint-Stanislas-de-Kostka', 'Saint-Urbain-Premier', 'Sainte-Martine',
'Salaberry-de-Valleyfield',
# MRC Brome-Missisquoi
'Abercorn', 'Bedford', 'Bolton-Ouest', 'Brigham', 'Brome', 'Bromont',
'Cowansville', 'Dunham', 'East Farnham', 'Farnham', 'Frelighsburg',
'Lac-Brome', 'Notre-Dame-de-Stanbridge', 'Pike River', 'Saint-Armand',
'Saint-Ignace-de-Stanbridge', 'Sainte-Sabine', 'Stanbridge East',
'Stanbridge Station', 'Sutton',
# MRC La Haute-Yamaska
'Granby', 'Roxton Pond', 'Saint-Alphonse-de-Granby', 'Saint-Joachim-de-Shefford',
'Sainte-Cécile-de-Milton', 'Shefford', 'Warden', 'Waterloo',
# MRC La Vallée-du-Richelieu
'Beloeil', 'Carignan', 'Chambly', 'McMasterville', 'Mont-Saint-Hilaire',
'Otterburn Park', 'Saint-Antoine-sur-Richelieu', 'Saint-Basile-le-Grand',
'Saint-Charles-sur-Richelieu', 'Saint-Denis-sur-Richelieu', 'Saint-Jean-Baptiste',
'Saint-Marc-sur-Richelieu', 'Saint-Mathieu-de-Beloeil',
# MRC Le Haut-Richelieu
'Saint-Jean-sur-Richelieu', 'Saint-Alexandre', 'Saint-Blaise-sur-Richelieu',
'Saint-Georges-de-Clarenceville', 'Saint-Paul-de-l\'Île-aux-Noix',
'Saint-Sébastien', 'Saint-Valentin', 'Sainte-Anne-de-Sabrevois',
'Sainte-Brigide-d\'Iberville', 'Venise-en-Québec',
# MRC Les Jardins-de-Napierville
'Hemmingford', 'Napierville', 'Saint-Bernard-de-Lacolle', 'Saint-Cyprien-de-Napierville',
'Saint-Édouard', 'Saint-Jacques-le-Mineur', 'Saint-Michel', 'Saint-Patrice-de-Sherrington',
'Saint-Rémi',
# MRC Les Maskoutains
'Saint-Hyacinthe', 'Saint-Barnabé-Sud', 'Saint-Bernard-de-Michaudville',
'Saint-Damase', 'Saint-Dominique', 'Saint-Hugues', 'Saint-Jude',
'Saint-Liboire', 'Saint-Louis', 'Saint-Marcel-de-Richelieu', 'Saint-Pie',
'Saint-Simon', 'Saint-Valérien-de-Milton', 'Sainte-Hélène-de-Bagot',
'Sainte-Madeleine', 'Sainte-Marie-Madeleine',
# MRC Marguerite-D'Youville
'Calixa-Lavallée', 'Contrecoeur', 'Saint-Amable', 'Sainte-Julie',
'Varennes', 'Verchères',
# MRC Pierre-De Saurel
'Massueville', 'Saint-Aimé', 'Saint-David', 'Saint-Gérard-Majella',
'Saint-Joseph-de-Sorel', 'Saint-Ours', 'Saint-Robert', 'Saint-Roch-de-Richelieu',
'Sainte-Anne-de-Sorel', 'Sainte-Victoire-de-Sorel', 'Sorel-Tracy',
'Yamaska',
# MRC Roussillon
'Candiac', 'Châteauguay', 'Delson', 'La Prairie', 'Léry', 'Mercier',
'Saint-Constant', 'Saint-Isidore', 'Saint-Mathieu', 'Saint-Philippe',
'Sainte-Catherine',
# MRC Rouville
'Ange-Gardien', 'Marieville', 'Richelieu', 'Rougemont', 'Saint-Césaire',
'Saint-Mathias-sur-Richelieu', 'Saint-Paul-d\'Abbotsford', 'Sainte-Angèle-de-Monnoir',
# MRC Vaudreuil-Soulanges
'Coteau-du-Lac', 'Hudson', 'Les Cèdres', 'Les Coteaux', 'L\'Île-Cadieux',
'L\'Île-Perrot', 'Notre-Dame-de-l\'Île-Perrot', 'Pincourt', 'Pointe-des-Cascades',
'Pointe-Fortune', 'Rigaud', 'Rivière-Beaudette', 'Saint-Clet', 'Saint-Lazare',
'Saint-Polycarpe', 'Saint-Télesphore', 'Saint-Zotique', 'Sainte-Justine-de-Newton',
'Sainte-Marthe', 'Terrasse-Vaudreuil', 'Très-Saint-Rédempteur', 'Vaudreuil-Dorion',
'Vaudreuil-sur-le-Lac',
# Agglomération de Longueuil
'Boucherville', 'Brossard', 'Longueuil', 'Saint-Bruno-de-Montarville',
'Saint-Lambert',
'VSL'
],
'LAURENTIDES': [
# MRC Antoine-Labelle
'Chute-Saint-Philippe', 'Ferme-Neuve', 'Kiamika', 'Lac-des-Écorces',
'Lac-du-Cerf', 'Lac-Saint-Paul', 'La Macaza', 'L\'Ascension',
'Mont-Laurier', 'Mont-Saint-Michel', 'Nominingue', 'Notre-Dame-de-Pontmain',
'Notre-Dame-du-Laus', 'Rivière-Rouge', 'Sainte-Anne-du-Lac', 'Saint-Aimé-du-Lac-des-Îles',
# MRC Argenteuil
'Brownsburg-Chatham', 'Gore', 'Grenville', 'Grenville-sur-la-Rouge',
'Harrington', 'Lachute', 'Mille-Isles', 'Saint-André-d\'Argenteuil',
'Wentworth',
# MRC Deux-Montagnes
'Deux-Montagnes', 'Oka', 'Pointe-Calumet', 'Saint-Eustache',
'Saint-Joseph-du-Lac', 'Saint-Placide', 'Sainte-Marthe-sur-le-Lac',
# MRC La Rivière-du-Nord
'Prévost', 'Saint-Colomban', 'Saint-Hippolyte', 'Saint-Jérôme',
'Sainte-Sophie',
# MRC Les Laurentides
'Amherst', 'Arundel', 'Barkmere', 'Brébeuf', 'Huberdeau', 'Ivry-sur-le-Lac',
'La Conception', 'La Minerve', 'Labelle', 'Lac-Supérieur', 'Lac-Tremblant-Nord',
'Lantier', 'Mont-Tremblant', 'Montcalm', 'Saint-Faustin–Lac-Carré',
'Sainte-Agathe-des-Monts', 'Sainte-Lucie-des-Laurentides', 'Val-David',
'Val-des-Lacs', 'Val-Morin',
# MRC Les Pays-d'en-Haut
'Estérel', 'Lac-des-Seize-Îles', 'Morin-Heights', 'Piedmont',
'Saint-Adolphe-d\'Howard', 'Sainte-Adèle', 'Sainte-Anne-des-Lacs',
'Sainte-Marguerite-du-Lac-Masson', 'Saint-Sauveur', 'Wentworth-Nord',
# MRC Thérèse-De Blainville
'Blainville', 'Boisbriand', 'Bois-des-Filion', 'Lorraine',
'Rosemère', 'Sainte-Anne-des-Plaines', 'Sainte-Thérèse',
# Mirabel
'Mirabel'
],
'LANAUDIERE': [
# MRC D'Autray
'Berthierville', 'La Visitation-de-l\'Île-Dupas', 'Lanoraie', 'Lavaltrie',
'Mandeville', 'Saint-Barthélemy', 'Saint-Cléophas-de-Brandon', 'Saint-Cuthbert',
'Saint-Didace', 'Saint-Gabriel', 'Saint-Gabriel-de-Brandon', 'Saint-Ignace-de-Loyola',
'Saint-Norbert', 'Sainte-Élisabeth', 'Sainte-Geneviève-de-Berthier',
# MRC Joliette
'Crabtree', 'Joliette', 'Notre-Dame-de-Lourdes', 'Notre-Dame-des-Prairies',
'Saint-Ambroise-de-Kildare', 'Saint-Charles-Borromée', 'Saint-Paul',
'Saint-Pierre', 'Saint-Thomas', 'Sainte-Mélanie',
# MRC L'Assomption
'Charlemagne', 'L\'Assomption', 'L\'Épiphanie', 'Repentigny',
'Saint-Sulpice',
# MRC Les Moulins
'Mascouche', 'Terrebonne',
# MRC Matawinie
'Chertsey', 'Entrelacs', 'Notre-Dame-de-la-Merci', 'Rawdon',
'Saint-Alphonse-Rodriguez', 'Saint-Côme', 'Saint-Damien', 'Saint-Donat',
'Saint-Félix-de-Valois', 'Saint-Jean-de-Matha', 'Saint-Michel-des-Saints',
'Saint-Zénon', 'Sainte-Béatrix', 'Sainte-Émélie-de-l\'Énergie',
'Sainte-Marcelline-de-Kildare',
# MRC Montcalm
'Saint-Alexis', 'Saint-Calixte', 'Saint-Esprit', 'Saint-Jacques',
'Saint-Liguori', 'Saint-Lin–Laurentides', 'Saint-Roch-de-l\'Achigan',
'Saint-Roch-Ouest', 'Sainte-Julienne', 'Sainte-Marie-Salomé'
],
'LAVAL': [
# City
'Laval', 'Ville de Laval', 'City of Laval',
# Districts/Neighborhoods
'Auteuil',
'Chomedey',
'Duvernay', 'Duvernay-Est', 'Duvernay-Ouest',
'Fabreville',
'Îles-Laval',
'Laval-des-Rapides',
'Laval-Ouest', 'Laval West',
'Laval-sur-le-Lac',
'Pont-Viau',
'Saint-François', 'St-François',
'Saint-Vincent-de-Paul', 'St-Vincent-de-Paul',
'Sainte-Dorothée', 'Ste-Dorothée',
'Sainte-Rose', 'Ste-Rose',
'Vimont'
]
}
# Create reverse mapping including all variations
CITY_TO_REGION = {}
for region, cities in REGION_MAPPING.items():
for city in cities:
CITY_TO_REGION[city.upper()] = region
# Add versions without accents
unaccented = unidecode(city)
if unaccented != city:
CITY_TO_REGION[unaccented.upper()] = region
# Higher level regional grouping
SHORE_MAPPING = {
'NORTH_SHORE': ['LAURENTIDES', 'LANAUDIERE'],
'SOUTH_SHORE': ['MONTEREGIE'],
'MONTREAL': ['MONTREAL'],
'LAVAL': ['LAVAL'],
'LONGUEUIL': []
}
# Cities in Longueuil agglomeration
LONGUEUIL_CITIES = {
'LONGUEUIL', 'VIEUX-LONGUEUIL',
'BROSSARD',
'SAINT-LAMBERT', 'ST-LAMBERT',
'BOUCHERVILLE',
'SAINT-BRUNO-DE-MONTARVILLE', 'ST-BRUNO', 'SAINT-BRUNO',
'GREENFIELD PARK',
'SAINT-HUBERT', 'ST-HUBERT',
'LEMOYNE', 'LE MOYNE'
}
# Define custom sectors using existing city definitions
CUSTOM_SECTORS = {
'flyer_chateauguay': [
'Candiac', 'Châteauguay', 'Delson', 'La Prairie', 'Léry', 'Mercier',
'Saint-Constant', 'Saint-Isidore', 'Saint-Mathieu', 'Saint-Philippe',
'Sainte-Catherine'
],
'flyer_sector_west': [
# Include only the cities we want from MRC Vaudreuil-Soulanges
'Hudson', 'Les Cèdres', 'L\'Île-Cadieux', 'L\'Île-Perrot',
'Notre-Dame-de-l\'Île-Perrot', 'Pincourt', 'Pointe-des-Cascades',
'Saint-Lazare', 'Terrasse-Vaudreuil', 'Vaudreuil-Dorion',
'Vaudreuil-sur-le-Lac'
]
}
# Add this new constant for postal code sectors
POSTAL_CODE_SECTORS = {
'flyer_sector_west': [
'H8T', 'H4Y', 'H4S', 'H8Y', 'H9B', 'H9P', 'H8Z', 'H9A', 'H9G',
'H9R', 'H9S', 'H9C', 'H9H', 'H9E', 'H9J', 'H9W', 'H9K', 'H9X'
]
}
def get_custom_sector(city, postal_code=None):
"""
Get the custom sector for a given city and postal code.
Returns sector name or None if city/postal code isn't in any custom sector
"""
if not city and not postal_code:
return None
city_upper = city.upper().strip() if city else ''
city_unaccented = unidecode(city_upper)
postal_prefix = postal_code[:3].upper() if postal_code else ''
# Check postal code first
for sector, postal_codes in POSTAL_CODE_SECTORS.items():
if postal_prefix and postal_prefix in postal_codes:
return sector
# Then check city
for sector, cities in CUSTOM_SECTORS.items():
if any(city_upper == c.upper() or city_unaccented == unidecode(c).upper() for c in cities):
return sector
return None
def get_shore_region(city):
"""
Get the shore region for a given city.
Returns one of: flyer_north_shore, flyer_south_shore, flyer_montreal, flyer_laval, flyer_longueuil, flyer_unknown
"""
if not city:
return 'unknown'
city_upper = city.upper().strip()
# Handle accents in lookup
city_unaccented = unidecode(city_upper)
# First check if it's in Longueuil agglomeration
if city_upper in LONGUEUIL_CITIES or city_unaccented in LONGUEUIL_CITIES:
return 'longueuil'
# Get the region (e.g., MONTEREGIE, LANAUDIERE, etc.)
city_region = CITY_TO_REGION.get(city_upper) or CITY_TO_REGION.get(city_unaccented)
if not city_region:
return 'unknown'
# Map region to shore
if city_region == 'MONTEREGIE':
return 'south_shore'
elif city_region in ['LAURENTIDES', 'LANAUDIERE']:
return 'north_shore'
elif city_region == 'MONTREAL':
return 'montreal'
elif city_region == 'LAVAL':
return 'laval'
return 'unknown'
# Example usage:
if __name__ == "__main__":
test_cities = [
'Terrebonne', # North Shore
'Brossard', # Longueuil
'Montreal', # Montreal
'Laval', # Laval
'Longueuil', # Longueuil
'Invalid City' # Unknown
]
for city in test_cities:
print(f"{city}: {get_shore_region(city)}")