More MySQL purging
[contractdashboard.git] / displayHeatmap.php
blob:a/displayHeatmap.php -> blob:b/displayHeatmap.php
<?php <?php
   
include('lib/common.inc.php'); include('lib/common.inc.php');
   
$year = 2006; $year = 2006;
$ZeroX = 112.5; $ZeroX = 112.5;
$MaxX = 157; $MaxX = 157;
$ZeroY = -9; $ZeroY = -9;
$MaxY = -45; $MaxY = -45;
   
$XRange = $MaxX - $ZeroX; $XRange = $MaxX - $ZeroX;
$YRange = abs($MaxY) - abs($ZeroY); $YRange = abs($MaxY) - abs($ZeroY);
$xdivs = 250; $xdivs = 250;
$xdivlength = $XRange / $xdivs; $xdivlength = $XRange / $xdivs;
$ydivs = 250; $ydivs = 250;
$ydivheight = $YRange / $ydivs; $ydivheight = $YRange / $ydivs;
   
$borderLeft = 100; $borderLeft = 100;
$borderTop = 35; $borderTop = 35;
$width = 497; $width = 497;
$height = 457; $height = 457;
//echo "http://dev.openstreetmap.org/~pafciu17/?module=map&bbox=".$ZeroX.",".$ZeroY.",".$MaxX.",".$MaxY."&width=".$width."&height=".$height; //echo "http://dev.openstreetmap.org/~pafciu17/?module=map&bbox=".$ZeroX.",".$ZeroY.",".$MaxX.",".$MaxY."&width=".$width."&height=".$height;
//$handle = ImageCreate ($width, $height) or die ("Cannot Create image"); //$handle = ImageCreate ($width, $height) or die ("Cannot Create image");
$handle = imagecreatefrompng('images/australia.png'); $handle = imagecreatefrompng('images/australia.png');
$white = imagecolorallocate($handle, 0, 0, 0); $white = imagecolorallocate($handle, 0, 0, 0);
imagecolortransparent($handle, $white); imagecolortransparent($handle, $white);
   
$query = 'SELECT "supplierPostcode", sum("value") as value, max(lat) as lat, max(lon) as lon FROM contractnotice inner join postcodes on "supplierPostcode" = postcode::text where "childCN" is null AND "supplierCountry" = \'Australia\' GROUP BY "supplierPostcode"'; $query = 'SELECT "supplierPostcode", sum("value") as value, max(lat) as lat, max(lon) as lon FROM contractnotice inner join postcodes on "supplierPostcode" = postcode::text where "childCN" is null AND "supplierCountry" = \'Australia\' GROUP BY "supplierPostcode"';
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->execute(); $query->execute();
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
   
   
$left = "FFFF50"; $left = "FFFF50";
$right = "EF0050"; $right = "EF0050";
$leftR = hexdec(substr($left, 0, 2)); $leftR = hexdec(substr($left, 0, 2));
$leftG = hexdec(substr($left, 2, 2)); $leftG = hexdec(substr($left, 2, 2));
$leftB = hexdec(substr($left, 4, 2)); $leftB = hexdec(substr($left, 4, 2));
$rightR = hexdec(substr($right, 0, 2)); $rightR = hexdec(substr($right, 0, 2));
$rightG = hexdec(substr($right, 2, 2)); $rightG = hexdec(substr($right, 2, 2));
$rightB = hexdec(substr($right, 4, 2)); $rightB = hexdec(substr($right, 4, 2));
for ($i = 0; $i < 250; $i++) { for ($i = 0; $i < 250; $i++) {
$colorset[$i] = imagecolorallocatealpha($handle, $leftR + ($i * (($rightR - $leftR) / 250)), $leftG + ($i * (($rightG - $leftG) / 250)), $leftB + ($i * (($rightB - $leftB) / 250)), 117 - ($i / 250) * 40); $colorset[$i] = imagecolorallocatealpha($handle, $leftR + ($i * (($rightR - $leftR) / 250)), $leftG + ($i * (($rightG - $leftG) / 250)), $leftB + ($i * (($rightB - $leftB) / 250)), 117 - ($i / 250) * 40);
} }
   
foreach ($query->fetchAll() as $row) { foreach ($query->fetchAll() as $row) {
$xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength)); $xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength));
if ($row['lat'] < -40.25) if ($row['lat'] < -40.25)
$row['lat']-= 0.75; $row['lat']-= 0.75;
$ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight); $ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight);
@$pagevalues[$xpage][$ypage] += $row['value']; @$pagevalues[$xpage][$ypage] += $row['value'];
//echo $ypage." ".$xpage."<br>"; //echo $ypage." ".$xpage."<br>";
} }
   
