googleMapAPIPolylineEnc_PMPro

The XML Google Maps googleMapAPIPolylineEnc PMPro class.

Defined (1)

The class is defined in the following location(s).

/xmlgooglemaps_googleMapsAPI.php  
  1. class googleMapAPIPolylineEnc_PMPro { 
  2. var $numLevels = 18; 
  3. var $zoomFactor = 2; 
  4. var $verySmall = 0.00001; 
  5. var $forceEndpoints = true; 
  6. var $zoomLevelBreaks = array(); 
  7.  
  8. function googleMapAPIPolylineEnc_PMPro($zoomfactor, $numlevels) { 
  9. $this->numLevels = $numlevels; 
  10. $this->zoomFactor = $zoomfactor; 
  11. for($i = 0; $i < $this->numLevels; $i++) 
  12. $this->zoomLevelBreaks[$i] = $this->verySmall*pow($this->zoomFactor, $this->numLevels-$i-1); 
  13.  
  14. function computeLevel($dd) 
  15. if($dd > $this->verySmall) 
  16. $lev = 0; 
  17. while($dd < $this->zoomLevelBreaks[$lev]) 
  18. $lev++; 
  19. return $lev; 
  20.  
  21. function dpEncode($points) 
  22. if(count($points) > 2) 
  23. $stack[] = array(0, count($points)-1); 
  24. while(count($stack) > 0) 
  25. $current = array_pop($stack); 
  26. $maxDist = 0; 
  27. for($i = $current[0]+1; $i < $current[1]; $i++) 
  28. $temp = $this->distance($points[$i], $points[$current[0]], $points[$current[1]]); 
  29. if($temp > $maxDist) 
  30. $maxDist = $temp; 
  31. $maxLoc = $i; 
  32. if($maxDist > $absMaxDist) 
  33. $absMaxDist = $maxDist; 
  34. if($maxDist > $this->verySmall) 
  35. $dists[$maxLoc] = $maxDist; 
  36. array_push($stack, array($current[0], $maxLoc)); 
  37. array_push($stack, array($maxLoc, $current[1])); 
  38.  
  39. $encodedPoints = $this->createEncodings($points, $dists); 
  40. $encodedLevels = $this->encodeLevels($points, $dists, $absMaxDist); 
  41. $encodedPointsLiteral = str_replace('\\', "\\\\", $encodedPoints); 
  42.  
  43. return array($encodedPoints, $encodedLevels, $encodedPointsLiteral); 
  44.  
  45. function distance($p0, $p1, $p2) 
  46. if($p1[0] == $p2[0] && $p1[1] == $p2[1]) 
  47. $out = sqrt(pow($p2[0]-$p0[0], 2) + pow($p2[1]-$p0[1], 2)); 
  48. else 
  49. $u = (($p0[0]-$p1[0])*($p2[0]-$p1[0]) + ($p0[1]-$p1[1]) * ($p2[1]-$p1[1])) / (pow($p2[0]-$p1[0], 2) + pow($p2[1]-$p1[1], 2)); 
  50. if($u <= 0) 
  51. $out = sqrt(pow($p0[0] - $p1[0], 2) + pow($p0[1] - $p1[1], 2)); 
  52. if($u >= 1) 
  53. $out = sqrt(pow($p0[0] - $p2[0], 2) + pow($p0[1] - $p2[1], 2)); 
  54. if(0 < $u && $u < 1) 
  55. $out = sqrt(pow($p0[0]-$p1[0]-$u*($p2[0]-$p1[0]), 2) + pow($p0[1]-$p1[1]-$u*($p2[1]-$p1[1]), 2)); 
  56. return $out; 
  57.  
  58. function encodeSignedNumber($num) 
  59. $sgn_num = $num << 1; 
  60. if ($num < 0) 
  61. $sgn_num = ~($sgn_num); 
  62. return $this->encodeNumber($sgn_num); 
  63.  
  64. function createEncodings($points, $dists) 
  65. for($i=0; $i<count($points); $i++) 
  66. if(isset($dists[$i]) || $i == 0 || $i == count($points)-1) 
  67. $point = $points[$i]; 
  68. $lat = $point[0]; 
  69. $lng = $point[1]; 
  70. $late5 = floor($lat * 1e5); 
  71. $lnge5 = floor($lng * 1e5); 
  72. $dlat = $late5 - $plat; 
  73. $dlng = $lnge5 - $plng; 
  74. $plat = $late5; 
  75. $plng = $lnge5; 
  76. $encoded_points .= $this->encodeSignedNumber($dlat) . $this->encodeSignedNumber($dlng); 
  77. return $encoded_points; 
  78.  
  79. function encodeLevels($points, $dists, $absMaxDist) 
  80. if($this->forceEndpoints) 
  81. $encoded_levels .= $this->encodeNumber($this->numLevels-1); 
  82. else 
  83. $encoded_levels .= $this->encodeNumber($this->numLevels-$this->computeLevel($absMaxDist)-1); 
  84. for($i=1; $i<count($points)-1; $i++) 
  85. if(isset($dists[$i])) 
  86. $encoded_levels .= $this->encodeNumber($this->numLevels-$this->computeLevel($dists[$i])-1); 
  87. if($this->forceEndpoints) 
  88. $encoded_levels .= $this->encodeNumber($this->numLevels -1); 
  89. else 
  90. $encoded_levels .= $this->encodeNumber($this->numLevels-$this->computeLevel($absMaxDist)-1); 
  91. return $encoded_levels; 
  92.  
  93. function encodeNumber($num) 
  94. while($num >= 0x20) 
  95. $nextValue = (0x20 | ($num & 0x1f)) + 63; 
  96. $encodeString .= chr($nextValue); 
  97. $num >>= 5; 
  98. $finalValue = $num + 63; 
  99. $encodeString .= chr($finalValue); 
  100. return $encodeString;