PHP Heat map creator

1. Description and info

Heat map generator (create_heatmap.php) is a PHP command line tool for generating translucent heat map images from XML data. The images are created in PNG format.

Download the project

Created by Jussi Männistö.

2. Usage

php create_heatmap.php [xml filename] [width] [height] [translucency] [top latitude] [left longitude] [bottom latitude] [right longitude] [image output filename]

Example: php create_heatmap.php example.xml 600 600 70 65.032597 25.409946 64.983134 25.528049 output.png

2.1. Parameters

2.1.1. XML filename

The XML file must be a well-formed XML document with a root element named dataResponse (for compatibility with the panOULU mobility analytics data interface). The dataResponse element must contain an element named locations, which may contain 0..n location-elements. Each location element has an optional id attribute and three mandatory child elements in a sequence: latitude, longitude and users. Latitude and longitude tags contain the Google Maps coordinate values of the location given as decimal values. The users tag contains the number of users in that location, given as an integer.

Each location element may also contain a touristPercentage tag. This tag is optional and does nothing in this version of the heat map generator.

View an example XML file here
View the XML schema definition here

2.1.2. Width, height

These arguments define the width and height of the output image. Both dimensions must be positive.

2.1.3. Translucency

This argument defines the translucency of the output image. Acceptable range is 0..127, where 0 is completely opaque and 127 is completely transparent.

2.1.4. Top latitude, Left longitude, Bottom latitude, Right longitude

These arguments define the bounds of the heat map area in Google Maps coordinates. These arguments are given as decimal values.

2.1.5. Image output filename

The output filename of the image. A path may be included.

3. Heat map generation process

The generator first creates a pixel grid with the given width and height dimensions. Each point on the grid represents a pixel on the final image. The generator then goes through all location elements in the given XML document, transforms coordinates of each location to a point on the grid and adds the user value of the location to that point (values cumulate). Points residing outside the defined map area are ignored. When users from all the locations have been added to the grid, the grid is a collection of points with different user values. See the image below.

pixels

The grid can at this point be thought of as a "landscape" with pillars of different altitudes: high user value on a point signifies a high pillar and zero value is the base level. The "landscape" needs to be smoothened so that that the heat map becomes more map-like. This is done by recursively shaping the landscape

A function called smoothenDrops is called on grid points with non-zero altitude. The function does the following:

  1. Checks the adjacent pixels and determines whether the "altitude" or "heat" drop from this pixel to the adjacent is too big
  2. If the drop is too big, raise the adjacent pixel so that it isn't and make a recursive call on the raised pixel

This operation smoothens the "landscape" nicely. See the image below.

smoothened

After the grid values are smoothened, colors are assigned for each pixel on the output image based on the "heat" or "altitude" value of that grid point. The translucency effect is then added by manipulating the alpha channel of each pixel in the image. After this the image is saved to the disk and the process is complete.