123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- ini_set("display_errors", "1");
- error_reporting(E_ALL);
- set_time_limit(300);
- require("connection.php");
- require("nmea_parser.php");
- // Opens a connection to a MySQL server
- $db = new PDO("mysql:host=$dbhost;dbname=$db;charset=utf8mb4", $dbuser, $dbpass);
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $data = Array();
- //print_r($_FILES);
- if(isset($_FILES['my_file'])){
- echo "<table border='1px'>";
- for($i=0;$i<count($_FILES['my_file']['name']);$i++){
- $ext = strtolower(pathinfo($_FILES['my_file']['name'][$i], PATHINFO_EXTENSION));
- if($ext == "txt") {
- $fp = fopen($_FILES['my_file']["tmp_name"][$i], "r");
- $fileContents=explode("\n", fread($fp, $_FILES['my_file']["size"][$i]));
- fclose($fp);
- echo "<tr><td>" . $_FILES['my_file']['name'][$i] . "</td>";
- echo "<td>" . count($fileContents) . "</td>";
-
- $updated = 0;
-
- foreach($fileContents as $row) {
- $row=explode(",", $row);
- if(count($row)!=8) continue;
- try {
- //connect as appropriate as above
- $stmt = $db->prepare("REPLACE INTO markers (timestamp,lat,lng,alt,hdop,speed,voltage,rssi) VALUES (?,?,?,?,?,?,?,?)");
- $stmt->execute(array($row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8]));
- $updated += $stmt->rowCount();
-
- } catch(PDOException $ex) {
- echo "An Error occured!"; //user friendly message
- //some_logging_function($ex->getMessage());
- }
-
- }
- echo "<td>" . $updated . " rows updated</td>";
-
- } elseif($ext == "nmea") {
- $fp = fopen($_FILES['my_file']["tmp_name"][$i], "r");
- $fileContents=explode("\n", fread($fp, $_FILES['my_file']["size"][$i]));
- fclose($fp);
- echo "<tr><td>" . $_FILES['my_file']['name'][$i] . "</td>";
- echo "<td>" . count($fileContents) . "</td>";
-
- $nmea = new NmeaParser();
- foreach($fileContents as $row) {
- $nmea->ParseLine($row);
- }
- $data = RamerDouglasPeucker2d(array_values($nmea->DumpNmea()), 0.00001); //.0001 = 7m
- echo "<td>" . count($data) . "</td>";
-
- $oldLat = $oldLong = 0;
- $rows = 0;
- $updated = 0;
-
- foreach($data as $row) {
- if(distance($oldLat, $oldLong, $row["lat"], $row["long"]) < 0.005) continue;
- $oldLat = $row["lat"];
- $oldLong = $row["long"];
-
- if($row["speed"] == 0) {
- $row["speed"] = null;
- }
- $rows++;
- try {
- $stmt = $db->prepare("REPLACE INTO markers (timestamp,lat,lng,alt,hdop,speed) VALUES (?,?,?,?,?,?)");
- $stmt->execute(array(date('Y-m-d H:i:s', $row['Unix']), $row["lat"], $row["long"], $row["alt"], intval($row["hdp"]), $row["speed"] * 100));
- $updated += $stmt->rowCount();
- } catch(PDOException $ex) {
- echo "An Error occured!"; //user friendly message
- error_log($ex->getMessage());
- }
- }
- echo "<td>" . $rows . "</td>";
- echo "<td>" . $updated . " rows updated</td>";
-
- }
- }
- echo '</table><br><a href="gps_filter.php">delete private</a>';
- }
- function perpendicularDistance2d($ptX, $ptY, $l1x, $l1y, $l2x, $l2y) {
- $result = 0;
- if ($l2x == $l1x)
- {
- //vertical lines - treat this case specially to avoid dividing
- //by zero
- $result = abs($ptX - $l2x);
- }
- else
- {
- $slope = (($l2y-$l1y) / ($l2x-$l1x));
- $passThroughY = (0-$l1x)*$slope + $l1y;
- $result = (abs(($slope * $ptX) - $ptY + $passThroughY)) /
- (sqrt($slope*$slope + 1));
- }
- return $result;
- }
- function RamerDouglasPeucker2d($pointList, $epsilon) {
- if ($epsilon <= 0)
- {
- throw new InvalidParameterException('Non-positive epsilon.');
- }
- if (count($pointList) < 2)
- {
- return $pointList;
- }
- // Find the point with the maximum distance
- $dmax = 0;
- $index = 0;
- $totalPoints = count($pointList);
- for ($i = 1; $i < ($totalPoints - 1); $i++)
- {
- $d = perpendicularDistance2d(
- $pointList[$i]["lat"], $pointList[$i]["long"],
- $pointList[0]["lat"], $pointList[0]["long"],
- $pointList[$totalPoints-1]["lat"],
- $pointList[$totalPoints-1]["long"]);
- if ($d > $dmax)
- {
- $index = $i;
- $dmax = $d;
- }
- }
- $resultList = array();
- // If max distance is greater than epsilon, recursively simplify
- if ($dmax >= $epsilon)
- {
- // Recursive call on each 'half' of the polyline
- $recResults1 = RamerDouglasPeucker2d(
- array_slice($pointList, 0, $index + 1),
- $epsilon);
- $recResults2 = RamerDouglasPeucker2d(
- array_slice($pointList, $index, $totalPoints - $index),
- $epsilon);
- // Build the result list
- $resultList = array_merge(array_slice($recResults1, 0,
- count($recResults1) - 1),
- array_slice($recResults2, 0,
- count($recResults2)));
- }
- else
- {
- $resultList = array($pointList[0], $pointList[$totalPoints-1]);
- }
- // Return the result
- return $resultList;
- }
- function distance($lat1, $lon1, $lat2, $lon2) {
- $theta = $lon1 - $lon2;
- $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
- $dist = acos($dist);
- $dist = rad2deg($dist);
- $miles = $dist * 60 * 1.1515;
- return ($miles * 1.609344); //km
- }
- ?>
|