Right now I’m working on visualising the Dutch election results (parliament, local etc.) in Google Maps. It’s far from done but this might will serve as the documentation of my efforts for myself and hopefully for others as well. This also the reason being this post is in English.
My goal is relatively simple, visualise turnout, votes for party X as percentage of total votes, winning party etc. per municipality. For that purpose I have collected the results of the elections at http://www.verkiezingsuitslagen.nl/, I understand the Kiesraad is planning to visualise the results themselves but I decided not to wait for that, this little project will probably not be as extensive as theirs. Besides, I have some ideas of my own I’d like to implement.
Next step was a map of Dutch municipalities, in itself not difficult but adapting it to serve my needs was. I had first decided to stick to a map in svg-format (from http://commons.wikimedia.org/wiki/File:Nederland_gemeenten_2007.svg), based on the information by the CBS (The Dutch Bureau of Statistics). In fact this worked rather well, using the jquery-svg library I was able to select specific municipalities and color them. I might even go back to using that. Nonetheless, the thought of using Google Maps for this experiment prevailed.
I downloaded the borders of municipalities in the Netherlands from the CBS and then the trouble started. I have no experience using Geodata whatsoever and figured it would be easy peasy from here on. Trouble however was that the CBS map uses a different projection that Google Maps. The projection is basically the way in which the earth is projected onto a 2d-surface. On maps Greenland is about the same size as South-America, but in fact it’s 8 times smaller, the projection is what’s causing that because there will always be distortions when you try to put a sphere on a flat piece of paper. Pretty much each country has its own projection for domestic uses and I needed to translate the Dutch one (Rijksdriehoekcoordinaten) to the one used by Google Maps.
I tried several programs across three different platforms (my native Os X, virtualized Windows 7 and Ubuntu running on a laptop) to achieve this. I tried ogr2ogr commandline tool to convert a shape-file (.shp) delivered by CBS. It failed and only then I found out about the projections. I moved on.
One excellent piece of open-source software MapWindow GIS, I first tried to use it to convert the projection of the shape-files but it failed on me. Still, I’m not sure what I did wrong. Nevertheless, it looked great and MapWindow came in handy a little later. I installed a bunch of stuff to no avail until I stumbled on Quantum G (Qgis). There was a version for OS X, Windows and Linux. What could go wrong? The OS X version worked pretty neat, till the conversion to the Google Maps projection where it crashed. The Windows version wouldn’t install and the Linux version missed the projection-conversion menu. I went back to the OS X version, fiddled with some settings and this time it worked. What did I do? (this is the documentation for myself since I won’t remember this in a year).
First, open the shapefile and set the projection right (Tools->Data Management->Define current projection). In this case to the Dutch system (Amersfoort/RD New: EPSG 28992). Then go to Tools->Data Management->Export to new projection and choose the Google Maps projection (Popular Visualisation CRS/Mercator:EPSG 3785) Next step, select ‘browse’ to give the filename for the output. In the save dialog, set the encoding to UTF-8. I think this was the cause of it failing on me the first time. Click OK and tada, a shapefile with the right projection.
Now Google Maps still needed to get the data in a format it would understand. This format is the Keyhole Markup Language or KML and many conversion tools exist for it. Qgis also has one (the ogr converter) but it didn’t work as good for me as the Shape2earth plugin in MapWindow GIS. The reason for this is the number of settings in the plugin. In the Shape2earth plugin I was able to include the extra data (# of inhabitants, ages etc.) in the kml-file and set opacity levels. Not sure yet what use it will be but it’s easier to delete than add in later.
This about concludes what has been done so far. What’s left is to delve into the Google Maps API to show the election results and map the election results to the municipalities. It should all be doable. I’m a bit afraid of the speed the application will have in the end, especially since I plan on handling the colouring on the client-side. Possible plans are to animate the election results so you can see the difference between elections visually, and visualise the extra data embedded in the kml files. In any case, here is what I have so far. Google Maps doesn’t show all municipalities with polygons because it only shows the current results, you’ll have to click next. Of course you are free to pose questions on how I did this.
Dutch Municipalities weergeven op een grotere kaart