/** * Get a workout from a string. * * @param string $string The data. * @return \SportTrackerConnector\Workout\Workout */ public function fromString($string) { $simpleXML = new SimpleXMLElement($string); $workout = new Workout(); if (isset($simpleXML->metadata->author->name)) { $workout->setAuthor(new Author($simpleXML->metadata->author->name)); } foreach ($simpleXML->trk as $simpleXMLTrack) { $track = new Track(); // Sport. if (isset($simpleXMLTrack->type)) { $track->setSport(SportGuesser::getSportFromCode($simpleXMLTrack->type)); } // Track points. foreach ($simpleXMLTrack->trkseg->trkpt as $point) { $attributes = $point->attributes(); $dateTime = new DateTime($point->time); $trackPoint = new TrackPoint((string) $attributes['lat'], (string) $attributes['lon'], $dateTime); $trackPoint->setElevation((int) $point->ele); if (isset($point->extensions)) { $trackPoint->setExtensions($this->parseExtensions($point->extensions)); } $track->addTrackPoint($trackPoint); } $workout->addTrack($track); } return $workout; }
/** * Get a workout from a string. * * @param string $string The data. * @return \SportTrackerConnector\Workout\Workout */ public function fromString($string) { $simpleXML = new SimpleXMLElement($string); $workout = new Workout(); foreach ($simpleXML->Activities[0] as $simpleXMLActivity) { $workoutTrack = new Track(); // Sport. $attributes = $simpleXMLActivity->attributes(); if (isset($attributes['Sport'])) { $workoutTrack->setSport(SportGuesser::getSportFromCode((string) $attributes['Sport'])); } // Track points. foreach ($simpleXMLActivity->Lap as $lap) { foreach ($lap->Track as $track) { foreach ($track->Trackpoint as $trackPoint) { $dateTime = new DateTime($trackPoint->Time); $latitude = (string) $trackPoint->Position->LatitudeDegrees; $longitude = (string) $trackPoint->Position->LongitudeDegrees; $workoutTrackPoint = new TrackPoint($latitude, $longitude, $dateTime); $workoutTrackPoint->setElevation(round((double) $trackPoint->AltitudeMeters, 2)); if ($trackPoint->DistanceMeters) { $workoutTrackPoint->setDistance($trackPoint->DistanceMeters); } $extensions = $this->parseExtensions($trackPoint); $workoutTrackPoint->setExtensions($extensions); $workoutTrack->addTrackPoint($workoutTrackPoint); } } } $workout->addTrack($workoutTrack); } return $workout; }
/** * Get a track point. * * @param string $lat The latitude. * @param string $lon The longitude. * @param string $time The time. * @param integer $ele The elevation. * @param integer $hr The heart rate. * @return TrackPoint */ private function getTrackPoint($lat, $lon, $time, $ele, $hr) { $trackPoint = new TrackPoint($lat, $lon, new DateTime($time)); $trackPoint->setElevation($ele); $trackPoint->addExtension(new HR($hr)); return $trackPoint; }
/** * Get a track point. * * @param string $latitude The latitude. * @param string $longitude The longitude. * @param string $time The time. * @param float $distance The distance from start to that point. * @param integer $elevation The elevation. * @param integer $heartRate The heart rate. * @return TrackPoint */ private function getTrackPoint($latitude, $longitude, $time, $elevation, $distance = null, $heartRate = null) { $trackPoint = new TrackPoint($latitude, $longitude, new DateTime($time)); $trackPoint->setElevation($elevation); $trackPoint->setDistance($distance); if ($heartRate !== null) { $trackPoint->setExtensions(array(new HR($heartRate))); } return $trackPoint; }
/** * Get a track point mock. * * @param float $latitude The latitude. * @param float $longitude The longitude. * @param string $dateTime The date and time of the point. * @param integer $elevation The elevation (in meters). * @param integer $hr The hear rate. * @return \SportTrackerConnector\Workout\Workout\TrackPoint */ private function getTrackPoint($latitude, $longitude, $dateTime, $elevation = null, $hr = null) { $trackPoint = new TrackPoint($latitude, $longitude, new \DateTime($dateTime, new \DateTimeZone('UTC'))); if ($elevation !== null) { $trackPoint->setElevation($elevation); } if ($hr !== null) { $trackPoint->addExtension(new HR($hr)); } return $trackPoint; }
/** * Test setting the elevation with invalid values. * * @dataProvider dataProviderTestSetGetElevationError * @param number $elevation The elevation to set. */ public function testSetGetElevationError($elevation) { $trackPoint = new TrackPoint(null, null, new DateTime()); $this->setExpectedException('InvalidArgumentException', 'Elevation for a tracking point must be a number.'); $trackPoint->setElevation($elevation); }
/** * Get the speed between this point and another point in km/h. * * @param TrackPoint $trackPoint The other point. * @return float */ public function speed(TrackPoint $trackPoint) { $start = $this->getDateTime(); $end = $trackPoint->getDateTime(); $dateDiff = $start->diff($end); $secondsDifference = $dateDiff->days * 86400 + $dateDiff->h * 3600 + $dateDiff->i * 60 + $dateDiff->s; if ($secondsDifference === 0) { return 0; } if ($this->hasDistance() === true && $trackPoint->hasDistance()) { $distance = abs($this->getDistance() - $trackPoint->getDistance()); } else { $distance = $this->distance($trackPoint); } return $distance / $secondsDifference * 3.6; }
/** * Download a workout. * * @param integer $idWorkout The ID of the workout to download. * @return Workout */ public function downloadWorkout($idWorkout) { $this->logger->debug('Downloading JSON summary for workout "' . $idWorkout . '"'); $workout = new Workout(); $track = new Track(); try { $workoutDetails = $this->getStravaAPI()->getWorkout($idWorkout); $this->logger->debug('Writing track points.'); $pointsSize = count(reset($workoutDetails)); for ($i = 0; $i < $pointsSize; $i++) { $latitude = $workoutDetails['latlng'][$i][0]; $longitude = $workoutDetails['latlng'][$i][1]; $dateTime = $workoutDetails['time'][$i]; $dateTime->setTimezone($this->getTimeZone()); $trackPoint = new TrackPoint($latitude, $longitude, $dateTime); if (isset($workoutDetails['altitude'])) { $trackPoint->setElevation($workoutDetails['altitude'][$i]); } if (isset($workoutDetails['heartrate'])) { $trackPoint->addExtension(new HR($workoutDetails['heartrate'][$i])); } $track->addTrackPoint($trackPoint); } } catch (NoTrackPointsFoundException $e) { $this->logger->warning('No track points found for workout "' . $idWorkout . '".'); } $workout->addTrack($track); return $workout; }
/** * Download a workout. * * @param integer $idWorkout The ID of the workout to download. * @return Workout */ public function downloadWorkout($idWorkout) { $this->logger->debug('Downloading JSON for workout "' . $idWorkout . '"'); $json = $this->getEndomondoAPI()->getWorkout($idWorkout); $workout = new Workout(); $track = new Track(); $track->setSport($this->getSportMapper()->getSportFromCode($json['sport'])); if (isset($json['points'])) { $this->logger->debug('Writing track points.'); foreach ($json['points'] as $point) { $trackPoint = new TrackPoint($point['lat'], $point['lng'], new DateTime($point['time'], $this->getTimeZone())); if (isset($point['alt'])) { $trackPoint->setElevation($point['alt']); } if (isset($point['hr'])) { $trackPoint->addExtension(new HR($point['hr'])); } $track->addTrackPoint($trackPoint); } } else { $this->logger->warning('No track points found for workout "' . $idWorkout . '".'); } $workout->addTrack($track); return $workout; }
/** * Flatten a track point to be posted on endomondo. * * @param TrackPoint $trackPoint The track point to flatten. * @param float $distance The total distance the point in meters. * @param float $speed The speed the point in km/h from the previous point. * @return string */ private function flattenTrackPoint(TrackPoint $trackPoint, $distance, $speed) { $dateTime = clone $trackPoint->getDateTime(); $dateTime->setTimezone(new \DateTimeZone('UTC')); return $this->formatEndomondoTrackPoint($dateTime, self::INSTRUCTION_START, $trackPoint->getLatitude(), $trackPoint->getLongitude(), $distance, $speed, $trackPoint->getElevation(), $trackPoint->hasExtension(HR::ID) ? $trackPoint->getExtension(HR::ID)->getValue() : ''); }
/** * Get a track point. * * @param string $lat The latitude. * @param string $lon The longitude. * @param string $time The time. * @param integer $ele The elevation. * @param integer $heartRate The heart rate. * @return TrackPoint */ private function getTrackPoint($lat, $lon, $time, $ele, $heartRate) { $trackPoint = new TrackPoint($lat, $lon, new DateTime($time)); $trackPoint->setElevation($ele); $extensions = array(new HR($heartRate)); $trackPoint->setExtensions($extensions); return $trackPoint; }