-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElectionData.cpp
918 lines (878 loc) · 35.3 KB
/
ElectionData.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
#include "ElectionData.h"
#include "General.h"
#include "Log.h"
#include <set>
Results2::Election Results2::Election::createAec(tinyxml2::XMLDocument const& xml)
{
Election election;
election.update(xml);
return election;
}
Results2::Election Results2::Election::createVec(nlohmann::json const& results, tinyxml2::XMLDocument const& input_candidates, tinyxml2::XMLDocument const& input_booths)
{
Election election;
election.update2022VicPrev(results, input_candidates, input_booths);
return election;
}
Results2::Election Results2::Election::createVec(tinyxml2::XMLDocument const& input_candidates, tinyxml2::XMLDocument const& input_booths)
{
Election election;
election.preload2022Vic(input_candidates, input_booths, true);
return election;
}
Results2::Election Results2::Election::createNswec(nlohmann::json const& results, tinyxml2::XMLDocument const& zeros)
{
Election election;
election.preloadNswec(results, zeros, true);
return election;
}
void Results2::Election::preload2022Vic(tinyxml2::XMLDocument const& input_candidates, tinyxml2::XMLDocument const& input_booths, bool includeSeatBooths)
{
const std::map<std::string, std::string> shortCodes = {
{"The Australian Greens - Victoria", "GRN"},
{"Australian Labor Party - Victorian Branch", "ALP"},
{"Liberal Party of Australia - Victorian Division", "LNP"},
{"National Party of Australia - Victoria", "LNP"}
};
auto candidateList = input_candidates.FirstChildElement("EML")->FirstChildElement("CandidateList")->FirstChildElement("Election");
auto currentContest = candidateList->FirstChildElement("Contest");
while (currentContest) {
Seat seat;
auto contestIdentifier = currentContest->FirstChildElement("PollingDistrictIdentifier");
seat.id = contestIdentifier->FindAttribute("Id")->IntValue();
if (seats.contains(seat.id)) seat = seats[seat.id]; // maintain already existing data
std::string contestName = currentContest->FirstChildElement("ContestIdentifier")->FirstChildElement("ContestName")->GetText();
seat.name = contestName.substr(0, contestName.length() - 9);
seat.enrolment = currentContest->FirstChildElement("Enrolment")->IntText();
auto currentCandidate = currentContest->FirstChildElement("Candidate");
while (currentCandidate) {
Candidate candidate;
auto candidateIdentifier = currentCandidate->FirstChildElement("CandidateIdentifier");
candidate.id = candidateIdentifier->FindAttribute("Id")->IntValue();
candidate.name = candidateIdentifier->FirstChildElement("CandidateName")->GetText();
auto affiliationEl = currentCandidate->FirstChildElement("Affiliation");
if (affiliationEl) {
auto affiliationIdentifier = affiliationEl->FirstChildElement("AffiliationIdentifier");
candidate.party = affiliationIdentifier->FindAttribute("Id")->IntValue();
if (!parties.contains(candidate.party)) {
parties[candidate.party] = Party();
parties[candidate.party].id = candidate.party;
parties[candidate.party].name =
affiliationIdentifier->FirstChildElement("RegisteredName")->GetText();
if (shortCodes.contains(parties[candidate.party].name)) {
parties[candidate.party].shortCode = shortCodes.at(parties[candidate.party].name);
}
}
}
else {
candidate.party = Candidate::Independent;
parties[Candidate::Independent].id = Candidate::Independent;
parties[Candidate::Independent].shortCode = "IND";
parties[Candidate::Independent].name = "Independent";
}
candidates[candidate.id] = candidate;
currentCandidate = currentCandidate->NextSiblingElement("Candidate");
}
seats[seat.id] = seat;
currentContest = currentContest->NextSiblingElement("Contest");
}
auto currentPollingDistrict = input_booths.FirstChildElement("PollingDistrictList")->FirstChildElement("PollingDistrict");
while (currentPollingDistrict) {
int seatId = currentPollingDistrict->FirstChildElement("PollingDistrictIdentifier")->IntAttribute("Id");
auto currentBooth = currentPollingDistrict->FirstChildElement("PollingPlaces")->FirstChildElement("PollingPlace");
while (currentBooth) {
Booth booth;
booth.id = currentBooth->FirstChildElement("PollingPlaceIdentifier")->IntAttribute("Id");
booth.name = currentBooth->FirstChildElement("PollingPlaceIdentifier")->Attribute("Name");
booth.parentSeat = seatId;
booths[booth.id] = booth;
currentBooth = currentBooth->NextSiblingElement("PollingPlace");
if (includeSeatBooths) {
seats[seatId].booths.push_back(booth.id);
}
}
currentPollingDistrict = currentPollingDistrict->NextSiblingElement("PollingDistrict");
}
}
void Results2::Election::update2022VicPrev(nlohmann::json const& results, tinyxml2::XMLDocument const& input_candidates, tinyxml2::XMLDocument const& input_booths)
{
preload2022Vic(input_candidates, input_booths);
std::map<std::string, int> seatNameToId;
for (auto [seatId, seat] : seats) {
if (seatNameToId.contains(seat.name)) {
logger << "Warning: duplicate name for " << seat.name << "!\n";
}
else {
seatNameToId[seat.name] = seatId;
}
}
std::map<std::string, int> candidateNameToId;
for (auto [candidateId, candidate] : candidates) {
if (candidateNameToId.contains(candidate.name)) {
logger << "Warning: duplicate name for " << candidate.name << "!\n";
}
else {
candidateNameToId[candidate.name] = candidateId;
}
}
std::set<std::string> seenBooths;
std::set<std::string> ambiguousBooths;
for (auto [boothId, booth] : booths) {
if (seenBooths.contains(booth.name)) {
ambiguousBooths.emplace(booth.name);
}
else {
seenBooths.emplace(booth.name);
}
}
std::map<std::string, int> boothNameToId;
std::map<std::pair<std::string, std::string>, int> ambiguousBoothNameToId;
for (auto [boothId, booth] : booths) {
if (ambiguousBooths.contains(booth.name)) {
ambiguousBoothNameToId[{booth.name, seats[booth.parentSeat].name}] = boothId;
}
else {
boothNameToId[booth.name] = boothId;
}
}
//PA_LOG_VAR(boothNameToId);
//PA_LOG_VAR(ambiguousBoothNameToId);
int dummyCandidateId = -100000; // Low numbers that will never be mistaken for an official id
int dummyBoothId = -100000; // Low numbers that will never be mistaken for an official id
// Not worth soft-coding this
const std::map<std::string, int> partyIds = {
{"ANIMAL JUSTICE PARTY", 25},
{"LIBERAL", 8},
{"FIONA PATTEN'S REASON PARTY", 20},
{"SUSTAINABLE AUSTRALIA", 91},
{"AUSTRALIAN GREENS", 4},
{"AUSTRALIAN LABOR PARTY", 5},
{"LABOUR DLP", 7},
{"SHOOTERS, FISHERS & FARMERS VIC", 28},
{"VICTORIAN SOCIALISTS", 90},
{"THE NATIONALS", 9},
{"DERRYN HINCH'S JUSTICE PARTY", 89},
{"LIBERAL DEMOCRATS", 30},
{"TRANSPORT MATTERS", 87},
{"AUSSIE BATTLER PARTY", -1},
{"AUSTRALIAN COUNTRY PARTY", -1},
{"AUSTRALIAN LIBERTY ALLIANCE", -1},
{"INDEPENDENT", -1}
};
std::set<int> matchedIds;
for (auto const& [seatName, seatValue] : results.items()) {
int seatId = -1;
if (seatNameToId.contains(seatName)) {
seatId = seatNameToId[seatName];
}
std::map<int, int> indexToId;
for (auto const& [candIndex, candValue] : seatValue["candidates"].items()) {
int candIndexI = std::stoi(candIndex);
auto candidateName = candValue["name"];
auto party = candValue["party"];
if (candidateNameToId.contains(candidateName)) {
indexToId[candIndexI] = candidateNameToId[candidateName];
}
else {
indexToId[candIndexI] = dummyCandidateId;
Candidate candidate;
candidate.id = dummyCandidateId;
candidate.name = candidateName;
candidate.party = partyIds.at(party);
candidates[candidate.id] = candidate;
--dummyCandidateId;
}
}
for (auto const& [boothName, boothValue] : seatValue["booths"].items()) {
if (boothName.find("Votes") != std::string::npos) {
VoteType voteType = VoteType::Invalid;
if (boothName == "Postal Votes") voteType = VoteType::Postal;
if (boothName == "Absent Votes") voteType = VoteType::Absent;
if (boothName == "Early Votes") voteType = VoteType::Early;
if (boothName == "Provisional Votes") voteType = VoteType::Provisional;
if (voteType == VoteType::Invalid) continue;
auto fps = boothValue["fp"];
for (auto const& [fpCandIndex, fpVotes] : fps.items()) {
int fpCandIndexI = std::stoi(fpCandIndex);
int fpCandId = indexToId[fpCandIndexI];
seats[seatId].fpVotes[fpCandId][voteType] += fpVotes;
}
auto tcps = boothValue["tcp"];
for (auto const& [tcpCandIndex, tcpVotes] : tcps.items()) {
int tcpCandIndexI = std::stoi(tcpCandIndex);
int tcpCandId = indexToId[tcpCandIndexI];
int tcpAffiliation = candidates[tcpCandId].party;
seats[seatId].tcpVotes[tcpAffiliation][voteType] += tcpVotes;
}
continue;
}
int boothId = dummyBoothId;
if (boothNameToId.contains(boothName)) {
boothId = boothNameToId[boothName];
}
else if (ambiguousBoothNameToId.contains({ boothName, seatName })) {
boothId = ambiguousBoothNameToId[{ boothName, seatName }];
}
else {
Booth booth;
booth.name = boothName;
booth.id = dummyBoothId;
booths[booth.id] = booth;
--dummyBoothId;
}
if (matchedIds.contains(boothId)) {
// If two "old" booths match to one "new" booth then we don't know
// which "old" booth to actually compare to
// so ... make new booths that won't match to either
if (booths.contains(boothId)) {
auto boothData = booths[boothId];
boothData.id = dummyBoothId;
booths.erase(boothId);
booths[dummyBoothId] = boothData;
auto& parent = seats[booths[dummyBoothId].parentSeat];
//PA_LOG_VAR(parent.name);
//PA_LOG_VAR(parent.booths);
//PA_LOG_VAR(boothId);
//PA_LOG_VAR(dummyBoothId);
auto foundBoothId = std::find(parent.booths.begin(), parent.booths.end(), boothId);
if (foundBoothId != parent.booths.end()) *foundBoothId = dummyBoothId;
//PA_LOG_VAR(parent.booths);
//logger << "Detached old ambiguous booth: " << booths[dummyBoothId].name << "\n";
--dummyBoothId;
}
boothId = dummyBoothId;
//logger << "Detached ambiguous booth: " << boothName << "\n";
--dummyBoothId;
}
matchedIds.insert(boothId);
Booth& booth = booths[boothId];
// the parent seat refers to the seat this booth is in for the new election,
// but it should the the one it is in for this (old) election
booth.parentSeat = seatId;
booth.id = boothId;
booth.name = boothName;
auto fps = boothValue["fp"];
for (auto const& [fpCandIndex, fpVotes] : fps.items()) {
int fpCandIndexI = std::stoi(fpCandIndex);
int fpCandId = indexToId[fpCandIndexI];
booth.fpVotes[fpCandId] = fpVotes;
if (seatId > 0) seats[seatId].fpVotes[fpCandId][VoteType::Ordinary] += fpVotes;
}
auto tcps = boothValue["tcp"];
for (auto const& [tcpCandIndex, tcpVotes] : tcps.items()) {
int tcpCandIndexI = std::stoi(tcpCandIndex);
int tcpCandId = indexToId[tcpCandIndexI];
int tcpAffiliation = candidates[tcpCandId].party;
booth.tcpVotes[tcpAffiliation] = tcpVotes;
if (seatId > 0) seats[seatId].tcpVotes[tcpAffiliation][VoteType::Ordinary] += tcpVotes;
}
if (seatId > 0) {
seats[seatId].booths.push_back(booth.id);
}
}
}
//logger << "==SEATS==\n";
//for (auto const& [seatId, seat] : seats) {
// PA_LOG_VAR(seat.name);
// PA_LOG_VAR(seat.id);
// PA_LOG_VAR(seat.enrolment);
// PA_LOG_VAR(seat.fpVotes);
// PA_LOG_VAR(seat.tcpVotes);
// PA_LOG_VAR(seat.tppVotes);
// for (int boothId : seat.booths) {
// PA_LOG_VAR(boothId);
// auto const& booth = booths.at(boothId);
// PA_LOG_VAR(booth.id);
// PA_LOG_VAR(booth.name);
// PA_LOG_VAR(booth.fpVotes);
// PA_LOG_VAR(booth.tcpVotes);
// PA_LOG_VAR(booth.type);
// }
//}
//logger << "==CANDIDATES==\n";
//for (auto const& candidate : candidates) {
// PA_LOG_VAR(candidate.second.id);
// PA_LOG_VAR(candidate.second.name);
// if (candidate.second.party != -1) {
// PA_LOG_VAR(parties[candidate.second.party].name);
// }
// else {
// logger << "Independent\n";
// }
//}
//logger << "==PARTIES==\n";
//for (auto const& party : parties) {
// PA_LOG_VAR(party.second.id);
// PA_LOG_VAR(party.second.name);
// PA_LOG_VAR(party.second.shortCode);
//}
logger << "Appeared to successfully load past election data\n";
}
void Results2::Election::preloadNswec([[maybe_unused]] nlohmann::json const& results, tinyxml2::XMLDocument const& zeros, bool includeSeatBooths)
{
const std::map<std::string, std::string> shortCodes = {
{"Shooters, Fishers and Farmers Party (NSW) Incorporated", "SFF"},
{"The Greens NSW", "GRN"},
{"Australian Labor Party (NSW Branch)", "ALP"},
{"Country Labor Party", "ALP"},
{"The Liberal Party of Australia, New South Wales Division", "LNP"},
{"National Party of Australia - NSW", "LNP"}
};
auto contests = zeros.FirstChildElement("MediaFeed")
->FirstChildElement("Election")
->FirstChildElement("House")
->FirstChildElement("Contests");
auto currentContest = contests->FirstChildElement("Contest");
int seatIdCounter = 1;
int candidateIdCounter = 1;
while (currentContest) {
Seat seat;
auto seatIdentifier = currentContest->FirstChildElement("PollingDistrictIdentifier");
seat.name = seatIdentifier->FindAttribute("Id")->Value();
seat.id = seatIdCounter;
++seatIdCounter;
if (seats.contains(seat.id)) seat = seats[seat.id]; // maintain already existing data
seat.enrolment = currentContest->FirstChildElement("Enrolment")->IntText();
auto currentCandidate = currentContest->FirstChildElement("FirstPreferences")->FirstChildElement("Candidate");
while (currentCandidate) {
Candidate candidate;
auto candidateIdentifier = currentCandidate->FirstChildElement("CandidateIdentifier");
candidate.name = candidateIdentifier->FindAttribute("Id")->Value();
candidate.id = candidateIdCounter;
++candidateIdCounter;
auto affiliationEl = currentCandidate->FirstChildElement("Affiliation");
auto affiliationIdentifier = affiliationEl->FirstChildElement("AffiliationIdentifier");
auto affiliationIdEl = affiliationIdentifier->FindAttribute("Id");
if (affiliationIdEl) {
candidate.party = std::stoi(affiliationIdEl->Value());
if (!parties.contains(candidate.party)) {
parties[candidate.party] = Party();
parties[candidate.party].id = candidate.party;
parties[candidate.party].name =
affiliationIdentifier->FirstChildElement("RegisteredName")->GetText();
if (shortCodes.contains(parties[candidate.party].name)) {
parties[candidate.party].shortCode = shortCodes.at(parties[candidate.party].name);
}
}
}
else {
candidate.party = Candidate::Independent;
parties[Candidate::Independent].id = Candidate::Independent;
parties[Candidate::Independent].shortCode = "IND";
parties[Candidate::Independent].name = "Independent";
}
candidates[candidate.id] = candidate;
currentCandidate = currentCandidate->NextSiblingElement("Candidate");
}
auto currentBooth = currentContest->FirstChildElement("PollingPlaces")->FirstChildElement("PollingPlace");
while (currentBooth) {
Booth booth;
booth.id = currentBooth->FirstChildElement("PollingPlaceIdentifier")->IntAttribute("Id");
booth.id += seat.id * 100000; // create unique booth ID for booths with the same name in different seats
booth.name = currentBooth->FirstChildElement("PollingPlaceIdentifier")->Attribute("Name");
booth.parentSeat = seat.id;
booths[booth.id] = booth;
currentBooth = currentBooth->NextSiblingElement("PollingPlace");
if (includeSeatBooths) {
seats[seat.id].booths.push_back(booth.id);
}
}
seats[seat.id] = seat;
currentContest = currentContest->NextSiblingElement("Contest");
}
if (!results.is_null()) {
// A lot of this is just a straight up copy of the Victorian Election
// In summary, the purpose is to match past booth data to current booth data
// The length of the code is primarily to deal with various types of ambiguities
// that can result from booths with the same name under redistributions
// (and can therefore take on vastly different character depening on the
// boundary changes) so these are largely removed unless the booth
// changed cleanly from one seat to another.
std::map<std::string, int> seatNameToId;
for (auto [seatId, seat] : seats) {
if (seatNameToId.contains(seat.name)) {
logger << "Warning: duplicate name for " << seat.name << "!\n";
}
else {
seatNameToId[seat.name] = seatId;
}
}
std::map<std::string, int> candidateNameToId;
for (auto [candidateId, candidate] : candidates) {
if (candidateNameToId.contains(candidate.name)) {
logger << "Warning: duplicate name for " << candidate.name << "!\n";
}
else {
candidateNameToId[candidate.name] = candidateId;
}
}
std::set<std::string> seenBooths;
std::set<std::string> ambiguousBooths;
for (auto [boothId, booth] : booths) {
if (seenBooths.contains(booth.name)) {
ambiguousBooths.emplace(booth.name);
}
else {
seenBooths.emplace(booth.name);
}
}
std::map<std::string, int> boothNameToId;
std::map<std::pair<std::string, std::string>, int> ambiguousBoothNameToId;
for (auto [boothId, booth] : booths) {
if (ambiguousBooths.contains(booth.name)) {
ambiguousBoothNameToId[{booth.name, seats[booth.parentSeat].name}] = boothId;
}
else {
boothNameToId[booth.name] = boothId;
}
}
//PA_LOG_VAR(boothNameToId);
//PA_LOG_VAR(ambiguousBoothNameToId);
int dummyCandidateId = -100000; // Low numbers that will never be mistaken for an official id
int dummyBoothId = -100000; // Low numbers that will never be mistaken for an official id
// Not worth soft-coding this
const std::map<std::string, int> partyIds = {
{"LAB", 2},
{"CDP", 4},
{"LIB", 7},
{"GRN", 16},
{"CLP", 5},
{"NAT", 8},
{"NP", 8},
{"SFF", 17},
{"SA", 14},
{"KSO", 979},
{"SAP", 857},
{"AJP", 376},
{"LDP", 310},
{"AC", 837},
{"PHON", 937},
{"SBP", 957},
{"UP", -1},
{"ORP", -1},
{"NLT", -1},
{"ACP", -1},
{"FLUX", -1},
{"IND", -1}
};
std::set<int> matchedIds;
for (auto const& [seatName, seatValue] : results.items()) {
int seatId = -1;
if (seatNameToId.contains(seatName)) {
seatId = seatNameToId[seatName];
}
std::map<int, int> indexToId;
for (auto const& [candIndex, candValue] : seatValue["candidates"].items()) {
int candIndexI = std::stoi(candIndex);
auto candidateName = candValue["name"];
auto party = candValue["party"];
// *** json file only contains the surname, need to fix
// (DoP file has the full name)
if (candidateNameToId.contains(candidateName)) {
indexToId[candIndexI] = candidateNameToId[candidateName];
}
else {
indexToId[candIndexI] = dummyCandidateId;
Candidate candidate;
candidate.id = dummyCandidateId;
candidate.name = candidateName;
if (!partyIds.contains(party)) PA_LOG_VAR(seatName);
if (!partyIds.contains(party)) PA_LOG_VAR(party);
candidate.party = partyIds.at(party);
candidates[candidate.id] = candidate;
--dummyCandidateId;
}
}
for (auto const& [boothName, boothValue] : seatValue["booths"].items()) {
if (boothName.find("Votes") != std::string::npos) {
VoteType voteType = VoteType::Invalid;
if (boothName == "Postal Votes") voteType = VoteType::Postal;
if (boothName == "Absent Votes") voteType = VoteType::Absent;
if (boothName == "Provisional Votes") voteType = VoteType::Provisional;
if (boothName == "iVote Votes") voteType = VoteType::IVote;
if (voteType == VoteType::Invalid) continue;
auto fps = boothValue["fp"];
for (auto const& [fpCandIndex, fpVotes] : fps.items()) {
int fpCandIndexI = std::stoi(fpCandIndex);
int fpCandId = indexToId[fpCandIndexI];
seats[seatId].fpVotes[fpCandId][voteType] += fpVotes;
}
auto tcps = boothValue["tcp"];
for (auto const& [tcpCandIndex, tcpVotes] : tcps.items()) {
int tcpCandIndexI = std::stoi(tcpCandIndex);
int tcpCandId = indexToId[tcpCandIndexI];
int tcpAffiliation = candidates[tcpCandId].party;
seats[seatId].tcpVotes[tcpAffiliation][voteType] += tcpVotes;
}
continue;
}
int boothId = dummyBoothId;
if (boothNameToId.contains(boothName)) {
boothId = boothNameToId[boothName];
}
else if (ambiguousBoothNameToId.contains({ boothName, seatName })) {
boothId = ambiguousBoothNameToId[{ boothName, seatName }];
}
else {
Booth booth;
booth.name = boothName;
booth.id = dummyBoothId;
booths[booth.id] = booth;
--dummyBoothId;
}
if (matchedIds.contains(boothId)) {
// If two "old" booths match to one "new" booth then we don't know
// which "old" booth to actually compare to
// so ... make new booths that won't match to either
if (booths.contains(boothId)) {
auto boothData = booths[boothId];
boothData.id = dummyBoothId;
booths.erase(boothId);
booths[dummyBoothId] = boothData;
auto& parent = seats[booths[dummyBoothId].parentSeat];
//PA_LOG_VAR(parent.name);
//PA_LOG_VAR(parent.booths);
//PA_LOG_VAR(boothId);
//PA_LOG_VAR(dummyBoothId);
auto foundBoothId = std::find(parent.booths.begin(), parent.booths.end(), boothId);
if (foundBoothId != parent.booths.end()) *foundBoothId = dummyBoothId;
//PA_LOG_VAR(parent.booths);
//logger << "Detached old ambiguous booth: " << booths[dummyBoothId].name << "\n";
--dummyBoothId;
}
boothId = dummyBoothId;
//logger << "Detached ambiguous booth: " << boothName << "\n";
--dummyBoothId;
}
matchedIds.insert(boothId);
Booth& booth = booths[boothId];
// the parent seat refers to the seat this booth is in for the new election,
// but it should be the one it is in for this (old) election
booth.parentSeat = seatId;
booth.id = boothId;
booth.name = boothName;
auto fps = boothValue["fp"];
for (auto const& [fpCandIndex, fpVotes] : fps.items()) {
int fpCandIndexI = std::stoi(fpCandIndex);
int fpCandId = indexToId[fpCandIndexI];
booth.fpVotes[fpCandId] = fpVotes;
if (seatId > 0) seats[seatId].fpVotes[fpCandId][VoteType::Ordinary] += fpVotes;
}
auto tcps = boothValue["tcp"];
for (auto const& [tcpCandIndex, tcpVotes] : tcps.items()) {
int tcpCandIndexI = std::stoi(tcpCandIndex);
int tcpCandId = indexToId[tcpCandIndexI];
int tcpAffiliation = candidates[tcpCandId].party;
booth.tcpVotes[tcpAffiliation] = tcpVotes;
if (seatId > 0) seats[seatId].tcpVotes[tcpAffiliation][VoteType::Ordinary] += tcpVotes;
}
auto tpps = boothValue["tpp"];
for (auto const& [tppCandIndex, tppVotes] : tpps.items()) {
int tppCandIndexI = std::stoi(tppCandIndex);
int tppCandId = indexToId[tppCandIndexI];
int tppAffiliation = candidates[tppCandId].party;
booth.tppVotes[tppAffiliation] = tppVotes;
if (seatId > 0) seats[seatId].tppVotes[tppAffiliation][VoteType::Ordinary] += tppVotes;
}
if (seatId > 0) {
seats[seatId].booths.push_back(booth.id);
}
}
}
logger << "==SEATS==\n";
for (auto const& [seatId, seat] : seats) {
PA_LOG_VAR(seat.name);
PA_LOG_VAR(seat.id);
PA_LOG_VAR(seat.enrolment);
PA_LOG_VAR(seat.fpVotes);
PA_LOG_VAR(seat.tcpVotes);
PA_LOG_VAR(seat.tppVotes);
for (int boothId : seat.booths) {
PA_LOG_VAR(boothId);
auto const& booth = booths.at(boothId);
PA_LOG_VAR(booth.id);
PA_LOG_VAR(booth.name);
PA_LOG_VAR(booth.fpVotes);
PA_LOG_VAR(booth.tcpVotes);
PA_LOG_VAR(booth.type);
}
}
logger << "==CANDIDATES==\n";
for (auto const& candidate : candidates) {
PA_LOG_VAR(candidate.second.id);
PA_LOG_VAR(candidate.second.name);
if (candidate.second.party != -1) {
PA_LOG_VAR(parties[candidate.second.party].name);
}
else {
logger << "Independent\n";
}
}
logger << "==PARTIES==\n";
for (auto const& party : parties) {
PA_LOG_VAR(party.second.id);
PA_LOG_VAR(party.second.name);
PA_LOG_VAR(party.second.shortCode);
}
logger << "Appeared to successfully load past election data\n";
}
}
void Results2::Election::update(tinyxml2::XMLDocument const& xml, Format format)
{
PA_LOG_VAR(xml.FirstChildElement()->Name());
auto resultsFinder = [&]() {
switch (format) {
case Format::AEC: return xml.FirstChildElement("MediaFeed")->FirstChildElement("Results");
case Format::VEC: return xml.FirstChildElement("MediaFeed");
case Format::NSWEC: return xml.FirstChildElement("MediaFeed");
default: return xml.FirstChildElement("MediaFeed")->FirstChildElement("Results");
}
};
auto results = resultsFinder();
auto getElectionInfo = [&]() {
switch (format) {
case Format::NSWEC:
{
auto electionEl = results->FirstChildElement("EventIdentifier");
id = std::stoi(std::string(electionEl->FindAttribute("Id")->Value()).substr(2));
name = electionEl->FirstChildElement("EventName")->GetText();
break;
}
default:
{
auto electionEl = results->FirstChildElement("eml:EventIdentifier");
id = electionEl->FindAttribute("Id")->IntValue();
name = electionEl->FirstChildElement("eml:EventName")->GetText();
}
}
};
getElectionInfo();
auto contests = results->FirstChildElement("Election")->FirstChildElement("House")->FirstChildElement("Contests");
auto currentContest = contests->FirstChildElement("Contest");
// For elections (like NSW) where seats and candidates aren't given their own id numbers
int seatIdCounter = 1;
int candidateIdCounter = 1;
std::map<std::string, int> candidateNameToId;
std::string candidateIdElName = format == Format::NSWEC ? "CandidateIdentifier" : "eml:CandidateIdentifier";
while (currentContest) {
Seat seat;
auto contestIdFinder = [&]() {
switch (format) {
case Format::AEC: return currentContest->FirstChildElement("eml:ContestIdentifier");
case Format::VEC: return currentContest->FirstChildElement("PollingDistrictIdentifier");
case Format::NSWEC: return currentContest->FirstChildElement("PollingDistrictIdentifier");
default: return currentContest->FirstChildElement("eml:ContestIdentifier");
}
};
auto contestIdEl = contestIdFinder();
if (format == Format::NSWEC) {
seat.id = seatIdCounter;
++seatIdCounter;
}
else {
seat.id = contestIdEl->FindAttribute("Id")->IntValue();
}
if (seats.contains(seat.id)) seat = seats[seat.id]; // maintain already existing data
auto nameFinder = [&]() -> std::string {
switch (format) {
case Format::AEC: return contestIdEl->FirstChildElement("eml:ContestName")->GetText();
case Format::VEC: {
std::string prelimText = contestIdEl->FirstChildElement("Name")->GetText();
return prelimText.substr(0, prelimText.length() - 9);
}
case Format::NSWEC: return contestIdEl->FindAttribute("Id")->Value();
default: return contestIdEl->FirstChildElement("eml:ContestName")->GetText();
}
};
seat.name = nameFinder();
seat.enrolment = std::stoi(currentContest->FirstChildElement("Enrolment")->GetText());
auto firstPrefs = currentContest->FirstChildElement("FirstPreferences");
auto currentCandidate = firstPrefs->FirstChildElement("Candidate");
while (currentCandidate) {
Candidate candidate;
auto candidateIdEl = currentCandidate->FirstChildElement(candidateIdElName.c_str());
if (format == Format::NSWEC) {
candidate.id = candidateIdCounter;
std::string candidateName = candidateIdEl->FirstChildElement("CandidateName")->GetText();
candidateNameToId[candidateName] = candidate.id;
++candidateIdCounter;
} else {
candidate.id = candidateIdEl->FindAttribute("Id")->IntValue();
}
// Any candidate/party data should already be preloaded for NSWEC
if (format != Format::NSWEC && candidateIdEl->FirstChildElement("eml:CandidateName")) {
candidate.name = candidateIdEl->FirstChildElement("eml:CandidateName")->GetText();
auto affiliationEl = currentCandidate->FirstChildElement("eml:AffiliationIdentifier");
if (affiliationEl) {
candidate.party = affiliationEl->FindAttribute("Id")->IntValue();
if (!parties.contains(candidate.party)) {
parties[candidate.party] = Party();
parties[candidate.party].id = candidate.party;
parties[candidate.party].name =
currentCandidate->FirstChildElement("eml:AffiliationIdentifier")->FirstChildElement("eml:RegisteredName")->GetText();
parties[candidate.party].shortCode = affiliationEl->FindAttribute("ShortCode")->Value();
}
}
else candidate.party = Candidate::Independent;
parties[Candidate::Independent].id = Candidate::Independent;
parties[Candidate::Independent].shortCode = "IND";
parties[Candidate::Independent].name = "Independent";
candidates[candidate.id] = candidate;
}
auto votesByType = currentCandidate->FirstChildElement("VotesByType");
auto fpVoteType = votesByType->FirstChildElement("Votes");
while (fpVoteType) {
std::string typeName = fpVoteType->FindAttribute("Type")->Value();
int fpCount = fpVoteType->GetText() ? std::stoi(fpVoteType->GetText()) : 0;
if (typeName == "Ordinary" || typeName == "PP" || typeName == "PR") seat.fpVotes[candidate.id][VoteType::Ordinary] += fpCount;
else if (typeName == "Absent") seat.fpVotes[candidate.id][VoteType::Absent] = fpCount;
else if (typeName == "Provisional" || typeName == "Enrolment/Provisional") seat.fpVotes[candidate.id][VoteType::Provisional] = fpCount;
else if (typeName == "PrePoll") seat.fpVotes[candidate.id][VoteType::PrePoll] = fpCount;
else if (typeName == "Postal") seat.fpVotes[candidate.id][VoteType::Postal] = fpCount;
else if (typeName == "Early") seat.fpVotes[candidate.id][VoteType::Early] = fpCount;
else if (typeName == "iVote") seat.fpVotes[candidate.id][VoteType::IVote] = fpCount;
fpVoteType = fpVoteType->NextSiblingElement("Votes");
}
currentCandidate = currentCandidate->NextSiblingElement("Candidate");
}
auto candidateIdFinder = [&]() {
switch (format) {
case Format::NSWEC:
{
auto candidateIdEl = currentCandidate->FirstChildElement("CandidateIdentifier");
return candidateNameToId[candidateIdEl->FindAttribute("Id")->Value()];
}
default:
{
auto candidateIdEl = currentCandidate->FirstChildElement("eml:CandidateIdentifier");
return candidateIdEl->FindAttribute("Id")->IntValue();
}
}
};
auto tcps = currentContest->FirstChildElement("TwoCandidatePreferred");
if (tcps) {
currentCandidate = tcps->FirstChildElement("Candidate");
while (currentCandidate) {
auto candidateId = candidateIdFinder();
int partyId = candidates.at(candidateId).party;
auto votesByType = currentCandidate->FirstChildElement("VotesByType");
auto tcpVoteType = votesByType->FirstChildElement("Votes");
while (tcpVoteType) {
std::string typeName = tcpVoteType->FindAttribute("Type")->Value();
int tcpCount = tcpVoteType->GetText() ? std::stoi(tcpVoteType->GetText()) : 0;
// Note: for NSWEC the PP/PR categories are always zero - will need to extract them from the booth data
if (typeName == "Ordinary" || typeName == "PP" || typeName == "PR") seat.tcpVotes[partyId][VoteType::Ordinary] = tcpCount;
else if (typeName == "Absent") seat.tcpVotes[partyId][VoteType::Absent] = tcpCount;
else if (typeName == "Provisional" || typeName == "Enrolment/Provisional") seat.tcpVotes[partyId][VoteType::Provisional] = tcpCount;
else if (typeName == "PrePoll") seat.tcpVotes[partyId][VoteType::PrePoll] = tcpCount;
else if (typeName == "Postal") seat.tcpVotes[partyId][VoteType::Postal] = tcpCount;
else if (typeName == "Early") seat.tcpVotes[partyId][VoteType::Early] = tcpCount;
else if (typeName == "iVote") seat.tcpVotes[partyId][VoteType::IVote] = tcpCount;
tcpVoteType = tcpVoteType->NextSiblingElement("Votes");
}
currentCandidate = currentCandidate->NextSiblingElement("Candidate");
}
}
auto boothsEl = currentContest->FirstChildElement("PollingPlaces");
auto currentBooth = boothsEl->FirstChildElement("PollingPlace");
while (currentBooth) {
auto boothIdEl = currentBooth->FirstChildElement("PollingPlaceIdentifier");
Booth booth;
booth.id = boothIdEl->FindAttribute("Id")->IntValue();
if (format == Format::NSWEC) booth.id += seat.id * 100000; // create unique booth ID for booths with the same name in different seats
if (booths.contains(booth.id)) booth = booths[booth.id]; // maintain already existing data
if (boothIdEl->FindAttribute("Name")) {
booth.parentSeat = seat.id;
booth.name = boothIdEl->FindAttribute("Name")->Value();
if (booth.name == "Absent" || booth.name == "Enrolment/Provisional" ||
booth.name == "Postal" || booth.name == "iVote")
{
currentBooth = currentBooth->NextSiblingElement("PollingPlace");
continue;
}
if (booth.name == "Declared Facility" || booth.name == "Sydney Town Hall") {
booth.name += " (" + seat.name + ")";
}
auto classifierEl = boothIdEl->FindAttribute("Classification");
if (classifierEl) {
std::string classifier = classifierEl->Value();
if (classifier == "PrePollVotingCentre") booth.type = Booth::Type::Ppvc;
if (classifier == "PrisonMobile") booth.type = Booth::Type::Prison;
if (classifier == "SpecialHospital") booth.type = Booth::Type::Hospital;
if (classifier == "RemoteMobile") booth.type = Booth::Type::Remote;
// These are technically PPVCs but they're very volatile and shouldn't be used as a proxy for normal PPVCs.
if (booth.name.find("Divisional Office") != std::string::npos) booth.type = Booth::Type::Other;
if (booth.name.find("BLV") != std::string::npos) booth.type = Booth::Type::Other;
}
seat.booths.push_back(booth.id);
}
auto fps = currentBooth->FirstChildElement("FirstPreferences");
currentCandidate = fps->FirstChildElement("Candidate");
while (currentCandidate) {
int candidateId = candidateIdFinder();
int votes = currentCandidate->FirstChildElement("Votes")->IntText();
booth.fpVotes[candidateId] = votes;
currentCandidate = currentCandidate->NextSiblingElement("Candidate");
}
tcps = currentBooth->FirstChildElement("TwoCandidatePreferred");
if (tcps) {
currentCandidate = tcps->FirstChildElement("Candidate");
while (currentCandidate) {
int candidateId = candidateIdFinder();
int partyId = candidates.at(candidateId).party;
int votes = currentCandidate->FirstChildElement("Votes")->IntText();
booth.tcpVotes[partyId] = votes;
currentCandidate = currentCandidate->NextSiblingElement("Candidate");
}
}
booths[booth.id] = booth;
currentBooth = currentBooth->NextSiblingElement("PollingPlace");
}
seats[seat.id] = seat;
currentContest = currentContest->NextSiblingElement("Contest");
}
logger << "==SEATS==\n";
for (auto const& [seatId, seat] : seats) {
PA_LOG_VAR(seat.name);
PA_LOG_VAR(seat.id);
PA_LOG_VAR(seat.enrolment);
PA_LOG_VAR(seat.fpVotes);
PA_LOG_VAR(seat.tcpVotes);
PA_LOG_VAR(seat.tppVotes);
for (int boothId : seat.booths) {
auto const& booth = booths.at(boothId);
PA_LOG_VAR(booth.id);
PA_LOG_VAR(booth.name);
PA_LOG_VAR(booth.fpVotes);
PA_LOG_VAR(booth.tcpVotes);
PA_LOG_VAR(booth.tppVotes);
PA_LOG_VAR(booth.type);
}
}
logger << "==CANDIDATES==\n";
for (auto const& candidate : candidates) {
PA_LOG_VAR(candidate.second.id);
PA_LOG_VAR(candidate.second.name);
if (candidate.second.party != -1) {
PA_LOG_VAR(parties[candidate.second.party].name);
}
else {
logger << "Independent\n";
}
}
logger << "==PARTIES==\n";
for (auto const& party : parties) {
PA_LOG_VAR(party.second.id);
PA_LOG_VAR(party.second.name);
PA_LOG_VAR(party.second.shortCode);
}
logger << "==COALITIONS==\n";
for (auto const& coalition : coalitions) {
PA_LOG_VAR(coalition.second.id);
PA_LOG_VAR(coalition.second.name);
PA_LOG_VAR(coalition.second.shortCode);
}
logger << "==ELECTION==\n";
PA_LOG_VAR(name);
PA_LOG_VAR(id);
}