Turn PEI Election Results into Machine-Readable Data

Elections PEI published results of yesterday’s provincial general election as a collection of HTML tables, which are not well-suited when you’re looking for machine-readable data to do interesting things with. Things like this visualization of the winners of each poll:

Visualization of the winners of every poll in the April 23, 2019 provincial general election

Or this visualization of the party that came second place in every poll:

Visualization showing which party came SECOND PLACE in every poll

Or this visualization of polls where Green candidates came first or second:

Map showing polls where Greens came first OR second

Fortunately, HTML tables are sort of machine-readable to start, and it doesn’t table much to parse them and turn them into something even more machine-readable.

So this morning I wrote some code to do exactly that, and you can download it and take it for a ride or, if you’re just looking for the data itself you can grab one of two files:

It’s that “winners” file that I used to create the visualization above, which you can explore in an interactive version that I’ve just published to the QGIS Cloud.

I combined the CSV file with the Electoral Districts GIS layer. To be able to do this I first needed to create “virtual field” called “distpoll” in the Electoral Districts layer:

Screen shot showing virtual field.

With that virtual field in place, I could join the winners CSV file to the Electoral Districts layer, using the “distpoll” field for the join:

Screen shot showing how the two files are joined.

And with the files joined, I used “rule-based” styles in QGIS to colour-code each poll by the winner (leaving those polls with no winner–those in District 9 in this case, where the election was delayed–white):

Styling the polls by poll winner.

I’m hopeful that others can build on this code to make additional interesting things from the results.

Comments

anonymous's picture
anonymous on April 26, 2019 - 15:22 Permalink

How do you account for the advance polls? Greens lost 22 or 23 of the advance polls, which is represented by each polling area. Can you do a map by advance polls as well?

Peter Rukavina's picture
Peter Rukavina on April 26, 2019 - 19:35 Permalink

Unfortunately advance poll count is published on a district-by-district level (despite all of the voters in the advance polls having “home” polls that they’re attached to by their civic address).

So it’s only be possible to produce advance polls results maps on a district level, not a poll level. Here’s what it looks like:

Map showing advance poll winners

This map is deceptive because it includes polling division boundaries — the province hasn’t yet released a GIS layer that includes only district boundaries (and trying to combined the polling division polygons in QGIS to produce district polygons leaves some annoying leftover bits).

Which is to say that what’s represented on this map are “what districts did each party win the advance poll in”. It’s not a poll-level visualization.