Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit c22b65d

Browse files
committed
moving towards PSR-2 compliance
1 parent 4b9b500 commit c22b65d

File tree

3 files changed

+243
-234
lines changed

3 files changed

+243
-234
lines changed

demo/mountain-biking.php

Lines changed: 121 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,93 @@
11
<?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.'&timestamp='.$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.'&timestamp='.$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+
}
9091
?>
9192
<!doctype html>
9293
<html>
@@ -120,7 +121,7 @@
120121
<dt>Recorded: </dt>
121122
<dd>
122123
<?php
123-
echo $date->format('D, d-M-y @ g:ia');
124+
echo $date->format('D, d-M-y @ g:ia');
124125
?>
125126
</dd>
126127
<dt>Duration: </dt>
@@ -137,12 +138,16 @@
137138
</div>
138139
<div class="panel-body">
139140
<?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+
}
146151
?>
147152
</div>
148153
</div>
@@ -152,12 +157,16 @@
152157
</div>
153158
<div class="panel-body">
154159
<?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+
}
161170
?>
162171
</div>
163172
</div>
@@ -237,11 +246,11 @@
237246
'color': 'rgba(11, 98, 164, 0.8)',
238247
'data': [
239248
<?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);
245254
?>
246255
]
247256
};
@@ -271,12 +280,12 @@
271280
'color': 'rgba(255, 0, 0, 0.8)',
272281
'data': [
273282
<?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);
280289
?>
281290
]
282291
};

0 commit comments

Comments
 (0)