Skip to content

Instantly share code, notes, and snippets.

@jpaljasma
Created October 27, 2015 20:18
Show Gist options
  • Save jpaljasma/04f54e0d2fa3a632071e to your computer and use it in GitHub Desktop.
Save jpaljasma/04f54e0d2fa3a632071e to your computer and use it in GitHub Desktop.
remove before flight - gps data
<?php
$__ts = microtime(true);
error_reporting(E_ALL);
ini_set('display_errors', 'On');
header('Content-Type: text/html; charset=utf-8');
define('CLEARDB_DATABASE_URL', getenv('CLEARDB_DATABASE_URL') ?: 'mysql://user:pass@serverhost/dbname');
preg_match('/([a-z]+):\/\/([a-f0-9]+):([a-f0-9]+)@([a-z0-9\-\.]+)\/([a-z0-9_]+)/', CLEARDB_DATABASE_URL, $matches);
$db = new PDO(sprintf('%s:host=%s;dbname=%s;charset=utf8', $matches[1], $matches[4], $matches[5]), $matches[2], $matches[3]);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$cmd = $db->prepare('SELECT `time`, `current`, (`current`-`previous`) as `delta`, `latitude`, `longitude` from
(
SELECT `time`, COALESCE(@row, unix_timestamp(`time`)) as `previous`, (@row:=unix_timestamp(`time`)) as `current`, `latitude`, `longitude`
FROM `gpsData`, (SELECT @row:=0) r
WHERE `provider`= :provider
) AS x');
$cmd->execute([':provider' => 'gps']);
$result = $cmd->setFetchMode(PDO::FETCH_OBJ);
$i = 0;
$ts = [];
$t = [];
while ($row = $cmd->fetch()) {
if ($row->delta > 600 && sizeof($t) > 0) {
// make new rowset
$ts[] = $t;
$t = [];
$i = 0;
}
if($i > 0) {
$prevCoords = $t[$i-1];
if($row->latitude == $prevCoords[0] && $row->longitude == $prevCoords[1]) {
// excluding same coordinates
continue;
}
}
$t[] = [$row->latitude, $row->longitude];
$i++;
}
$ts[] = $t;
$t = null;
$cmd = null;
$db = null;
// echo 'Executed in '.(microtime(true) - $__ts).'s'.PHP_EOL;
// echo (memory_get_peak_usage(true) / pow(1024,2) ).'MB'.PHP_EOL;
// print_r($ts);
$polyColors = [
'#cf2c91',
'#dc8f4f',
'#852d3f',
'#246845',
'#221422',
'#4ce1a1',
'#cf2c91',
];
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GPS Map Example</title>
<link rel="stylesheet" href="//cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<style>
html, body, #map {
margin: 0; padding: 0;
width: 100%;
height: 100%;
}
#map {
overflow: hidden;
}
#map .text {
text-align: center;
margin: 25% auto;
}
</style>
</head>
<body>
<div id="map"><div class="text">Map is loading, please wait ...</div></div>
</body>
<script src="//cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<script type="text/javascript">
(function(L){
var map = L.map('map').setView([42.877742, -97.380979], 4);
var mapLink = '<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; ' + mapLink + ' Contributors',
maxZoom: 18
}).addTo(map);
<?php foreach($ts as $k=>$t) : ?>
var polyline_options = {
color: '<?php echo $polyColors[$k]; ?>',
noClip: false,
smoothFactor: 1.0,
opacity: 0.9
};
var polyline = L.polyline(<?php echo json_encode($t, JSON_NUMERIC_CHECK); ?>, polyline_options).addTo(map);
<?php if($k == 3): ?>
map.fitBounds(polyline.getBounds(), [150,150]);
<?php endif; ?>
<?php endforeach; ?>
})(L);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment