Posts tagged ‘’

Applying weighting to heatmaps

Heatmap with weighting applied

Following up on the playing around I was having with heatmaps the other day, I made a few updates to the script to allow for each point to have an associated ‘intensity’. When the points are plotted, the intensities are normalised and each point plotted with a relative intensity (as shown in the image on the right).
Few other little changes I made:

  • allow the area covered by the size of the final image to be specified, rather than using the max & min x/y coordinates from the input points
  • change how the the dots are built – in theory should be a little quicker – but I’ve not tested with large enough dataset to know if it makes a big difference
  • in my demo script I’ve shown how you can convert the latitude to its Mercartor projection coordinate

For those interested in the changes I made to the script you can download the diff and an example script. Any feedback, comments welcome 🙂

Playing with heatmaps

My 'hello world' heatmap

Over that past few days I’ve been playing around with creating heatmaps to overlay onto OpenStreetMap – the image on the left is where I’ve got to so far. My interest in this is for creating heatmaps from data collected during my colleagues health sciences research in Ethiopia.

There are already several programs and services available to create heatmaps, for example gheat and OpenHeatMap. But none of these quite suited me, gheat because I didn’t want to create a full tile server – just an image to overlay on a particular area of the map and OpenHeatMap because I wanted to have access to the code to tweak how I wanted.

I then found which (using a similar algorithm to gheat) did almost exactly what I wanted. There were only a couple of changes that I needed to make:

1) Update to account for the Mercator projection – I wanted my overlay to be on a view of the whole world (zoom level 2 in OpenStreetMap). When I first ran the program the areas over northern Europe (and others) were almost but not quite inline with the marker overlays, but this was due to the projection. I just edited the input script to convert my lat/lng coordinates into Mercator coordinates. I also hooked up the script to read the lat/lng coordinated from a MySQL database.

2) Allow the script to have weights against each point. The current script looks at the number of points in a particular area (or on top of each other) to generate the ‘heat’. I also wanted to allow for points to have a weight – as mentioned here. I’ve not yet implemented the weighting, but I’m not anticipating this to be too tricky. GHeat (as far as I can tell) doesn’t allow for weights on points, but OpenHeatMap does (please let me know if I’m wrong about this).

All has been much easier than I’d expected, I’ve learnt a little more about python and once I’ve got the weighting working how I’d like, then I’ll share the code back.