gps_insert.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. ini_set("display_errors", "1");
  3. error_reporting(E_ALL);
  4. set_time_limit(300);
  5. require("connection.php");
  6. require("nmea_parser.php");
  7. // Opens a connection to a MySQL server
  8. $db = new PDO("mysql:host=$dbhost;dbname=$db;charset=utf8mb4", $dbuser, $dbpass);
  9. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  11. $data = Array();
  12. //print_r($_FILES);
  13. if(isset($_FILES['my_file'])){
  14. echo "<table border='1px'>";
  15. for($i=0;$i<count($_FILES['my_file']['name']);$i++){
  16. $ext = strtolower(pathinfo($_FILES['my_file']['name'][$i], PATHINFO_EXTENSION));
  17. if($ext == "txt") {
  18. $fp = fopen($_FILES['my_file']["tmp_name"][$i], "r");
  19. $fileContents=explode("\n", fread($fp, $_FILES['my_file']["size"][$i]));
  20. fclose($fp);
  21. echo "<tr><td>" . $_FILES['my_file']['name'][$i] . "</td>";
  22. echo "<td>" . count($fileContents) . "</td>";
  23. $updated = 0;
  24. foreach($fileContents as $row) {
  25. $row=explode(",", $row);
  26. if(count($row)!=8) continue;
  27. try {
  28. //connect as appropriate as above
  29. $stmt = $db->prepare("REPLACE INTO markers (timestamp,lat,lng,alt,hdop,speed,voltage,rssi) VALUES (?,?,?,?,?,?,?,?)");
  30. $stmt->execute(array($row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8]));
  31. $updated += $stmt->rowCount();
  32. } catch(PDOException $ex) {
  33. echo "An Error occured!"; //user friendly message
  34. //some_logging_function($ex->getMessage());
  35. }
  36. }
  37. echo "<td>" . $updated . " rows updated</td>";
  38. } elseif($ext == "nmea") {
  39. $fp = fopen($_FILES['my_file']["tmp_name"][$i], "r");
  40. $fileContents=explode("\n", fread($fp, $_FILES['my_file']["size"][$i]));
  41. fclose($fp);
  42. echo "<tr><td>" . $_FILES['my_file']['name'][$i] . "</td>";
  43. echo "<td>" . count($fileContents) . "</td>";
  44. $nmea = new NmeaParser();
  45. foreach($fileContents as $row) {
  46. $nmea->ParseLine($row);
  47. }
  48. $data = RamerDouglasPeucker2d(array_values($nmea->DumpNmea()), 0.00001); //.0001 = 7m
  49. echo "<td>" . count($data) . "</td>";
  50. $oldLat = $oldLong = 0;
  51. $rows = 0;
  52. $updated = 0;
  53. foreach($data as $row) {
  54. if(distance($oldLat, $oldLong, $row["lat"], $row["long"]) < 0.005) continue;
  55. $oldLat = $row["lat"];
  56. $oldLong = $row["long"];
  57. if($row["speed"] == 0) {
  58. $row["speed"] = null;
  59. }
  60. $rows++;
  61. try {
  62. $stmt = $db->prepare("REPLACE INTO markers (timestamp,lat,lng,alt,hdop,speed) VALUES (?,?,?,?,?,?)");
  63. $stmt->execute(array(date('Y-m-d H:i:s', $row['Unix']), $row["lat"], $row["long"], $row["alt"], intval($row["hdp"]), $row["speed"] * 100));
  64. $updated += $stmt->rowCount();
  65. } catch(PDOException $ex) {
  66. echo "An Error occured!"; //user friendly message
  67. error_log($ex->getMessage());
  68. }
  69. }
  70. echo "<td>" . $rows . "</td>";
  71. echo "<td>" . $updated . " rows updated</td>";
  72. }
  73. }
  74. echo '</table><br><a href="gps_filter.php">delete private</a>';
  75. }
  76. function perpendicularDistance2d($ptX, $ptY, $l1x, $l1y, $l2x, $l2y) {
  77. $result = 0;
  78. if ($l2x == $l1x)
  79. {
  80. //vertical lines - treat this case specially to avoid dividing
  81. //by zero
  82. $result = abs($ptX - $l2x);
  83. }
  84. else
  85. {
  86. $slope = (($l2y-$l1y) / ($l2x-$l1x));
  87. $passThroughY = (0-$l1x)*$slope + $l1y;
  88. $result = (abs(($slope * $ptX) - $ptY + $passThroughY)) /
  89. (sqrt($slope*$slope + 1));
  90. }
  91. return $result;
  92. }
  93. function RamerDouglasPeucker2d($pointList, $epsilon) {
  94. if ($epsilon <= 0)
  95. {
  96. throw new InvalidParameterException('Non-positive epsilon.');
  97. }
  98. if (count($pointList) < 2)
  99. {
  100. return $pointList;
  101. }
  102. // Find the point with the maximum distance
  103. $dmax = 0;
  104. $index = 0;
  105. $totalPoints = count($pointList);
  106. for ($i = 1; $i < ($totalPoints - 1); $i++)
  107. {
  108. $d = perpendicularDistance2d(
  109. $pointList[$i]["lat"], $pointList[$i]["long"],
  110. $pointList[0]["lat"], $pointList[0]["long"],
  111. $pointList[$totalPoints-1]["lat"],
  112. $pointList[$totalPoints-1]["long"]);
  113. if ($d > $dmax)
  114. {
  115. $index = $i;
  116. $dmax = $d;
  117. }
  118. }
  119. $resultList = array();
  120. // If max distance is greater than epsilon, recursively simplify
  121. if ($dmax >= $epsilon)
  122. {
  123. // Recursive call on each 'half' of the polyline
  124. $recResults1 = RamerDouglasPeucker2d(
  125. array_slice($pointList, 0, $index + 1),
  126. $epsilon);
  127. $recResults2 = RamerDouglasPeucker2d(
  128. array_slice($pointList, $index, $totalPoints - $index),
  129. $epsilon);
  130. // Build the result list
  131. $resultList = array_merge(array_slice($recResults1, 0,
  132. count($recResults1) - 1),
  133. array_slice($recResults2, 0,
  134. count($recResults2)));
  135. }
  136. else
  137. {
  138. $resultList = array($pointList[0], $pointList[$totalPoints-1]);
  139. }
  140. // Return the result
  141. return $resultList;
  142. }
  143. function distance($lat1, $lon1, $lat2, $lon2) {
  144. $theta = $lon1 - $lon2;
  145. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  146. $dist = acos($dist);
  147. $dist = rad2deg($dist);
  148. $miles = $dist * 60 * 1.1515;
  149. return ($miles * 1.609344); //km
  150. }
  151. ?>