This is a follow-up to my post last year about simplistic heat-maps using Vecto. To recap, I’m trying to make heat maps for google maps overlays.

Here’s how it works in a nutshell:

From javascript I pass to the server the lat/lng region currently shown on the google map, and what size heat map to generate, in pixels. lisp pulls weights from my database within the given lat/lng region lisp iterates over the db results, mapping lat/lng to x/y coordinates for the final heat map image lisp uses the list of mapped (x y weight) to draw the heat map in png javascript throws the png on top of the google map

I tried a few things based upon the comments I got back from the helpful lisp community.

used zpng to get direct pixel access, and calculated each pixel’s color using a weighted average of nearby points using distance. This didn’t produce good images, and was pretty slow.

used zpng to get direct pixel access, and calculated each pixel’s color using the gravity formula against nearby points. This didn’t produce good images, and was very slow.

I did some more research and learned about the Generic Mapping Tools and bicubic interpolation. The GMT is a set of C programs, similar to the Imagemagick suite. GMT showed one way to draw heat maps in the Image Presentations tutorial. It spoke of gridded data sets, and that gave me one more vecto-based idea: split the desired heat-map into a grid and color each square in the grid based upon an average of the weights mapped in that square. This is a neat effect, but not what I was going for:

This is reasonably fast, taking about 1 second on my dev server. To quickly find what weights belong in which grid square, I make a spatial index of all the weights, using an r-tree from the spatial-trees library.

The next method I tried was to use interpolation to get a smooth look. I found Cyrus Harmon‘s ch-image library supports image interpolation, and got to it. As Patrick Stein noted elsewhere, ch-image isn’t easy to install. It’s not asdf-installable, and the project page doesn’t list all its dependencies. For future reference, here’s what I think I needed to install it:

(asdf-install:install "http://cyrusharmon.org/static/releases/ch-asdf_0.2.14.tar.gz") (asdf-install:install "http://cyrusharmon.org/static/releases/ch-util_0.3.10.tar.gz") (asdf-install:install "http://cyrusharmon.org/static/releases/smarkup_0.4.2.tar.gz") (asdf-install:install "http://mirror.its.uidaho.edu/pub/savannah/cl-bibtex/cl-bibtex-1.0.1.tar.gz") (asdf-install:install "http://cyrusharmon.org/static/releases/clem_0.4.1.tar.gz") (asdf-install:install "http://cyrusharmon.org/static/releases/ch-image_0.4.1.tar.gz")

Armed with ch-image, now the drawing process becomes:

draw a small image, coloring pixels based upon weights enlarge the small image with interpolation

The first step is very similar to the code I wrote to make the grid version above. Instead of drawing a rectangle, I draw a pixel using ch-image’s pixel access functions. This was a little weird because ch-image’s coordinate system has 0,0 at the top left of the image. I’m still not sure how to best choose the size of this smaller image, but ultimately it should depend on my data. For now I just have it hard-coded be 20x smaller than the desired size:

Yep, that’s pretty small. Applying a transform to scale it up to the desired size using bilinear interpolation yields:

It looks pretty good and takes about a half-second to draw. If you click into the larger version, you can see some discontinuities in there, which is a well-known result of bilinear interpolation. However, based upon other graphics I’ve seen, what I really want is bicubic interpolation. Luckily, ch-image has this built in:

Oops, maybe not so luckily. I can certainly see the kinds of look I’m wanting in all the garbled stuff, but ch-image is freaking out somewhere there.

Bilinear it is! Here’s a screenshot of the overlay in place on the map:

It’s pretty fast, and looks pretty nice, and is fairly close to the look I wanted. I probably still have some off-by-one errors somewhere, and need to check the git repos for the ch-* libs to see if there might be newer versions than the tarballs I installed. I still count this as great progress for 5 hours of coding and research. Huzzah for the much-maligned lisp libraries!