Depreciate MySQL and GD image graphs
[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, lat, lon FROM `contractnotice`,`postcodes` where childCN = 0 AND supplierCountry = 'Australia' AND YEAR(contractStart) >= '$year' AND supplierPostcode = postcode GROUP BY supplierPostcode"; $query = $conn->prepare($query);
$result = mysql_query($query); $query->execute();
  databaseError($conn->errorInfo());
$left = "FFFF50";  
$right= "EF0050";  
$leftR = hexdec(substr($left,0,2)); $left = "FFFF50";
$leftG = hexdec(substr($left,2,2)); $right = "EF0050";
$leftB = hexdec(substr($left,4,2)); $leftR = hexdec(substr($left, 0, 2));
$rightR = hexdec(substr($right,0,2)); $leftG = hexdec(substr($left, 2, 2));
$rightG = hexdec(substr($right,2,2)); $leftB = hexdec(substr($left, 4, 2));
$rightB = hexdec(substr($right,4,2)); $rightR = hexdec(substr($right, 0, 2));
for($i=0;$i<250;$i++) { $rightG = hexdec(substr($right, 2, 2));
$colorset[$i] = imagecolorallocatealpha($handle, $leftR + ($i*(($rightR-$leftR)/250)), $leftG + ($i*(($rightG-$leftG)/250)), $leftB + ($i*(($rightB-$leftB)/250)),117 - ($i/250)*40); $rightB = hexdec(substr($right, 4, 2));
} 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);
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) }
{  
$xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength)); foreach ($query->fetchAll() as $row) {
if ($row['lat'] < -40.25) $row['lat']-= 0.75; $xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength));
$ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight); if ($row['lat'] < -40.25)
@$pagevalues[$xpage][$ypage] += $row['value']; $row['lat']-= 0.75;
//echo $ypage." ".$xpage."<br>"; $ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight);
} @$pagevalues[$xpage][$ypage] += $row['value'];
  //echo $ypage." ".$xpage."<br>";
$max = 0; }
$min = 0;  
for ($i = 0;$i < $xdivs; $i++ ) { $max = 0;
for ($j = 0;$j < $ydivs; $j++ ) { $min = 0;
if (@$pagevalues[$i][$j]) for ($i = 0; $i < $xdivs; $i++) {
{ for ($j = 0; $j < $ydivs; $j++) {
if (@$pagevalues[$i][$j] > $max) $max = @$pagevalues[$i][$j]; if (@$pagevalues[$i][$j]) {
if ($min == 0 || @$pagevalues[$i][$j] < $min) $min = @$pagevalues[$i][$j]; if (@$pagevalues[$i][$j] > $max)
} else { $max = @$pagevalues[$i][$j];
@$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i+1][$j] + @$pagevalues[$i][$j+1] if ($min == 0 || @$pagevalues[$i][$j] < $min)
+ @$pagevalues[$i-1][$j] + @$pagevalues[$i][$j-1])/4) /2)-9; $min = @$pagevalues[$i][$j];
@$pagevalues[$i-1][$j] = ((@$pagevalues[$i-1][$j] + (@$pagevalues[$i][$j] + @$pagevalues[$i-1][$j+1] } else {
+ @$pagevalues[$i-2][$j] + @$pagevalues[$i-1][$j-1])/4) /2)-9; @$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i + 1][$j] + @$pagevalues[$i][$j + 1]
@$pagevalues[$i][$j-1] = ((@$pagevalues[$i][$j-1] + (@$pagevalues[$i+1][$j-1] + @$pagevalues[$i][$j] + @$pagevalues[$i - 1][$j] + @$pagevalues[$i][$j - 1]) / 4) / 2) - 9;
+ @$pagevalues[$i-1][$j-1] + @$pagevalues[$i][$j-2])/4) /2)-9; @$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][$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;
//echo $max." ".$min; }
//echo "<table width='100%'>"; }
for ($i = 0;$i < $xdivs; $i++ ) { }
// echo "<tr>"; //echo $max." ".$min;
for ($j = 0; $j < $ydivs; $j++ ) { //echo "<table width='100%'>";
// echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>"); for ($i = 0; $i < $xdivs; $i++) {
if ((@$pagevalues[$i][$j]) > $min) { // echo "<tr>";
$x = $i*($width/$xdivs); for ($j = 0; $j < $ydivs; $j++) {
$x1 = $borderLeft + $x -($width/$xdivs); // echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>");
$x2 = $borderLeft + $x +($width/$xdivs); if ((@$pagevalues[$i][$j]) > $min) {
$y = $j*($height/$ydivs); $x = $i * ($width / $xdivs);
$y1 = $borderTop + $y - ($height/$ydivs); $x1 = $borderLeft + $x - ($width / $xdivs);
$y2 = $borderTop + $y + ($height/$ydivs); $x2 = $borderLeft + $x + ($width / $xdivs);
imagefilledrectangle ($handle, $x1, $y1, $x2, $y2, $colorset[(int)((log10(@$pagevalues[$i][$j])/log10($max))*249)]); $y = $j * ($height / $ydivs);
} $y1 = $borderTop + $y - ($height / $ydivs);
} $y2 = $borderTop + $y + ($height / $ydivs);
//echo "</tr>"; imagefilledrectangle($handle, $x1, $y1, $x2, $y2, $colorset[(int) ((log10(@$pagevalues[$i][$j]) / log10($max)) * 249)]);
} }
//echo "</table>" }
  //echo "</tr>";
header ("Content-type: image/png"); }
ImagePng ($handle); //echo "</table>"
   
  header("Content-type: image/png");
  ImagePng($handle);
?> ?>