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 ""; for($i=0;$i"; echo ""; $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 ""; } 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 ""; echo ""; $nmea = new NmeaParser(); foreach($fileContents as $row) { $nmea->ParseLine($row); } $data = RamerDouglasPeucker2d(array_values($nmea->DumpNmea()), 0.00001); //.0001 = 7m echo ""; $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 ""; echo ""; } } echo '
" . $_FILES['my_file']['name'][$i] . "" . count($fileContents) . "" . $updated . " rows updated
" . $_FILES['my_file']['name'][$i] . "" . count($fileContents) . "" . count($data) . "" . $rows . "" . $updated . " rows updated

delete private'; } 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 } ?>