$max = 0; $max = 0;
$min = 0; $min = 0;
for ($i = 0; $i < $xdivs; $i++) { for ($i = 0; $i < $xdivs; $i++) {
for ($j = 0; $j < $ydivs; $j++) { for ($j = 0; $j < $ydivs; $j++) {
if (@$pagevalues[$i][$j]) { if (@$pagevalues[$i][$j]) {
if (@$pagevalues[$i][$j] > $max) if (@$pagevalues[$i][$j] > $max)
$max = @$pagevalues[$i][$j]; $max = @$pagevalues[$i][$j];
if ($min == 0 || @$pagevalues[$i][$j] < $min) if ($min == 0 || @$pagevalues[$i][$j] < $min)
$min = @$pagevalues[$i][$j]; $min = @$pagevalues[$i][$j];
} else { } else {
@$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i + 1][$j] + @$pagevalues[$i][$j + 1] @$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i + 1][$j] + @$pagevalues[$i][$j + 1]
+ @$pagevalues[$i - 1][$j] + @$pagevalues[$i][$j - 1]) / 4) / 2) - 9; + @$pagevalues[$i - 1][$j] + @$pagevalues[$i][$j - 1]) / 4) / 2) - 9;
@$pagevalues[$i - 1][$j] = ((@$pagevalues[$i - 1][$j] + (@$pagevalues[$i][$j] + @$pagevalues[$i - 1][$j + 1] @$pagevalues[$i - 1][$j] = ((@$pagevalues[$i - 1][$j] + (@$pagevalues[$i][$j] + @$pagevalues[$i - 1][$j + 1]
+ @$pagevalues[$i - 2][$j] + @$pagevalues[$i - 1][$j - 1]) / 4) / 2) - 9; + @$pagevalues[$i - 2][$j] + @$pagevalues[$i - 1][$j - 1]) / 4) / 2) - 9;
@$pagevalues[$i][$j - 1] = ((@$pagevalues[$i][$j - 1] + (@$pagevalues[$i + 1][$j - 1] + @$pagevalues[$i][$j] @$pagevalues[$i][$j - 1] = ((@$pagevalues[$i][$j - 1] + (@$pagevalues[$i + 1][$j - 1] + @$pagevalues[$i][$j]
+ @$pagevalues[$i - 1][$j - 1] + @$pagevalues[$i][$j - 2]) / 4) / 2) - 9; + @$pagevalues[$i - 1][$j - 1] + @$pagevalues[$i][$j - 2]) / 4) / 2) - 9;
} }
} }
} }
//echo $max." ".$min; //echo $max." ".$min;
//echo "<table width='100%'>"; //echo "<table width='100%'>";
for ($i = 0; $i < $xdivs; $i++) { for ($i = 0; $i < $xdivs; $i++) {
// echo "<tr>"; // echo "<tr>";
for ($j = 0; $j < $ydivs; $j++) { for ($j = 0; $j < $ydivs; $j++) {
// echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>"); // echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>");
if ((@$pagevalues[$i][$j]) > $min) { if ((@$pagevalues[$i][$j]) > $min) {
$x = $i * ($width / $xdivs); $x = $i * ($width / $xdivs);
$x1 = $borderLeft + $x - ($width / $xdivs); $x1 = $borderLeft + $x - ($width / $xdivs);
$x2 = $borderLeft + $x + ($width / $xdivs); $x2 = $borderLeft + $x + ($width / $xdivs);
$y = $j * ($height / $ydivs); $y = $j * ($height / $ydivs);
$y1 = $borderTop + $y - ($height / $ydivs); $y1 = $borderTop + $y - ($height / $ydivs);
$y2 = $borderTop + $y + ($height / $ydivs); $y2 = $borderTop + $y + ($height / $ydivs);
imagefilledrectangle($handle, $x1, $y1, $x2, $y2, $colorset[(int) ((log10(@$pagevalues[$i][$j]) / log10($max)) * 249)]); imagefilledrectangle($handle, $x1, $y1, $x2, $y2, $colorset[(int) ((log10(@$pagevalues[$i][$j]) / log10($max)) * 249)]);
} }
} }
//echo "</tr>"; //echo "</tr>";
} }
//echo "</table>" //echo "</table>"
   
header("Content-type: image/png"); header("Content-type: image/png");
ImagePng($handle); ImagePng($handle);
?> ?>