XKCDnerd sniped me this week with a fantastic comic containing an upside down map.
My immediate thought was “I wonder if I can make that?” - After all I’m a geographer I understand how this is done, right?
Turns out it was harder than I thought. Rotating features is pretty easy, all I needed to do was extend some old code I had for a GeoTools function that used an affine transformation to scale geometries, that I used to make discontinuous cartograms, to implement rotation. Unfortunately I couldn’t find that code so I started from scratch using this tutorial. It is actually very easy to extend GeoTools (and hence GeoServer) with new functions.
To start with you declare a class that extends FunctionImpl and provides a static FunctionName that defines the return type and parameter types for the function.
After that it’s a simple case of overriding the evaluate method:
The only tricky part is that you have to move to the centroid of the geometry before you start scaling or rotating the shape otherwise it will use the corner which looks odd. It is a little unwieldy to use at present as you have to specify all 5 parameters even if you only want to rotate the geometry. I plan to tidy it up into four functions before I merge it into the GeoTools code base.
Using the new function requires that you add a couple of files to the resources folder of your project which is all covered in the tutorial. Then you can just use it directly (as I have) or from an SLD file in GeoServer if you prefer. I wrapped the function in some code to read in and write out a shapefile:
So can I recreate XKCD’s map with this function? First I had to find some data, luckily USGS came to my aid with a shapefile of the continents.
My first attempt was nothing like the XKCD map as I had Asia and Europe as separate features, Great Britain was attached to Europe & New Zealand and Micronesia crossed the dateline and rotated way off to the right as a result. However once I had done some splitting and merging in QGis I could generate this map:
As you can see it is not the same as XKCD’s map. The main details are the same, the UK is across from North Korea but South Africa is on top of them. If I really needed to recreate his map I could use QGis to move Africa south and tidy up North America and the Canadian Islands. But I think I’ve been sniped enough so I’ll leave that as an exercise for the reader.