|
1 | 1 | <?php |
2 | | - /* |
3 | | - * Demonstration of the phpFITFileAnalysis class using Twitter Bootstrap framework |
4 | | - * https://github.com/adriangibbons/phpFITFileAnalysis |
5 | | - * |
6 | | - * Not intended to be demonstration of how to best use Google APIs, but works for me! |
7 | | - * |
8 | | - * If you find this useful, feel free to drop me a line at Adrian.GitHub@gmail.com |
9 | | - */ |
10 | | - require __DIR__ . '/../src/phpFITFileAnalysis.php'; |
11 | | - require __DIR__ . '/libraries/PolylineEncoder.php'; // https://github.com/dyaaj/polyline-encoder |
12 | | - require __DIR__ . '/libraries/Line_DouglasPeucker.php'; // https://github.com/gregallensworth/PHP-Geometry |
13 | | - try { |
14 | | - $file = '/fit_files/mountain-biking.fit'; |
15 | | - |
16 | | - $options = [ |
17 | | - // Just using the defaults so no need to provide |
18 | | - // 'fixData' => [], |
19 | | - // 'units' => 'metric', |
20 | | - // 'pace' => false |
21 | | - ]; |
22 | | - $pFFA = new adriangibbons\phpFITFileAnalysis\phpFITFileAnalysis(__DIR__ . $file, $options); |
23 | | - } |
24 | | - catch(Exception $e) { |
25 | | - echo 'caught exception: '.$e->getMessage(); |
26 | | - die(); |
27 | | - } |
28 | | - |
29 | | - // Google Static Maps API |
30 | | - $position_lat = $pFFA->data_mesgs['record']['position_lat']; |
31 | | - $position_long = $pFFA->data_mesgs['record']['position_long']; |
32 | | - $lat_long_combined = []; |
33 | | - |
34 | | - foreach($position_lat as $key => $value) { // Assumes every lat has a corresponding long |
35 | | - $lat_long_combined[] = [$position_lat[$key],$position_long[$key]]; |
36 | | - } |
37 | | - |
38 | | - $delta = 0.0001; |
39 | | - do { |
40 | | - $RDP_LatLng_coord = simplify_RDP($lat_long_combined,$delta); // Simplify the array of coordinates using the Ramer-Douglas-Peucker algorithm. |
41 | | - $delta += 0.0001; // Rough accuracy somewhere between 4m and 12m depending where in the World coordinates are, source http://en.wikipedia.org/wiki/Decimal_degrees |
42 | | - |
43 | | - $polylineEncoder = new PolylineEncoder(); // Create an encoded string to pass as the path variable for the Google Static Maps API |
44 | | - foreach($RDP_LatLng_coord as $RDP) |
45 | | - $polylineEncoder->addPoint($RDP[0], $RDP[1]); |
46 | | - $map_encoded_polyline = $polylineEncoder->encodedString(); |
47 | | - |
48 | | - $map_string = '&path=color:red%7Cenc:'.$map_encoded_polyline; |
49 | | - } while(strlen($map_string) > 1800); // Google Map web service URL limit is 2048 characters. 1800 is arbitrary attempt to stay under 2048 |
50 | | - |
51 | | - $LatLng_start = implode(',',$lat_long_combined[0]); |
52 | | - $LatLng_finish = implode(',',$lat_long_combined[count($lat_long_combined)-1]); |
53 | | - |
54 | | - $map_string .= '&markers=color:red%7Clabel:F%7C'.$LatLng_finish.'&markers=color:green%7Clabel:S%7C'.$LatLng_start; |
55 | | - |
56 | | - |
57 | | - // Google Time Zone API |
58 | | - $date = new DateTime('1989-12-31', new DateTimeZone('UTC')); // FIT timestamps are seconds since UTC 00:00:00 Dec 31 1989, source FIT SDK |
59 | | - $date_s = $date->getTimestamp() + $pFFA->data_mesgs['session']['start_time']; |
60 | | - |
61 | | - $url_tz = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$LatLng_start.'×tamp='.$date_s.'&key=AIzaSyDlPWKTvmHsZ-X6PGsBPAvo0nm1-WdwuYE'; |
62 | | - |
63 | | - $result = file_get_contents($url_tz); |
64 | | - $json_tz = json_decode($result); |
65 | | - if($json_tz->status == 'OK') { |
66 | | - $date_s = $date_s + $json_tz->rawOffset + $json_tz->dstOffset; |
67 | | - } |
68 | | - else { |
69 | | - $json_tz->timeZoneName = 'Error'; |
70 | | - } |
71 | | - $date->setTimestamp($date_s); |
72 | | - |
73 | | - |
74 | | - // Google Geocoding API |
75 | | - $location = 'Error'; |
76 | | - $url_coord = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.$LatLng_start.'&key=AIzaSyDlPWKTvmHsZ-X6PGsBPAvo0nm1-WdwuYE'; |
77 | | - $result = file_get_contents($url_coord); |
78 | | - $json_coord = json_decode($result); |
79 | | - if($json_coord->status == 'OK') { |
80 | | - foreach($json_coord->results[0]->address_components as $addressPart) { |
81 | | - if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))) |
82 | | - $city = $addressPart->long_name; |
83 | | - else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types))) |
84 | | - $state = $addressPart->short_name; |
85 | | - else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))) |
86 | | - $country = $addressPart->long_name; |
87 | | - } |
88 | | - $location = $city.', '.$state.', '.$country; |
89 | | - } |
| 2 | +/** |
| 3 | + * Demonstration of the phpFITFileAnalysis class using Twitter Bootstrap framework |
| 4 | + * https://github.com/adriangibbons/phpFITFileAnalysis |
| 5 | + * |
| 6 | + * Not intended to be demonstration of how to best use Google APIs, but works for me! |
| 7 | + * |
| 8 | + * If you find this useful, feel free to drop me a line at Adrian.GitHub@gmail.com |
| 9 | + */ |
| 10 | +require __DIR__ . '/../src/phpFITFileAnalysis.php'; |
| 11 | +require __DIR__ . '/libraries/PolylineEncoder.php'; // https://github.com/dyaaj/polyline-encoder |
| 12 | +require __DIR__ . '/libraries/Line_DouglasPeucker.php'; // https://github.com/gregallensworth/PHP-Geometry |
| 13 | + |
| 14 | +try { |
| 15 | + $file = '/fit_files/mountain-biking.fit'; |
| 16 | + |
| 17 | + $options = [ |
| 18 | + // Just using the defaults so no need to provide |
| 19 | + // 'fixData' => [], |
| 20 | + // 'units' => 'metric', |
| 21 | + // 'pace' => false |
| 22 | + ]; |
| 23 | + $pFFA = new adriangibbons\phpFITFileAnalysis\phpFITFileAnalysis(__DIR__ . $file, $options); |
| 24 | +} catch (Exception $e) { |
| 25 | + echo 'caught exception: '.$e->getMessage(); |
| 26 | + die(); |
| 27 | +} |
| 28 | + |
| 29 | + // Google Static Maps API |
| 30 | + $position_lat = $pFFA->data_mesgs['record']['position_lat']; |
| 31 | + $position_long = $pFFA->data_mesgs['record']['position_long']; |
| 32 | + $lat_long_combined = []; |
| 33 | + |
| 34 | +foreach ($position_lat as $key => $value) { // Assumes every lat has a corresponding long |
| 35 | + $lat_long_combined[] = [$position_lat[$key],$position_long[$key]]; |
| 36 | +} |
| 37 | + |
| 38 | + $delta = 0.0001; |
| 39 | +do { |
| 40 | + $RDP_LatLng_coord = simplify_RDP($lat_long_combined, $delta); // Simplify the array of coordinates using the Ramer-Douglas-Peucker algorithm. |
| 41 | + $delta += 0.0001; // Rough accuracy somewhere between 4m and 12m depending where in the World coordinates are, source http://en.wikipedia.org/wiki/Decimal_degrees |
| 42 | + |
| 43 | + $polylineEncoder = new PolylineEncoder(); // Create an encoded string to pass as the path variable for the Google Static Maps API |
| 44 | + foreach ($RDP_LatLng_coord as $RDP) { |
| 45 | + $polylineEncoder->addPoint($RDP[0], $RDP[1]); |
| 46 | + } |
| 47 | + $map_encoded_polyline = $polylineEncoder->encodedString(); |
| 48 | + |
| 49 | + $map_string = '&path=color:red%7Cenc:'.$map_encoded_polyline; |
| 50 | +} while (strlen($map_string) > 1800); // Google Map web service URL limit is 2048 characters. 1800 is arbitrary attempt to stay under 2048 |
| 51 | + |
| 52 | + $LatLng_start = implode(',', $lat_long_combined[0]); |
| 53 | + $LatLng_finish = implode(',', $lat_long_combined[count($lat_long_combined)-1]); |
| 54 | + |
| 55 | + $map_string .= '&markers=color:red%7Clabel:F%7C'.$LatLng_finish.'&markers=color:green%7Clabel:S%7C'.$LatLng_start; |
| 56 | + |
| 57 | + |
| 58 | + // Google Time Zone API |
| 59 | + $date = new DateTime('1989-12-31', new DateTimeZone('UTC')); // FIT timestamps are seconds since UTC 00:00:00 Dec 31 1989, source FIT SDK |
| 60 | + $date_s = $date->getTimestamp() + $pFFA->data_mesgs['session']['start_time']; |
| 61 | + |
| 62 | + $url_tz = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$LatLng_start.'×tamp='.$date_s.'&key=AIzaSyDlPWKTvmHsZ-X6PGsBPAvo0nm1-WdwuYE'; |
| 63 | + |
| 64 | + $result = file_get_contents($url_tz); |
| 65 | + $json_tz = json_decode($result); |
| 66 | +if ($json_tz->status == 'OK') { |
| 67 | + $date_s = $date_s + $json_tz->rawOffset + $json_tz->dstOffset; |
| 68 | +} else { |
| 69 | + $json_tz->timeZoneName = 'Error'; |
| 70 | +} |
| 71 | + $date->setTimestamp($date_s); |
| 72 | + |
| 73 | + |
| 74 | + // Google Geocoding API |
| 75 | + $location = 'Error'; |
| 76 | + $url_coord = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.$LatLng_start.'&key=AIzaSyDlPWKTvmHsZ-X6PGsBPAvo0nm1-WdwuYE'; |
| 77 | + $result = file_get_contents($url_coord); |
| 78 | + $json_coord = json_decode($result); |
| 79 | +if ($json_coord->status == 'OK') { |
| 80 | + foreach ($json_coord->results[0]->address_components as $addressPart) { |
| 81 | + if ((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))) { |
| 82 | + $city = $addressPart->long_name; |
| 83 | + } elseif ((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types))) { |
| 84 | + $state = $addressPart->short_name; |
| 85 | + } elseif ((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))) { |
| 86 | + $country = $addressPart->long_name; |
| 87 | + } |
| 88 | + } |
| 89 | + $location = $city.', '.$state.', '.$country; |
| 90 | +} |
90 | 91 | ?> |
91 | 92 | <!doctype html> |
92 | 93 | <html> |
|
120 | 121 | <dt>Recorded: </dt> |
121 | 122 | <dd> |
122 | 123 | <?php |
123 | | - echo $date->format('D, d-M-y @ g:ia'); |
| 124 | + echo $date->format('D, d-M-y @ g:ia'); |
124 | 125 | ?> |
125 | 126 | </dd> |
126 | 127 | <dt>Duration: </dt> |
|
137 | 138 | </div> |
138 | 139 | <div class="panel-body"> |
139 | 140 | <?php |
140 | | - // Output all the Messages read in the FIT file. |
141 | | - foreach($pFFA->data_mesgs as $mesg_key=> $mesg) { |
142 | | - if($mesg_key == 'record') echo '<strong><mark><u>'; |
143 | | - echo $mesg_key.'<br>'; |
144 | | - if($mesg_key == 'record') echo '</u></mark></strong>'; |
145 | | - } |
| 141 | + // Output all the Messages read in the FIT file. |
| 142 | +foreach ($pFFA->data_mesgs as $mesg_key => $mesg) { |
| 143 | + if ($mesg_key == 'record') { |
| 144 | + echo '<strong><mark><u>'; |
| 145 | + } |
| 146 | + echo $mesg_key.'<br>'; |
| 147 | + if ($mesg_key == 'record') { |
| 148 | + echo '</u></mark></strong>'; |
| 149 | + } |
| 150 | +} |
146 | 151 | ?> |
147 | 152 | </div> |
148 | 153 | </div> |
|
152 | 157 | </div> |
153 | 158 | <div class="panel-body"> |
154 | 159 | <?php |
155 | | - // Output all the Fields found in Record messages within the FIT file. |
156 | | - foreach($pFFA->data_mesgs['record'] as $mesg_key=> $mesg) { |
157 | | - if($mesg_key == 'speed' || $mesg_key == 'heart_rate') echo '<strong><mark><u>'; |
158 | | - echo $mesg_key.'<br>'; |
159 | | - if($mesg_key == 'speed' || $mesg_key == 'heart_rate') echo '</strong></mark></u>'; |
160 | | - } |
| 160 | + // Output all the Fields found in Record messages within the FIT file. |
| 161 | +foreach ($pFFA->data_mesgs['record'] as $mesg_key => $mesg) { |
| 162 | + if ($mesg_key == 'speed' || $mesg_key == 'heart_rate') { |
| 163 | + echo '<strong><mark><u>'; |
| 164 | + } |
| 165 | + echo $mesg_key.'<br>'; |
| 166 | + if ($mesg_key == 'speed' || $mesg_key == 'heart_rate') { |
| 167 | + echo '</strong></mark></u>'; |
| 168 | + } |
| 169 | +} |
161 | 170 | ?> |
162 | 171 | </div> |
163 | 172 | </div> |
|
237 | 246 | 'color': 'rgba(11, 98, 164, 0.8)', |
238 | 247 | 'data': [ |
239 | 248 | <?php |
240 | | - $tmp = []; |
241 | | - foreach($pFFA->data_mesgs['record']['speed'] as $key => $value) { |
242 | | - $tmp[] = '['.$key.', '.$value.']'; |
243 | | - } |
244 | | - echo implode(', ', $tmp); |
| 249 | + $tmp = []; |
| 250 | +foreach ($pFFA->data_mesgs['record']['speed'] as $key => $value) { |
| 251 | + $tmp[] = '['.$key.', '.$value.']'; |
| 252 | +} |
| 253 | + echo implode(', ', $tmp); |
245 | 254 | ?> |
246 | 255 | ] |
247 | 256 | }; |
|
271 | 280 | 'color': 'rgba(255, 0, 0, 0.8)', |
272 | 281 | 'data': [ |
273 | 282 | <?php |
274 | | - unset($tmp); |
275 | | - $tmp = []; |
276 | | - foreach($pFFA->data_mesgs['record']['heart_rate'] as $key => $value) { |
277 | | - $tmp[] = '['.$key.', '.$value.']'; |
278 | | - } |
279 | | - echo implode(', ', $tmp); |
| 283 | + unset($tmp); |
| 284 | + $tmp = []; |
| 285 | +foreach ($pFFA->data_mesgs['record']['heart_rate'] as $key => $value) { |
| 286 | + $tmp[] = '['.$key.', '.$value.']'; |
| 287 | +} |
| 288 | + echo implode(', ', $tmp); |
280 | 289 | ?> |
281 | 290 | ] |
282 | 291 | }; |
|
0 commit comments