Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. define("BASE32", "0123456789bcdefghjkmnpqrstuvwxyz");
  5.  
  6. function geo_hash_decode($geo_hash) {
  7. $ones_and_zeros = array_map("chr2bin", str_split($geo_hash, 1));
  8. $exploded = str_split(join($ones_and_zeros, ""), 1);
  9.  
  10. $l_l = array(0 => array(), 1 => array());
  11. foreach($exploded as $i => $o) {
  12. $l_l[$i % 2][] = $o;
  13. }
  14.  
  15. $lon = array2val($l_l[0], 180);
  16. $lat = array2val($l_l[1], 90);
  17.  
  18. return(array("latitude" => $lat, "longitude" => $lon));
  19. }
  20.  
  21. function chr2bin($chr) {
  22. $base32 = str_split(BASE32, 1);
  23. $pos = array_search($chr, $base32);
  24. return sprintf("%05b", $pos);
  25. }
  26.  
  27. function bin2char($bin) {
  28. $base32 = str_split(BASE32, 1);
  29. return $base32[bindec($bin)];
  30. }
  31.  
  32. function array2val($array, $error) {
  33. $val = 0;
  34. $min = $error * -1;
  35. $max = $error;
  36.  
  37. foreach ($array as $m) {
  38. ($m==0) ? $max = $val : $min = $val;
  39. $val = $min + abs($max - $min) / 2;
  40. //echo "min$min max:$max val:$val\n";
  41. }
  42.  
  43. return $val;
  44. }
  45.  
  46. function geo_hash_encode($lat, $lon, $precision = 12) {
  47. if ($precision > 12) { $precision = 12; }
  48. if ($precision < 1) { $precision = 1; }
  49.  
  50. $string = "";
  51. $final = "";
  52.  
  53. $b_lat = val2bin($lat, 90);
  54. $b_lon = val2bin($lon, 180);
  55.  
  56. for ($i = 0; $i < max(sizeof($b_lat), sizeof($b_lon)); $i++) {
  57. $string .= $b_lon[$i] . $b_lat[$i];
  58. }
  59.  
  60. foreach(str_split($string, 5) as $chunk) {
  61. //echo "doing $chunk\n";
  62. $final .= bin2char($chunk);
  63. }
  64.  
  65. return substr($final, 0, $precision);
  66. }
  67.  
  68. function val2bin($target, $error) {
  69. $val = 0;
  70. $min = $error * -1;
  71. $max = $error;
  72. $moves = array();
  73.  
  74. while ($i++ <= 30) {
  75. if ($target > $val) {
  76. $moves[] = 1;
  77. $min = $val;
  78. }
  79. else {
  80. $moves[] = 0;
  81. $max = $val;
  82. }
  83. $val = $min + ($max - $min) / 2;
  84. }
  85.  
  86. return $moves;
  87. }
  88.  
  89. // val2bin(-5.60302734375, 180);
  90.  
  91. // geo_hash_decode("ezs42");
  92. // echo "\n";
  93. //
  94. // geo_hash_decode("ezs4");
  95. // echo "\n";
  96. //
  97. // geo_hash_decode("ezs");
  98. // echo "\n";
  99. //
  100. // geo_hash_decode("e");
  101. // echo "\n";
  102. // geo_hash_decode("dpkpdcs81jpy");
  103. //
  104.  
  105. //geo_hash_encode(42.6049804688, -5.60302734375);
  106. //geo_hash_decode("ezs42ebpbpbm");
  107.  
  108. $c = "ezefd36yxyrb";
  109. $c = "6gkzwgjzn820";
  110. echo $c . "\n";
  111. $g = geo_hash_decode($c);
  112. var_dump($g);
  113. echo geo_hash_encode($g["latitude"], $g["longitude"]);
  114.  
  115. ?>
Add Comment
Please, Sign In to add comment