Depreciate MySQL and GD image graphs
[contractdashboard.git] / displayHeatmap.php
blob:a/displayHeatmap.php -> blob:b/displayHeatmap.php
--- a/displayHeatmap.php
+++ b/displayHeatmap.php
@@ -1,95 +1,96 @@
-<?php

-

-

-include('../lib/common.inc.php');

-

-$year = 2006;

-$ZeroX = 112.5;

-$MaxX = 157;

-$ZeroY = -9;

-$MaxY = -45;

-

-$XRange = $MaxX - $ZeroX;

-$YRange = abs($MaxY) - abs($ZeroY);

-$xdivs = 250;

-$xdivlength = $XRange / $xdivs;

-$ydivs = 250;

-$ydivheight = $YRange / $ydivs;

-

-$borderLeft = 100;

-$borderTop = 35;

-$width = 497;

-$height = 457;

-//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 = imagecreatefrompng('../images/australia.png');

-$white = imagecolorallocate($handle, 0, 0, 0);

-imagecolortransparent($handle, $white);

-

-$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";

-$result = mysql_query($query);

-

-$left = "FFFF50";

-$right= "EF0050";

-$leftR = hexdec(substr($left,0,2));

-$leftG = hexdec(substr($left,2,2));

-$leftB = hexdec(substr($left,4,2));

-$rightR = hexdec(substr($right,0,2));

-$rightG = hexdec(substr($right,2,2));

-$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));

-	if ($row['lat'] < -40.25) $row['lat']-= 0.75;

-   $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++ ) {

-   for ($j = 0;$j < $ydivs; $j++ ) {

-   if (@$pagevalues[$i][$j])

-   {

-      if (@$pagevalues[$i][$j] > $max) $max = @$pagevalues[$i][$j];

-      if ($min == 0 || @$pagevalues[$i][$j] < $min) $min = @$pagevalues[$i][$j];

-   } else {

-      @$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-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>";

-   for ($j = 0; $j < $ydivs; $j++ ) {

-//   echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>");

-   if ((@$pagevalues[$i][$j]) > $min) {

-      $x = $i*($width/$xdivs);

-      $x1 = $borderLeft + $x -($width/$xdivs);

-      $x2 = $borderLeft + $x +($width/$xdivs);

-      $y = $j*($height/$ydivs);

-      $y1 = $borderTop + $y - ($height/$ydivs);

-      $y2 = $borderTop + $y + ($height/$ydivs);

-      imagefilledrectangle ($handle, $x1, $y1, $x2, $y2, $colorset[(int)((log10(@$pagevalues[$i][$j])/log10($max))*249)]);

-   }

-   }

-//echo "</tr>";

-}

-//echo "</table>"

-

-header ("Content-type: image/png");

-ImagePng ($handle); 

-

-

+<?php
+
+include('lib/common.inc.php');
+
+$year = 2006;
+$ZeroX = 112.5;
+$MaxX = 157;
+$ZeroY = -9;
+$MaxY = -45;
+
+$XRange = $MaxX - $ZeroX;
+$YRange = abs($MaxY) - abs($ZeroY);
+$xdivs = 250;
+$xdivlength = $XRange / $xdivs;
+$ydivs = 250;
+$ydivheight = $YRange / $ydivs;
+
+$borderLeft = 100;
+$borderTop = 35;
+$width = 497;
+$height = 457;
+//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 = imagecreatefrompng('images/australia.png');
+$white = imagecolorallocate($handle, 0, 0, 0);
+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 = $conn->prepare($query);
+$query->execute();
+databaseError($conn->errorInfo());
+
+
+$left = "FFFF50";
+$right = "EF0050";
+$leftR = hexdec(substr($left, 0, 2));
+$leftG = hexdec(substr($left, 2, 2));
+$leftB = hexdec(substr($left, 4, 2));
+$rightR = hexdec(substr($right, 0, 2));
+$rightG = hexdec(substr($right, 2, 2));
+$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);
+}
+
+foreach ($query->fetchAll() as $row) {
+    $xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength));
+    if ($row['lat'] < -40.25)
+        $row['lat']-= 0.75;
+    $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++) {
+    for ($j = 0; $j < $ydivs; $j++) {
+        if (@$pagevalues[$i][$j]) {
+            if (@$pagevalues[$i][$j] > $max)
+                $max = @$pagevalues[$i][$j];
+            if ($min == 0 || @$pagevalues[$i][$j] < $min)
+                $min = @$pagevalues[$i][$j];
+        } else {
+            @$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 - 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>";
+    for ($j = 0; $j < $ydivs; $j++) {
+//   echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>");
+        if ((@$pagevalues[$i][$j]) > $min) {
+            $x = $i * ($width / $xdivs);
+            $x1 = $borderLeft + $x - ($width / $xdivs);
+            $x2 = $borderLeft + $x + ($width / $xdivs);
+            $y = $j * ($height / $ydivs);
+            $y1 = $borderTop + $y - ($height / $ydivs);
+            $y2 = $borderTop + $y + ($height / $ydivs);
+            imagefilledrectangle($handle, $x1, $y1, $x2, $y2, $colorset[(int) ((log10(@$pagevalues[$i][$j]) / log10($max)) * 249)]);
+        }
+    }
+//echo "</tr>";
+}
+//echo "</table>"
+
+header("Content-type: image/png");
+ImagePng($handle);
 ?>