PageRenderTime 26ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/php-edition/chapter7/ServerCustomTiles/server.php

https://github.com/iamamused/Apress-Beginning-Google-Maps-Applications
PHP | 161 lines | 97 code | 21 blank | 43 comment | 9 complexity | c290e811027b454f6a410c4e98db948e MD5 | raw file
  1. <?php
  2. //include the helper calculations
  3. require('GoogleMapUtility.php');
  4. //this script may require additional memory and time
  5. set_time_limit(0);
  6. ini_set('memory_limit',8388608*10);
  7. //create an array of the size for each marker at each zoom level
  8. $markerSizes = array(1,1,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12);
  9. //get the lat/lng bounds of this tile from the utility function
  10. //return abounds object with width,height,x,y
  11. $rect = GoogleMapUtility::getTileRect(
  12. (int)$_GET['x'],
  13. (int)$_GET['y'],
  14. (int)$_GET['zoom']
  15. );
  16. //create aunique file name for this tile
  17. $file = 'tiles/c'.md5(
  18. serialize($markerSizes).
  19. serialize($rect).'|'.
  20. $_GET['x'].'|'.
  21. $_GET['y'].'|'.
  22. $_GET['zoom']).'.gif';
  23. //check if the file already exists
  24. if(!file_exists($file)) {
  25. //create anew image
  26. $im = imagecreate(GoogleMapUtility::TILE_SIZE,GoogleMapUtility::TILE_SIZE);
  27. $trans = imagecolorallocate($im,0,0,255);
  28. imagefill($im,0,0,$trans);
  29. imagecolortransparent($im, $trans);
  30. $black = imagecolorallocate($im,0,0,0);
  31. $white = imagecolorallocate($im,255,255,255);
  32. //set up some colors for the markers.
  33. //each marker will have acolor based on the height of the tower
  34. $darkRed = imagecolorallocate($im,150,0,0);
  35. $red = imagecolorallocate($im,250,0,0);
  36. $darkGreen = imagecolorallocate($im,0,150,0);
  37. $green = imagecolorallocate($im,0,250,0);
  38. $darkBlue = imagecolorallocate($im,0,0,150);
  39. $blue = imagecolorallocate($im,0,0,250);
  40. $orange = imagecolorallocate($im,250,150,0);
  41. //init some vars
  42. $extend = 0;
  43. $z = (int)$_GET['zoom'];
  44. $swlat=$rect->y + $extend;
  45. $swlng=$rect->x+ $extend;
  46. $nelat=$swlat+$rect->height + $extend;
  47. $nelng=$swlng+$rect->width + $extend;
  48. //connect to the database
  49. require($_SERVER['DOCUMENT_ROOT'] . '/db_credentials.php');
  50. $conn = mysql_connect("localhost", $db_name, $db_pass);
  51. mysql_select_db("googlemapsbook", $conn);
  52. /*
  53. * Retrieve the points within the boundary of the map.
  54. * For the FCC data, all the points are within the US so we
  55. * don't need to worry about the meridian problem.
  56. */
  57. /*
  58. //using a view...
  59. $result = mysql_query(
  60. "SELECT
  61. longitude as lng,latitude as lat,struc_height,struc_elevation
  62. FROM
  63. fcc_towers
  64. WHERE
  65. (longitude > $swlng AND longitude < $nelng)
  66. AND (latitude <= $nelat AND latitude >= $swlat)
  67. ORDER BY
  68. lat"
  69. , $conn);
  70. */
  71. //using joins...
  72. $result = mysql_query(
  73. "SELECT
  74. fcc_location.longitude as lng,fcc_location.latitude as lat,fcc_structure.struc_height,fcc_structure.struc_elevation
  75. FROM
  76. fcc_structure, fcc_owner, fcc_location
  77. WHERE
  78. unique_si=unique_si_own
  79. AND unique_si=unique_si_loc
  80. AND (longitude > $swlng AND longitude < $nelng)
  81. AND (latitude <= $nelat AND latitude >= $swlat)
  82. ORDER BY
  83. lat"
  84. , $conn);
  85. //get the number of points in this tile
  86. $count = mysql_num_rows($result);
  87. $filled=array();
  88. if($count>0) {
  89. $row = mysql_fetch_assoc($result);
  90. while($row)
  91. {
  92. //get the x,y coordinate of the marker in the tile
  93. $point = GoogleMapUtility::getPixelOffsetInTile($row['lat'],$row['lng'],$z);
  94. //check if the marker was already drawn there
  95. if($filled["{$point->x},{$point->y}"]<2) {
  96. //pick acolor based on the structure's height
  97. if($row['struc_height']<=20) $c = $darkRed;
  98. elseif($row['struc_height']<=40) $c = $red;
  99. elseif($row['struc_height']<=80) $c = $darkGreen;
  100. elseif($row['struc_height']<=120) $c = $green;
  101. elseif($row['struc_height']<=200) $c = $darkBlue;
  102. else $c = $blue;
  103. //if there is aready apoint there, make it orange
  104. if($filled["{$point->x},{$point->y}"]==1) $c=$orange;
  105. //get the size
  106. $size = $markerSizes[$z];
  107. //draw the marker
  108. if($z<2) imagesetpixel($im, $point->x, $point->y, $c );
  109. elseif($z<12) {
  110. imagefilledellipse($im, $point->x, $point->y, $size, $size, $c );
  111. imageellipse($im, $point->x, $point->y, $size, $size, $white );
  112. } else {
  113. imageellipse($im, $point->x, $point->y, $size-1, $size-1, $c );
  114. imageellipse($im, $point->x, $point->y, $size-2, $size-2, $c );
  115. imageellipse($im, $point->x, $point->y, $size+1, $size+1, $black );
  116. imageellipse($im, $point->x, $point->y, $size, $size, $white );
  117. }
  118. //record that we drew the marker
  119. $filled["{$point->x},{$point->y}"]++;
  120. }
  121. $row = mysql_fetch_assoc($result);
  122. }
  123. }
  124. //write some info about the tile to the image for testing
  125. imagestring($im,1,-1,0, "$count points in tile ({$_GET['x']},{$_GET['y']}) @ zoom $z ",$white);
  126. imagestring($im,1,0,1, "$count points in tile ({$_GET['x']},{$_GET['y']}) @ zoom $z ",$white);
  127. imagestring($im,1,0,-1, "$count points in tile ({$_GET['x']},{$_GET['y']}) @ zoom $z ",$white);
  128. imagestring($im,1,1,0, "$count points in tile ({$_GET['x']},{$_GET['y']}) @ zoom $z ",$white);
  129. imagestring($im,1,0,0, "$count points in tile ({$_GET['x']},{$_GET['y']}) @ zoom $z ",$black);
  130. imagestring($im,1,0,9, date('r'),$black);
  131. //output the new image to the file system and then send it to the browser
  132. header('content-type:image/gif;');
  133. imagegif($im,$file);
  134. echo file_get_contents($file);
  135. } else {
  136. //output the existing image to the browser
  137. header('content-type:image/gif;');
  138. echo file_get_contents($file);
  139. }
  140. ?>