forked from Waicung/WayFindingServerSide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyfunctions.php
More file actions
63 lines (56 loc) · 1.82 KB
/
myfunctions.php
File metadata and controls
63 lines (56 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* Created by PhpStorm.
* User: waicung
* Date: 02/06/2016
* Time: 18:13
*/
function initialSpeedComputation($locations){
$result = array();
$pointObject = array();
$window = 5;
$interval = 10;
$counter = 0;
for($i=9; $i<count($locations)-$window;$i+=$interval){
$location_id = $locations[$i]['location_id'];
$lat1=doubleval($locations[$i-$window]['lat']);
$lng1=doubleval($locations[$i-$window]['lng']);
$time1=$locations[$i-$window]['time_stamp'];
$lat2=doubleval($locations[$i+$window]['lat']);
$lng2=doubleval($locations[$i+$window]['lng']);
$time2=$locations[$i+$window]['time_stamp'];
$speed = computeSpeed($lat1,$lng1,$time1,$lat2,$lng2,$time2);
if($speed>=0){
$pointObject['location_id'] = $location_id;
$pointObject['speed'] = $speed;
array_push($result,$pointObject);
}else{
//echo "error";
}
//echo ++$counter;
}
return $result;
}
function computeSpeed($lat1, $lon1, $time1, $lat2, $lon2, $time2){
$distance = distance($lat1, $lon1, $lat2, $lon2, "K");
$datetime1 = new DateTime($time1);
$datetime2 = new DateTime($time2);
$interval = $datetime1->diff($datetime2);
$speed = $distance/intval($interval->format('%s'));
return $speed*1000;
}
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$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;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}