Advertisement
Guest User

eniron reittihaku php-skripti

a guest
Jan 28th, 2011
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. #!/usr/bin/php-cgi -q
  2. <?php
  3.  
  4. //
  5. // eniroreitti.cgi by Addic
  6. // v0.1
  7. //
  8. // hakee matka-ajan ja pituuden kahden paikan välillä suomessa, käyttäen eniron reittihakua
  9. // skripti olettaa, että on talletettu levylle iso-8859-1-encodingilla
  10. //
  11. // käyttö:
  12. //  eniroreitti.cgi [nopeus km/h] <lähtöpaikka> [-] <kohdepaikka>
  13. //
  14.  
  15. /**
  16.  * Capitalize all words
  17.  * @param string Data to capitalize
  18.  * @param string Word delimiters
  19.  * @return string Capitalized words
  20.  */
  21. function capitalizeWords($words, $charList = null) {
  22.     // Use ucwords if no delimiters are given
  23.     if (!isset($charList)) {
  24.         return ucwords($words);
  25.     }
  26.    
  27.     // Go through all characters
  28.     $capitalizeNext = true;
  29.    
  30.     for ($i = 0, $max = strlen($words); $i < $max; $i++) {
  31.         if (strpos($charList, $words[$i]) !== false) {
  32.             $capitalizeNext = true;
  33.         } else if ($capitalizeNext) {
  34.             $capitalizeNext = false;
  35.             $words[$i] = strtoupper($words[$i]);
  36.         }
  37.     }
  38.    
  39.     return $words;
  40. }
  41.  
  42.  
  43. setlocale(LC_CTYPE, "fi_FI");
  44.  
  45. // (thx to virus)
  46. $urlplace = "http://map.eniro.com/search/search.json?p=geo_fi&ap=geo_fi&q=";
  47. $urlroute = "http://map.eniro.com/route/route.json?waypoints=";
  48.  
  49. array_shift($argv);     // shiftataan skriptin nimi pois
  50.  
  51. $speed = "";
  52. if(intval($argv[0]) > 0)    
  53. {
  54.     $speed = $argv[0];
  55.     array_shift($argv); // nopeus pois
  56. }
  57.  
  58. // matkan päät joko välilyönnillä, tai " - " erotettuna
  59. if(!strstr(implode(" ",$argv), " - "))
  60.     $query = $argv;
  61. else    
  62.     $query = explode(" - ",implode(" ",$argv));
  63.  
  64. # kun eggdrop kutsuu tätä, tulee parametrit iso-8859-1:llä, muutetaan ne tässä:
  65. # (oletuksena on, että skripti on tallennettu iso-8859-1-enkoodauksella, muutoin
  66. #  seuraavassa koodissa ei ole järkeä ja se kannattaa kommentoida pois)
  67. if(strpbrk(implode(" ",$argv), "åäöÅÄÖ")) {
  68.     $query[0] = utf8_encode($query[0]);
  69.     $query[1] = utf8_encode($query[1]);
  70. }
  71.  
  72. # lähtöpaikka
  73. $data1 = json_decode(@file_get_contents($urlplace . urlencode($query[0])), true);
  74.  
  75. $lon1 = $data1["search"]["geo_fi"]["features"][0]["geometry"]["coordinates"][0];
  76. $lat1 = $data1["search"]["geo_fi"]["features"][0]["geometry"]["coordinates"][1];
  77.  
  78. $street1 = strtolower(utf8_decode($data1["search"]["geo_fi"]["features"][0]["properties"]["address"]["streetName"]));
  79. $streetnum1 = strtolower(utf8_decode($data1["search"]["geo_fi"]["features"][0]["properties"]["address"]["streetNumber"]));
  80. if($streetnum1 != "")  $street1 .= " " . $streetnum1;
  81. $city1 = strtolower(utf8_decode($data1["search"]["geo_fi"]["features"][0]["properties"]["address"]["city"]));
  82. $place1 = capitalizeWords(($street1 != $city1 ? "$street1, $city1" : $city1), " -");
  83.  
  84. if($lon1 == "" || $lat1 == "")
  85.     return;
  86.  
  87. # kohdepaikka
  88. $data2 = json_decode(@file_get_contents($urlplace . urlencode($query[1])), true);
  89.  
  90. $lon2 = $data2["search"]["geo_fi"]["features"][0]["geometry"]["coordinates"][0];
  91. $lat2 = $data2["search"]["geo_fi"]["features"][0]["geometry"]["coordinates"][1];
  92.  
  93. $street2 = strtolower(utf8_decode($data2["search"]["geo_fi"]["features"][0]["properties"]["address"]["streetName"]));
  94. $streetnum2 = strtolower(utf8_decode($data2["search"]["geo_fi"]["features"][0]["properties"]["address"]["streetNumber"]));
  95. if($streetnum2 != "")  $street2 .= " " . $streetnum2;
  96. $city2 = strtolower(utf8_decode($data2["search"]["geo_fi"]["features"][0]["properties"]["address"]["city"]));
  97.  
  98. $place2 = capitalizeWords(($street2 != $city2 ? "$street2, $city2" : $city2), " -");
  99.  
  100. if($lon2 == "" || $lat2 == "")
  101.     return;
  102.  
  103. if($lon1 == $lon2 && $lat2 == $lat1)
  104.     return;
  105.  
  106. # paikat haettu, haetaan itse reitti
  107. $data3 = json_decode(@file_get_contents($urlroute . "$lon1,$lat1;$lon2,$lat2"), true);;
  108.  
  109. $duration = $data3["route-geometries"]["features"][0]["properties"]["duration"];
  110. $length = $data3["route-geometries"]["features"][0]["properties"]["length"];
  111.  
  112. if(!intval($duration) || !intval($length))
  113.     return;
  114.  
  115. $kilometers = $length/1000;
  116. $mikaaika = "ajoaika";
  117. if(intval($speed) && $speed < 8)
  118.     $mikaaika = "kävelyaika";
  119. if(intval($speed) && $speed < 2)
  120.     $mikaaika = "mateluaika";
  121. $vauhdilla = "arvioitu $mikaaika ";
  122. if($speed != "") {
  123.     $duration = round(60*60*$kilometers / $speed);
  124.     $vauhdilla = "$mikaaika $speed km/h vauhdilla ";
  125. }
  126.  
  127. // nämä järkevämmiksi:
  128. $matkanpituus = "$length metriä";
  129. if($length > 5000)
  130.     $matkanpituus = round($length/1000) . " km";
  131. $ajoaika = "$duration sekuntia";
  132. if($duration > 120) {
  133.     $duration = round($duration / 60);
  134.     $ajoaika = "$duration minuuttia";
  135.     if($duration > 60)
  136.         $ajoaika = round($duration / 60) . " h " . $duration % 60 . " minuuttia";
  137. }
  138.  
  139. echo "Välimatka $place1 - $place2 on $matkanpituus, $vauhdilla$ajoaika";
  140.  
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement