-
Notifications
You must be signed in to change notification settings - Fork 0
/
region_sherbrooke.py
157 lines (134 loc) · 5.02 KB
/
region_sherbrooke.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
import os
import folium
import numpy as np
import geopandas
from pprint import pprint
import json
import pandas as pd
import re
import math
def getColor(d):
max = 0
color = 'gray'
try:
p =d
if (p['pq']== p['pq'] and p['pq'] > max):
max = p['pq']
color = 'blue'
if (p['plq'] == p['plq'] and p['plq'] > max):
max = p['plq']
color = 'red'
if p['caq']== p['caq'] and p['caq'] > max:
max = p['caq']
color = 'lightblue'
if p['qs']== p['qs'] and p['qs'] > max:
max = p['qs']
color = 'orange'
except:
color = 'white'
return color
couleurs = {
'C.A.Q.-É.F.L.': 'lightblue',
'P.L.Q./Q.L.P.': 'red',
'Q.S.': 'orange',
'P.Q.': 'blue'
}
re_caq = re.compile('.*C\.A\.Q\.-É\.F\.L\.$')
re_qs = re.compile('.*Q\.S\.$')
re_plq = re.compile('.*P\.L\.Q\./Q\.L\.P\.$')
re_pq = re.compile('.* P\.Q\.$')
re_pvq = re.compile('.*P\.V\.Q\./G\.P\.Q\.$')
re_pmlq = re.compile('.* P\.M\.L\.Q\.$')
re_pcq = re.compile('.* P.C.Q./C.P.Q.$')
re_npdq = re.compile('.* N\.P\.D\.Q.$')
re_bp = re.compile('.* B\.P\.$')
re_ea = re.compile('.* É\.A\.$')
re_pn = re.compile('.* P\.N\.$')
couverture = ['104','110', '116', '120', '126', '132', '138', '144', '150', '204',
'206', '210','212','216', '218', '220', '224', '226', '230', '232', '236', '238', '240', '244',
'246', '250', '252', '256', '258', '260', '264',
'502', '508', '514', '542', '548', '554', '560', '566',
'554', '594', '570','576',
'660', '666', '670', '676',
'708','714', '720', '742','754', '760',
'802','806','810','814', '818', '822', '826', '834','838',
'842','850', '854','858','902', '906',
'914','918','922','926','930', '938']
# Donnees géographiques des sections de vote.
#Utilison sles données publuiées par le DGEQ pour extraire
#les limites territoriales de chacune des circonscripton
#le résultat (circonscriptons) est un dataframe créé par
#la bibliotèque geopandas
circ_path = os.path.join('data', 'sherbrooke.json')
sections = geopandas.read_file(circ_path)
sections['NO_SV'].astype('str')
sections['CO_CEP'].astype('str')
sections.rename(columns={'NO_SV':'S.V.'}, inplace=True)
pprint(sections.shape[0])
pprint(sections.columns.values.tolist())
df_internes = sections[sections['CO_CEP'].isin(couverture)]
sections = df_internes
sections.set_index(['CO_CEP', 'S.V.'], inplace=True, drop=False)
pprint(sections.shape[0])
#pprint(sections)
m = folium.Map([45.5, -72], tiles='stamentoner', zoom_start=8)
cumul = pd.DataFrame()
for co_cep in couverture:
res_path = os.path.join('data', 'resultats-section-vote', "%s.csv"%co_cep)
data = pd.read_csv(res_path, sep=';', encoding='latin1', dtype={'S.V.':'str', 'Code':'str'})
data.dropna(subset=['S.V.'], inplace=True)
data.rename(columns={"Code":"CO_CEP", 'Nom des Municipalités':'NM_MUNI'}, inplace=True)
data.set_index(['CO_CEP','S.V.'], inplace=True)
candidats = data.columns.tolist()[7:-3]
pprint(candidats)
for candidat in candidats:
if re_caq.match(candidat):
data.rename(columns={candidat:'caq'}, inplace=True)
elif re_qs.match(candidat):
data.rename(columns={candidat:'qs'}, inplace=True)
elif re_plq.match(candidat):
data.rename(columns={candidat:'plq'}, inplace=True)
elif re_pq.match(candidat):
data.rename(columns={candidat:'pq'}, inplace=True)
elif re_pvq.match(candidat):
data.rename(columns={candidat:'pvq'}, inplace=True)
elif re_pmlq.match(candidat):
data.rename(columns={candidat:'pmlq'}, inplace=True)
elif re_pcq.match(candidat):
data.rename(columns={candidat:'pcq'}, inplace=True)
elif re_npdq.match(candidat):
data.rename(columns={candidat:'npdq'}, inplace=True)
elif re_bp.match(candidat):
data.rename(columns={candidat:'bp'}, inplace=True)
elif re_ea.match(candidat):
data.rename(columns={candidat:'ea'}, inplace=True)
elif re_pn.match(candidat):
data.rename(columns={candidat:'pn'}, inplace=True)
else:
print("WARNING: candidat:{} not matched".format(candidat))
data.drop(labels=candidat, axis=1, inplace=True)
data.drop(labels=['Circonscription', 'Date scrutin', 'Étendue', 'Secteur', 'Regroupement', 'É.I.'], axis=1, inplace=True)
if (cumul.size <= 0):
cumul = data
else:
a = cumul.append(data, sort=False)
cumul = a
pprint(cumul.dtypes)
striped = cumul[['plq', 'pq', 'caq', 'qs', 'NM_MUNI']]
sections = sections.join(striped)
#pprint(sections.dtypes)
sections['couleur'] = sections.apply(getColor, axis=1)
sections.set_index(['CO_CEP', 'S.V.'], inplace=True, drop=False)
pprint(sections[['plq', 'pq', 'caq', 'qs', 'couleur', 'NM_MUNI']])
folium.GeoJson(sections,
tooltip = folium.features.GeoJsonTooltip(fields=['NM_CEP', 'S.V.', 'couleur', 'NM_MUNI'], labels=True),
style_function=lambda dd: {
'fillColor': dd['properties']['couleur'],
'color' : 'green',
'weight' : 0.25,
'fillOpacity' : 0.5,
},
).add_to(m)
#Enfin sauvegardons la carte
region_sherbrooke_html = os.path.join('docs', 'region_sherbrooke.html')
m.save(region_sherbrooke_html)