1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This class is part of FlightAirmap. It's used to do Map Matching. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Ycarus (Yannick Chabanois) at Zugaina <[email protected]> |
6
|
|
|
* Licensed under AGPL license. |
7
|
|
|
* For more information see: https://www.flightairmap.com/ |
8
|
|
|
*/ |
9
|
|
|
require_once(dirname(__FILE__).'/class.Common.php'); |
10
|
|
|
|
11
|
|
|
class MapMatching { |
12
|
|
|
/* |
13
|
|
|
* Return results from map matching engine set by $globalMapMatchingSource |
14
|
|
|
* @param Array $spotter_history_array Array with latitude, longitude, altitude and date |
15
|
|
|
* @return Array Modified data with new map matching coordinates |
16
|
|
|
*/ |
17
|
|
|
public function match($spotter_history_array) { |
18
|
|
|
global $globalMapMatchingSource; |
19
|
|
|
if ($globalMapMatchingSource == 'mapbox') { |
20
|
|
|
return $this->mapbox($spotter_history_array); |
21
|
|
|
} elseif ($globalMapMatchingSource == 'graphhopper') { |
22
|
|
|
return $this->GraphHopper($spotter_history_array); |
23
|
|
|
} elseif ($globalMapMatchingSource == 'osmr') { |
24
|
|
|
return $this->osmr($spotter_history_array); |
25
|
|
|
/* |
26
|
|
|
} elseif ($globalMapMatchingSource == 'trackmatching') { |
27
|
|
|
return $this->TrackMatching($spotter_history_array); |
28
|
|
|
*/ |
29
|
|
|
} else { |
30
|
|
|
return $spotter_history_array; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/* |
35
|
|
|
* Create simple GPX file |
36
|
|
|
* @param Array $spotter_history_array Array with latitude, longitude, altitude and date |
37
|
|
|
* @return String The GPX file |
38
|
|
|
*/ |
39
|
|
|
public function create_gpx($spotter_history_array) { |
40
|
|
|
$gpx = '<?xml version="1.0" encoding="UTF-8"?>'; |
41
|
|
|
$gpx .= '<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpsies="http://www.gpsies.com/GPX/1/0" creator="GPSies http://www.gpsies.com - Sendl.-O&apos;sch-heim" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.gpsies.com/GPX/1/0 http://www.gpsies.com/gpsies.xsd">'; |
42
|
|
|
$gpx .= '<trk>'; |
43
|
|
|
$gpx .= '<trkseg>'; |
44
|
|
|
foreach($spotter_history_array as $spotter_data) { |
45
|
|
|
$gpx .= '<trkpt lat="'.sprintf("%.8f",$spotter_data['latitude']).'" lon="'.sprintf("%.8f",$spotter_data['longitude']).'">'; |
46
|
|
|
$gpx .= '<ele>'.sprintf("%.6f",$spotter_data['altitude']).'</ele>'; |
47
|
|
|
$gpx .= '<time>'.date("Y-m-d\TH:i:s\Z",strtotime($spotter_data['date'])).'</time>'; |
48
|
|
|
$gpx .= '</trkpt>'; |
49
|
|
|
} |
50
|
|
|
$gpx .= '</trkseg>'; |
51
|
|
|
$gpx .= '</trk>'; |
52
|
|
|
$gpx .= '</gpx>'; |
53
|
|
|
return $gpx; |
54
|
|
|
} |
55
|
|
|
/* |
56
|
|
|
public function Mapzen($spotter_history_array) { |
57
|
|
|
global $globalMapMatchingMaxPts, $globalMapzenKey; |
58
|
|
|
if (!isset($globalMapMatchingMaxPts)) $globalMapMatchingMaxPts = 500; |
59
|
|
|
if (count($spotter_history_array) < 2) return $spotter_history_array; |
60
|
|
|
if (count($spotter_history_array) > $globalMapMatchingMaxPts) $spotter_history_array = array_slice($spotter_history_array,-$globalMapMatchingMaxPts); |
61
|
|
|
$data = $this->create_gpx($spotter_history_array); |
62
|
|
|
$url = 'https://valhalla.mapzen.com/trace_route?api_key='.$globalMapzenKey; |
63
|
|
|
$Common = new Common(); |
64
|
|
|
$matching = $Common->getData($url,'post',$data,array('Content-Type: application/gpx+xml')); |
65
|
|
|
$matching = json_decode($matching,true); |
66
|
|
|
//print_r($matching); |
67
|
|
|
|
68
|
|
|
if (isset($matching['paths'][0]['points']['coordinates'])) { |
69
|
|
|
$spotter_history_array = array(); |
70
|
|
|
foreach ($matching['paths'][0]['points']['coordinates'] as $match) { |
71
|
|
|
$coord = $match; |
72
|
|
|
$spotter_history_array[] = array('longitude' => $coord[0],'latitude' => $coord[1]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $spotter_history_array; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
*/ |
79
|
|
|
|
80
|
|
|
public function TrackMatching($spotter_history_array) { |
81
|
|
|
global $globalMapMatchingMaxPts, $globalTrackMatchingAppKey, $globalTrackMatchingAppId; |
82
|
|
|
if (!isset($globalMapMatchingMaxPts)) $globalMapMatchingMaxPts = 100; |
83
|
|
|
if (count($spotter_history_array) < 2) return $spotter_history_array; |
84
|
|
|
if (count($spotter_history_array) > $globalMapMatchingMaxPts) $spotter_history_array = array_slice($spotter_history_array,-$globalMapMatchingMaxPts); |
85
|
|
|
$data = $this->create_gpx($spotter_history_array); |
86
|
|
|
$url = 'https://test.roadmatching.com/rest/mapmatch/?app_id='.$globalTrackMatchingAppId.'&app_key='.$globalTrackMatchingAppKey.'&output.waypoints=true'; |
87
|
|
|
$Common = new Common(); |
88
|
|
|
$matching = $Common->getData($url,'post',$data,array('Content-Type: application/gpx+xml','Accept: application/json')); |
89
|
|
|
$matching = json_decode($matching,true); |
90
|
|
|
if (isset($matching['diary']['entries'][0]['route']['links'])) { |
91
|
|
|
$spotter_history_array = array(); |
92
|
|
|
foreach ($matching['diary']['entries'][0]['route']['links'] as $match) { |
93
|
|
|
if (isset($match['wpts'])) { |
94
|
|
|
foreach ($match['wpts'] as $coord) { |
95
|
|
|
$spotter_history_array[] = array('longitude' => $coord['x'],'latitude' => $coord['y']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return $spotter_history_array; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
* Use https://www.graphhopper.com/ as map matching engine |
105
|
|
|
* @param Array $spotter_history_array Array with latitude, longitude, altitude and date |
106
|
|
|
* @return Array Modified data with new map matching coordinates |
107
|
|
|
*/ |
108
|
|
|
public function GraphHopper($spotter_history_array) { |
109
|
|
|
global $globalMapMatchingMaxPts, $globalGraphHopperKey; |
110
|
|
|
if (!isset($globalMapMatchingMaxPts)) $globalMapMatchingMaxPts = 100; |
111
|
|
|
if (count($spotter_history_array) < 2) return $spotter_history_array; |
112
|
|
|
$spotter_history_initial_array = array(); |
113
|
|
|
if (count($spotter_history_array) > $globalMapMatchingMaxPts) { |
114
|
|
|
$spotter_history_array = array_slice($spotter_history_array,-$globalMapMatchingMaxPts); |
115
|
|
|
$spotter_history_initial_array = array_slice($spotter_history_array,0,count($spotter_history_array)-$globalMapMatchingMaxPts); |
116
|
|
|
} |
117
|
|
|
$data = $this->create_gpx($spotter_history_array); |
118
|
|
|
$url = 'https://graphhopper.com/api/1/match?vehicle=car&points_encoded=0&key='.$globalGraphHopperKey; |
119
|
|
|
$Common = new Common(); |
120
|
|
|
$matching = $Common->getData($url,'post',$data,array('Content-Type: application/gpx+xml')); |
121
|
|
|
$matching = json_decode($matching,true); |
122
|
|
|
if (isset($matching['paths'][0]['points']['coordinates'])) { |
123
|
|
|
$spotter_history_array = array(); |
124
|
|
|
foreach ($matching['paths'][0]['points']['coordinates'] as $match) { |
125
|
|
|
$coord = $match; |
126
|
|
|
$spotter_history_array[] = array('longitude' => $coord[0],'latitude' => $coord[1]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
$spotter_history_array = array_merge($spotter_history_initial_array,$spotter_history_array); |
130
|
|
|
return $spotter_history_array; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/* |
134
|
|
|
* Use https://www.project-osrm.org/ as map matching engine |
135
|
|
|
* @param Array $spotter_history_array Array with latitude, longitude, altitude and date |
136
|
|
|
* @return Array Modified data with new map matching coordinates |
137
|
|
|
*/ |
138
|
|
|
public function osmr($spotter_history_array) { |
139
|
|
|
global $globalMapMatchingMaxPts; |
140
|
|
|
if (!isset($globalMapMatchingMaxPts)) $globalMapMatchingMaxPts = 50; |
141
|
|
|
if (count($spotter_history_array) < 2) return $spotter_history_array; |
142
|
|
|
$spotter_history_initial_array = array(); |
143
|
|
|
if (count($spotter_history_array) > $globalMapMatchingMaxPts) { |
144
|
|
|
$spotter_history_array = array_slice($spotter_history_array,-$globalMapMatchingMaxPts); |
145
|
|
|
$spotter_history_initial_array = array_slice($spotter_history_array,0,count($spotter_history_array)-$globalMapMatchingMaxPts); |
146
|
|
|
} |
147
|
|
|
$coord = ''; |
148
|
|
|
$ts = ''; |
149
|
|
|
$rd = ''; |
150
|
|
|
foreach ($spotter_history_array as $spotter_data) { |
151
|
|
|
if ($coord != '') $coord .= ';'; |
152
|
|
|
$coord .= $spotter_data['longitude'].','.$spotter_data['latitude']; |
153
|
|
|
if ($ts != '') $ts .= ';'; |
154
|
|
|
$ts .= strtotime($spotter_data['date']); |
155
|
|
|
if ($rd != '') $rd .= ';'; |
156
|
|
|
$rd .= '20'; |
157
|
|
|
} |
158
|
|
|
$url = 'https://router.project-osrm.org/match/v1/driving/'.$coord.'?timestamps='.$ts.'&overview=full&geometries=geojson&tidy=true&gaps=ignore'; |
159
|
|
|
$Common = new Common(); |
160
|
|
|
$matching = $Common->getData($url); |
161
|
|
|
$matching = json_decode($matching,true); |
162
|
|
|
if (isset($matching['matchings'][0]['geometry']['coordinates'])) { |
163
|
|
|
$spotter_history_array = array(); |
164
|
|
|
foreach ($matching['matchings'][0]['geometry']['coordinates'] as $match) { |
165
|
|
|
$coord = $match; |
166
|
|
|
$spotter_history_array[] = array('longitude' => $coord[0],'latitude' => $coord[1]); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
$spotter_history_array = array_merge($spotter_history_initial_array,$spotter_history_array); |
170
|
|
|
return $spotter_history_array; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/* |
174
|
|
|
* Use https://www.mapbox.com/ as map matching engine |
175
|
|
|
* @param Array $spotter_history_array Array with latitude, longitude, altitude and date |
176
|
|
|
* @return Array Modified data with new map matching coordinates |
177
|
|
|
*/ |
178
|
|
|
public function mapbox($spotter_history_array) { |
179
|
|
|
global $globalMapMatchingMaxPts, $globalMapboxToken; |
180
|
|
|
if (!isset($globalMapMatchingMaxPts)) $globalMapMatchingMaxPts = 60; |
181
|
|
|
if (count($spotter_history_array) < 2) return $spotter_history_array; |
182
|
|
|
$spotter_history_initial_array = array(); |
183
|
|
|
if (count($spotter_history_array) > $globalMapMatchingMaxPts) { |
184
|
|
|
$spotter_history_array = array_slice($spotter_history_array,-$globalMapMatchingMaxPts); |
185
|
|
|
$spotter_history_initial_array = array_slice($spotter_history_array,0,count($spotter_history_array)-$globalMapMatchingMaxPts); |
186
|
|
|
} |
187
|
|
|
$coord = ''; |
188
|
|
|
$ts = ''; |
189
|
|
|
$rd = ''; |
190
|
|
|
foreach ($spotter_history_array as $spotter_data) { |
191
|
|
|
if ($coord != '') $coord .= ';'; |
192
|
|
|
$coord .= $spotter_data['longitude'].','.$spotter_data['latitude']; |
193
|
|
|
if ($ts != '') $ts .= ';'; |
194
|
|
|
$ts .= strtotime($spotter_data['date']); |
195
|
|
|
if ($rd != '') $rd .= ';'; |
196
|
|
|
$rd .= '20'; |
197
|
|
|
} |
198
|
|
|
//$url = 'https://api.mapbox.com/matching/v5/mapbox/driving/'.$coord.'?access_token='.$globalMapboxToken.'×tamps='.$ts.'&overview=full&tidy=true&geometries=geojson&radiuses='.$rd; |
199
|
|
|
$url = 'https://api.mapbox.com/matching/v5/mapbox/driving/'.$coord.'?access_token='.$globalMapboxToken.'×tamps='.$ts.'&overview=full&tidy=true&geometries=geojson'; |
200
|
|
|
$Common = new Common(); |
201
|
|
|
$matching = $Common->getData($url); |
202
|
|
|
$matching = json_decode($matching,true); |
203
|
|
|
if (isset($matching['matchings'][0]['geometry']['coordinates'])) { |
204
|
|
|
$spotter_history_array = array(); |
205
|
|
|
foreach ($matching['matchings'][0]['geometry']['coordinates'] as $match) { |
206
|
|
|
$coord = $match; |
207
|
|
|
$spotter_history_array[] = array('longitude' => $coord[0],'latitude' => $coord[1]); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
$spotter_history_array = array_merge($spotter_history_initial_array,$spotter_history_array); |
211
|
|
|
return $spotter_history_array; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
?> |