A couple of days poking around on the internet revealed a few things:
- There was existing software for generating cartograms.
- Some of it was in the form of free add-ons for commercial packages. The cost of the commercial packages was larger than the household research budget was comfortable with .
- Some of the available code wouldn't run on my Mac.
- There was at least one free standalone package written in Java that would run on my Mac. I disliked its graphical user interface and slow response. (That should not be construed as a slight on the developers or their code; it just wasn't right for me.)
- There was at least one of the better algorithms with source code available from the original authors that "just" needed a wrapper to make use of it.
The first thing I needed was map data. A map like I wanted was just a set of polygons. Wikipedia to the rescue! Tucked away in the Wikimedia Commons was an SVG (Scalable Vector Graphics) file that provided polygon outlines for the 50 US states. I had used SVG on a couple of other occasions so knew that extracting the outlines only took a small chunk of Perl code. I created my own very simple format for a map file anticipating that I might want to convert from other sources in the future. It was intended to be a placeholder, but I'm still using it today.
I needed to draw the polygons, both outlines and filled. The GD Graphics Library has been around for a long time. GD had the advantage that it was oriented towards drawing in memory and then rendering to a file; newer alternatives were focused much more towards rendering in real time in a video frame buffer. GD is written in C, but people have written bindings to the library for many other programming languages. I do most of my programming in Perl , and GD.pm was readily available. When I drew the polygons from the Wikimedia file there were some problems with the data. I ended up with a "check outlines" program that did simple drawings of specified subsets of the map data so that I could identify and fix problems.
For doing the contiguous cartogram transformation, I chose the Gastner and Newman gas-diffusion density-equalization method . The results were attractive and Mark Newman was distributing open source code. The cart program took an initial density file and produced an intermediate result – a representation of the transform from initial density to equalized density. The interp program used that intermediate file and a list of point coordinates – say, the points defining a polygon – and produced a list of the transformed coordinates. Draw the transformed polygons and the result is a contiguous cartogram. It wasn't quite as simple as that sounds. For example, I had to figure out how to generate a suitable initial density file. Still, after a couple of weeks of part-time effort, I could draw a version of the cartogram shown above.
The formal study of map projections – representing the surface of a sphere, or part of that surface, on a piece of paper – is a very old subject, going back centuries. The PROJ.4 project provides the open source program cs2cs that will translate from one coordinate system to another for almost every coordinate system known (or at least in somewhat common usage). The cs2cs program made it possible to take the latitude-longitude data extracted from the shapefile and produce nicely oriented maps using an equal-area projection (second figure down).
Extreme distortion in some cartograms created its own problems. In particular, many of the lines looked pretty ratty. Solving that required a combination of adding intermediate points along the edges of the polygons and finding ways to get around the GD library's anti-aliasing restrictions. By this point there were lots of picky little details about which outlines to use where, what colors, the thickness of the lines, etc. The "wrapper" was accepting a lot of command-line options in order to deal with those. It still does, although it will read the options from a file in addition to any specified on the command line. The resulting cartograms seemed to be reasonably attractive even in extreme cases (third figure down).
Counties, particularly in the coastal states of the US, suffer from irregular shapes and sizes. Some western counties are larger than some eastern states. California's largest county is almost 500 times the area of the smallest county. For some purposes, replacing county outlines with a uniform mesh (sometimes called a grid in the literature) provided more useful results. Getting a smooth rendition of the lines meant splines; the GD::Polyline module added a nice spline capability to the GD library.
Sometimes, particularly where there are extreme differences in density, cartograms are difficult to read to the point that they become unuseful. Prism maps are an alternative. The same basic approach – let the "wrapper" generate the necessary description and have an existing piece of software do the heavy lifting – made it straightforward to add simple prism maps. In this case, the heavy lifting was done by POV-Ray, an open source ray-tracing 3D renderer . 3D rendering opens up a whole new set of parameters to play with: camera and light placement, transparency of objects, etc. Sometimes those produce visually interesting results. The bottom of the several images shows an example, with the camera positioned near the base on which the prisms rest, looking slightly north of due west, showing the Kansas City suburbs and Omaha along the near edge, Wichita a bit farther west, and the Denver metro area towering in the distance, hundreds of miles away.
The difficulties aren't just a matter of drawing the maps. I think everyone with any experience that I've discussed the problems with would agree with the statement, "Mapping is straightforward; data is hard." Just matching up identifiers can be difficult. Most of my early cartograms used Wikipedia data with states identified by postal abbreviations. County-level data was identified by name. This becomes a problem when I needed to combine multiple data sources. The most common approach is to use FIPS (Federal Information Processing Standards) codes. Even then there are problems. South Dakota – fairly recently as these things go – changed a county name, so the federal government changed its FIPS code. The "wrapper" tries to catch this and change everything to the new code.
Things didn't really happen in exactly the order described above, of course. As with most unplanned personal projects, things got added when they become desirable, problems got fixed when they got discovered (or became obnoxious enough), etc. The "wrapper" is, as I write this, about 2500 lines of Perl (including comments and vertical white space). About 2300 lines of that is the code; the remainder is a file with a collection of look-up tables with static information that has been useful from time to time. An example of that kind of information is a Perl hash that converts two-character standard state abbreviations into the corresponding FIPS code (eg, from Florida's "FL" to "12"). The code isn't particularly well structured. The style isn't particularly consistent throughout. (What? Your coding style doesn't change at all over five years?) A serious Perl stylist would probably be appalled by some of my abuses of the language, regardless of the Perl slogan that "There's more than one way to do it".
 One of the things that I frequently say about being retired: the upside is that I can choose the questions I want to study and how to structure the research; the downside is that funding is considerably more difficult. ↩
 "Diffusion-based method for producing density equalizing maps," Michael T. Gastner and M. E. J. Newman, Proc. Natl. Acad. Sci. USA 101, 7499-7504 (2004). ↩
 There really was a 51st State movement in Colorado. They were, and perhaps still are, a registered 501(c) nonprofit organization so that donations were tax deductible and they could legally lobby the state legislature. ↩
 Somewhat embarrassingly, I found POV-Ray when I was having trouble finding contemporary software that I thought would meet my needs – like the drawing libraries, the emphasis is on fast rendering to a frame buffer – and asked myself the question, "What software would I have used if I were doing this 20 years ago?" ↩
Next: A Small Example
Last updated Mar 4, 2018