bulton-fr /
nmea-parser
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BultonFr\NMEA\Utils; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Tools to convert NMEA coordinates |
||
| 7 | * |
||
| 8 | * @package BultonFr\NMEA |
||
| 9 | * @author Vermeulen Maxime <[email protected]> |
||
| 10 | */ |
||
| 11 | class Coordinates |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Convert coordinate to degree format |
||
| 15 | * |
||
| 16 | * @param string $data Data readed by parser |
||
| 17 | * @param string $direction (default null) Direction of the coordinate |
||
| 18 | * @param string $isLongitude (default false) If is for longitude |
||
| 19 | * @param boolean $toString (default false) Return format |
||
| 20 | * |
||
| 21 | * @return \stdClass|string Change with $toString parameter value |
||
| 22 | */ |
||
| 23 | public static function convertGPSDataToDegree( |
||
| 24 | $data, |
||
| 25 | $direction = null, |
||
| 26 | $isLongitude = false, |
||
| 27 | $toString = false |
||
| 28 | ) { |
||
| 29 | 1 | $dotPosition = strpos($data, '.'); |
|
| 30 | 1 | $degreeEndPos = ($isLongitude === false) ? 2 : 3; |
|
| 31 | |||
| 32 | $obj = (object) [ |
||
| 33 | 1 | 'degree' => (int) substr($data, 0, $degreeEndPos), |
|
| 34 | 1 | 'minute' => (int) substr($data, $degreeEndPos, $dotPosition), |
|
| 35 | 1 | 'second' => (int) substr($data, $dotPosition+1) |
|
| 36 | ]; |
||
| 37 | |||
| 38 | 1 | if ($toString === false) { |
|
| 39 | 1 | return $obj; |
|
| 40 | } |
||
| 41 | |||
| 42 | 1 | return $obj->degree.'° '.$obj->minute.'\' '.$obj->second.'" '.$direction; |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Convert coordinate to decimal format |
||
| 47 | * |
||
| 48 | * @param string $data Data readed by parser |
||
| 49 | * @param string $isLongitude (default false) If is for longitude |
||
| 50 | * |
||
| 51 | * @return float |
||
| 52 | */ |
||
| 53 | public static function convertGPSDataToDec($data, $isLongitude = false) |
||
| 54 | { |
||
| 55 | 1 | $obj = static::convertGPSDataToDegree($data, null, $isLongitude); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 56 | |||
| 57 | /** |
||
| 58 | * DD = d + (min/60) + (sec/3600) |
||
| 59 | * @link http://www.latlong.net/degrees-minutes-seconds-to-decimal-degrees |
||
| 60 | */ |
||
| 61 | 1 | return ($obj->degree) + ($obj->minute / 60) + ($obj->second / 3600); |
|
| 62 | } |
||
| 63 | } |
||
| 64 |