Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: PHP  |  size: 2.53 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $FILE = "test.gpx";
  3. //$metriPerLat = 111133.35292; // 60.00721 miglia nautiche
  4.  
  5.         function CalcolaDistanza($lat1, $lon1, $alt1, $lat2, $lon2, $alt2) {
  6.  
  7.                 // Costanti
  8.                 $metriPerLon = 111319.88636; // 60.10793 miglia nautiche
  9.                 $metriPerLat = 111133.35292; // 60.00721 miglia nautiche
  10.                 $rad = pi() / 180.0;
  11.  
  12.                 $xDistance = (cos($lat1 * $rad) + cos($lat2 * $rad)) * ($lon2 - $lon1) * ($metriPerLon / 2);
  13.                 $yDistance = ($lat2 - $lat1) * $metriPerLat;
  14.                 $vDistance = ($alt2 - $alt1); // espressa in metri
  15.  
  16.         // le distanze vanno calcolate come valori assoluti
  17.         // secondo me (davide) inutile, ne fai il quadrato
  18.                 if ( $xDistance < 0) $xDistance = $xDistance * -1;
  19.                 if ( $yDistance < 0) $yDistance = $yDistance * -1;
  20.                 if ( $vDistance < 0) $vDistance = $vDistance * -1;
  21.  
  22. //              $Distanza_piana = sqrt( pow($yDistance, 2) + pow($xDistance, 2) );
  23. //              $Distanza = sqrt( pow($Distanza_piana, 2) + pow($vDistance, 2) );
  24.  
  25.                 $Distanza = sqrt(pow($yDistance, 2) + pow($xDistance, 2) + pow($vDistance, 2) );
  26.                 return $Distanza;
  27.         }
  28.  
  29.  
  30.  
  31.  
  32. if (file_exists($FILE)) {
  33.         $xml = simplexml_load_file($FILE);
  34.         $lat_pre=0;
  35.     $distanza_totale = 0;
  36.  
  37.         foreach( $xml->children() AS $child ) {
  38.                 $name = $child->getName();
  39.  
  40.                 if ($name == 'trk') {
  41.                         foreach( $child->children() AS $grandchild ) {
  42.                                 $grandname = $grandchild->getName();
  43.                                 if ($grandname == 'name') {
  44.                                         printf ("<h2>ROUTE NAME: %s</h2>", $grandchild);
  45.                                 }
  46.                                 if ($grandname == 'trkseg') {
  47.                                         foreach( $grandchild->children() AS $greatgrandchild ) {
  48.                                                 $greatgrandname = $greatgrandchild->getName();
  49.  
  50.                                                 printf("%s - ", $greatgrandname);
  51.  
  52.                                                 if ($greatgrandname == 'trkpt') {
  53.                                                         $lat=(float)$greatgrandchild['lat'];
  54.                                                         $lon=(float)$greatgrandchild['lon'];
  55.  
  56.                                                         printf("LAT: %s - LON: %s - ", $lat, $lon);
  57. /*
  58.                                                         foreach( $greatgrandchild->children() AS $elegreatgrandchild ) {
  59.                                                                 printf ("%s: %s - ", $elegreatgrandchild->getName(), $elegreatgrandchild);
  60.                                                         }
  61.  */
  62.                                                 }
  63.                                                 if ($greatgrandname == 'ele') {
  64.                                                         $alt=$greatgrandchild;
  65.                                                         print_r("ELE: %s", $alt);
  66.                                                 }
  67.  
  68.                                                 if ( $lat_pre != 0 ) $DIST=CalcolaDistanza ($lat_pre, $lon_pre, $alt_pre, $lat, $lon, $alt);
  69.                                                 printf ("DIST: %s<br>\n", $DIST);
  70.                         $distanza_totale = $distanza_totale + $DIST ;
  71.  
  72.                                                 $lat_pre=$lat;
  73.                                                 $lon_pre=$lon;
  74.                                                 $alt_pre=$alt;
  75.  
  76.                         printf ( "\n Distanza totale: %s<br>\n", $distanza_totale);
  77.                                         }
  78.                                 }
  79.                         }
  80.                 }
  81.         }
  82. } else {
  83.         exit('Failed to open file');
  84. }
  85. ?>