apps / maps / routes / index.js /
Newer Older
195 lines | 7.086kb
initial commit
admin cloud-section (root) authored on 2016-12-10
1
var express = require('express');
2
var router = express.Router();
3
var sqlite3 = require('sqlite3');
4
var fs = require('fs');
5

            
6
var db = new sqlite3.Database('./public/elections.db');
mise à jour découpage bureau...
admin cloud-section (root) authored on 2019-03-17
7
var zonesfull = JSON.parse(fs.readFileSync("./public/secteurs-des-bureaux-de-vote.json"));
initial commit
admin cloud-section (root) authored on 2016-12-10
8

            
9
var elections = ["Élections"];
10
var sql_election = [""];
11
var sections = ["Sections"];
12
var sql_section = [""];
13

            
14
var route_name = "/cartes-elections";
15
var url = "https://cloud.paris12.pcf.fr" + route_name;
16

            
17
function resultat_par_bureau (from_where, numerateur, denominateur, gradient, next) {
18
    db.all('select bureau,((1.00 * ' + numerateur + ') / (1.00 * ' + denominateur + ')) as data' + from_where, function (err, rows) {
19
        if (err) {
20
            console.log(err);
21
        }
22
        else {
23
            gradient.min = 9999;
24
            gradient.max = 0 - gradient.min;
25
            for (var row in rows) {
26
                gradient[rows[row].bureau] = rows[row].data;
27
                gradient.max = (rows[row].data > gradient.max) ? rows[row].data : gradient.max;
28
                gradient.min = (rows[row].data < gradient.min) ? rows[row].data : gradient.min;
29
            }
30
            next();
31
        }
32
    });
33
}
34

            
35
var requetes = {
36
    "Requêtes" : "",
37
    "Scores candidat-e (ou liste de) soutenu-e (en %)" : function (req, next) {
38
        var gradient = { 
39
            titre : req.requete + '<br>' + elections[req.election],
40
            lightness : {
41
                intitule : 'résultat',
42
                type     : '%'
43
            }
44
        };
45
        var from_where = ' from scrutins where soutien is 1 and ' + sql_election[req.election] + ' and ' + sql_section[req.section];
46
        resultat_par_bureau(from_where, 'voix', 'exprimes', gradient.lightness, function () {
47
            next(req.section, gradient);
48
        });
49
    },
50
    "Voix candidat-e (ou liste de de) soutenu-e": function (req, next) {
51
        var gradient = { 
52
            titre : req.requete + '<br>' + elections[req.election],
53
            lightness : {
54
                intitule : 'voix',
55
                type    : 'absolues'
56
            },
57
            saturation : {
58
                intitule : 'inscrits',
59
                type    : 'absolus'
60
            }
61
        };
62
        var from_where = ' from scrutins where soutien is 1 and ' + sql_election[req.election] + ' and ' + sql_section[req.section];
63
        resultat_par_bureau(from_where, 'voix', 1, gradient.lightness, function () {
64
            resultat_par_bureau(from_where, 'inscrits', 1, gradient.saturation, function () {
65
                next(req.section, gradient);
66
            });
67
        });
68
    },
69
    "Abstention seule (%)" : function (req, next) {
70
        var gradient = { 
71
            titre : req.requete + '<br>' + elections[req.election],
72
            lightness : {
73
                intitule : 'abstention',
74
                type     : '%'
75
            }
76
        };
77
        var from_where = ' from scrutins where soutien is 1 and ' + sql_election[req.election] + ' and ' + sql_section[req.section];
78
        resultat_par_bureau(from_where, 'inscrits - exprimes', 'inscrits', gradient.lightness, function () {
79
            next(req.section, gradient);
80
        });
81
    },
82
    "Score soutien (%) + abstention (%)" : function (req, next) {
83
        var gradient = {
84
            titre : req.requete + '<br>' + elections[req.election],
85
            lightness : {
86
                intitule : 'résultat',
87
                type     : '%'
88
            },
89
            saturation : {
90
                intitule : 'abstention',
91
                type     : '%'
92
            }
93
        };
94
        var from_where = ' from scrutins where soutien is 1 and ' + sql_election[req.election] + ' and ' + sql_section[req.section];
95
        resultat_par_bureau(from_where, 'voix', 'exprimes', gradient.lightness, function () {
96
            resultat_par_bureau(from_where, 'inscrits - exprimes', 'inscrits', gradient.saturation, function () {
97
                next(req.section, gradient);
98
            });
99
        });
100
    },
101
    "Evolution avec autre scrutin (pas encore disponible)" : function () { return; }
102
}
103

            
104
function pad(n, width, z) {
105
    z = z || '0';
106
    n = n + '';
107
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
108
}
109

            
110
function zones_to_geoJSON (arrondissement, resultats, next) {
111
    var geoJSON = {
112
        "type": "FeatureCollection",
113
        "features": []
114
    };
115
    for (var bureau in zonesfull) {
mise à jour découpage bureau...
admin cloud-section (root) authored on 2019-03-17
116
        if (zonesfull[bureau].fields.arrondissement == arrondissement) {
initial commit
admin cloud-section (root) authored on 2016-12-10
117
            var num_bv = zonesfull[bureau].fields.num_bv;
118
            geoJSON.features.push({
119
                "type" : "Feature",
120
                "id"   : num_bv,
121
                "properties": {"num_bv": num_bv },
122
                "geometry": zonesfull[bureau].fields.geo_shape
123
            });
124
        }
125
    }
126
    next(resultats, geoJSON);
127
}
128

            
129
function format_first_second (v) {
130
    return v + (v === 1 && "er" || "ème");
131
}
132

            
133
function set_elections_arrays (err, row) {
134
    if (!err) {
135
        elections.push(row.annee + " - " + row.election + " - " + format_first_second(row.tour) + " tour" );
136
        sql_election.push("election is '" + row.election + "' and annee is " + row.annee + " and tour is " + row.tour);
137
    }
138
    else {
139
        console.log(err);
140
    }
141
}
142

            
143
function get_elections_choices () {
144
    db.each("select distinct election,annee,tour from scrutins order by annee, election, tour asc", set_elections_arrays);
145
}
146
get_elections_choices();
147

            
148
for (var section = 1; section <= 20; section++) {
149
    sections.push(format_first_second(section) + " arrondissement");
150
    sql_section.push("arrondissement is " + section);
151
}
152

            
153
router
154
    .route(route_name)
155
    .get(function (req, res, next) {
156
        res.render('choix', {
157
            url : url,
158
            elections : elections, 
159
            sections : sections, 
160
            requetes : requetes
161
        });
162
    })
163
    .post(function(req, res, next) {
164
        if (!req.body) return res.sendStatus(400);
165
        requetes[req.body.requete](req.body, function (arrondissement, resultats) {
166
            function testresultats () {
167
                for (var hsla in resultats) {
168
                    if (resultats[hsla][1]) { 
169
                        return true; 
170
                    }
171
                }
172
                return false;
173
            }
174
            if (testresultats()) {
175
                zones_to_geoJSON(arrondissement, resultats, function (resultats, zones) {
176
                    if (!zones.features[0].geometry) {
177
                        throw("pas de découpage...");
178
                    }
179
                    else {
180
                        res.render('paris', { 
181
                            'url' : url,
182
                            'gradient' : resultats,
183
                            'zones'    : zones
184
                        })
185
                    }
186
                });
187
            }
188
            else {
189
                var noresult = "Pas de résultat sur le " + req.body.section + " pour '" + req.body.election + "'...";
190
                res.simpleText(200, noresult);
191
            }
192
        });
193
    });
194

            
195
module.exports = router;