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.
Created by Jussi Männistö.
php create_heatmap.php [xml filename] [width] [height] [translucency] [top latitude] [left longitude] [bottom latitude] [right longitude] [image output filename]
php create_heatmap.php example.xml 600 600 70 65.032597 25.409946 64.983134 25.528049 output.png
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
These arguments define the width and height of the output image. Both dimensions must be positive.
This argument defines the translucency of the output image. Acceptable range is 0..127, where 0 is completely opaque and 127 is completely transparent.
These arguments define the bounds of the heat map area in Google Maps coordinates. These arguments are given as decimal values.
The output filename of the image. A path may be included.
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.
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:
This operation smoothens the "landscape" nicely. See the image below.
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.