1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Mapper for mapping data between raw input and a Height VO |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/PHPExif/php-exif-native for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2016 Tom Van Herreweghe <[email protected]> |
7
|
|
|
* @license http://github.com/PHPExif/php-exif-native/blob/master/LICENSE MIT License |
8
|
|
|
* @category PHPExif |
9
|
|
|
* @package Native |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPExif\Adapter\Native\Reader\Mapper\Exif; |
13
|
|
|
|
14
|
|
|
use PHPExif\Common\Data\ExifInterface; |
15
|
|
|
use PHPExif\Common\Data\ValueObject\Coordinates; |
16
|
|
|
use PHPExif\Common\Data\ValueObject\DigitalDegrees; |
17
|
|
|
use PHPExif\Common\Mapper\FieldMapper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Mapper |
21
|
|
|
* |
22
|
|
|
* @category PHPExif |
23
|
|
|
* @package Native |
24
|
|
|
*/ |
25
|
|
|
class CoordinatesFieldMapper implements FieldMapper |
26
|
|
|
{ |
27
|
|
|
use GuardInvalidArgumentsTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public function getSupportedFields() |
33
|
|
|
{ |
34
|
|
|
return array( |
35
|
|
|
Coordinates::class, |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
public function mapField($field, array $input, &$output) |
43
|
|
|
{ |
44
|
|
|
$this->guardInvalidArguments($field, $input, $output); |
45
|
|
|
|
46
|
|
|
if (!(array_key_exists('GPSLatitude', $input) && array_key_exists('GPSLongitude', $input))) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$latitude = $this->extractGpsCoordinate($input['GPSLatitude']); |
51
|
|
|
$longitude = $this->extractGpsCoordinate($input['GPSLongitude']); |
52
|
|
|
$latitudeRef = empty($input['GPSLatitudeRef'][0]) ? 'N' : $input['GPSLatitudeRef'][0]; |
53
|
|
|
$longitudeRef = empty($input['GPSLongitudeRef'][0]) ? 'E' : $input['GPSLongitudeRef'][0]; |
54
|
|
|
|
55
|
|
|
$coordinates = new Coordinates( |
56
|
|
|
new DigitalDegrees( |
57
|
|
|
round((strtoupper($latitudeRef) === 'S' ? -1 : 1) * $latitude, 6) |
58
|
|
|
), |
59
|
|
|
new DigitalDegrees( |
60
|
|
|
round((strtoupper($longitudeRef) === 'W' ? -1 : 1) * $longitude, 6) |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$output = $output->withCoordinates($coordinates); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Extract & convert GPS coordinate to digital degrees |
69
|
|
|
* |
70
|
|
|
* @param array $data |
71
|
|
|
* |
72
|
|
|
* @return float |
73
|
|
|
*/ |
74
|
|
|
protected function extractGpsCoordinate(array $data) |
75
|
|
|
{ |
76
|
|
|
// make sure we always have 3 components |
77
|
|
|
$default = [0, 0, 0]; |
78
|
|
|
$data = array_replace($default, $data); |
79
|
|
|
|
80
|
|
|
// normalize the components |
81
|
|
|
array_walk($data, array($this, 'normalizeCoordinatePartData')); |
82
|
|
|
|
83
|
|
|
// convert to digital degrees |
84
|
|
|
return floatval($data[0]) + floatval($data[1]/60) + floatval($data[2]/3600); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Converts notations like 5610/100 to 56.1 |
89
|
|
|
* |
90
|
|
|
* @param mixed $data |
91
|
|
|
*/ |
92
|
|
|
protected function normalizeCoordinatePartData(&$data) |
93
|
|
|
{ |
94
|
|
|
$parts = explode('/', $data); |
95
|
|
|
|
96
|
|
|
if (count($parts) == 1) { |
97
|
|
|
return $parts[0]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$data = floatval($parts[0]) / floatval($parts[1]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|