Skip to content

Commit d10ee92

Browse files
committed
Merge pull request #42 from bonzai/fix/zero-degrees-coordinates
Fixed normalization of zero degrees coordinates
2 parents 25941f2 + 3bf399f commit d10ee92

File tree

4 files changed

+84
-27
lines changed

4 files changed

+84
-27
lines changed

Diff for: lib/PHPExif/Mapper/Exiftool.php

+12-18
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,19 @@ public function mapRawData(array $data)
162162
}
163163

164164
// add GPS coordinates, if available
165-
if (count($gpsData) === 2) {
166-
$latitude = $gpsData['lat'];
167-
$longitude = $gpsData['lon'];
168-
169-
if ($latitude !== false && $longitude !== false) {
170-
$gpsLocation = sprintf(
171-
'%s,%s',
172-
(strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $latitude,
173-
(strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $longitude
174-
);
175-
176-
$key = $this->map[self::GPSLATITUDE];
177-
178-
$mappedData[$key] = $gpsLocation;
179-
} else {
180-
unset($mappedData[$this->map[self::GPSLATITUDE]]);
181-
}
165+
if (count($gpsData) === 2 && $gpsData['lat'] !== false && $gpsData['lon'] !== false) {
166+
$latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0];
167+
$longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0];
168+
169+
$gpsLocation = sprintf(
170+
'%s,%s',
171+
(strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'],
172+
(strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon']
173+
);
174+
175+
$mappedData[Exif::GPS] = $gpsLocation;
182176
} else {
183-
unset($mappedData[$this->map[self::GPSLATITUDE]]);
177+
unset($mappedData[Exif::GPS]);
184178
}
185179

186180
return $mappedData;

Diff for: lib/PHPExif/Mapper/Native.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,20 @@ public function mapRawData(array $data)
179179
$mappedData[$key] = $value;
180180
}
181181

182+
// add GPS coordinates, if available
182183
if (count($gpsData) === 2) {
184+
$latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0];
185+
$longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0];
186+
183187
$gpsLocation = sprintf(
184188
'%s,%s',
185-
(strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'],
186-
(strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $gpsData['lon']
189+
(strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'],
190+
(strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon']
187191
);
192+
188193
$mappedData[Exif::GPS] = $gpsLocation;
194+
} else {
195+
unset($mappedData[Exif::GPS]);
189196
}
190197

191198
return $mappedData;
@@ -212,7 +219,11 @@ protected function extractGPSCoordinate(array $components)
212219
{
213220
$components = array_map(array($this, 'normalizeGPSComponent'), $components);
214221

215-
return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600);
222+
if (count($components) > 2) {
223+
return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600);
224+
}
225+
226+
return reset($components);
216227
}
217228

218229
/**
@@ -225,6 +236,14 @@ protected function normalizeGPSComponent($component)
225236
{
226237
$parts = explode('/', $component);
227238

228-
return count($parts) === 1 ? $parts[0] : (int) reset($parts) / (int) end($parts);
239+
if (count($parts) > 1) {
240+
if ($parts[1]) {
241+
return intval($parts[0]) / intval($parts[1]);
242+
}
243+
244+
return 0;
245+
}
246+
247+
return floatval(reset($parts));
229248
}
230249
}

Diff for: tests/PHPExif/Mapper/ExiftoolMapperTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ public function testMapRawDataCorrectlyFormatsCreationDate()
116116
);
117117
}
118118

119+
/**
120+
* @group mapper
121+
* @covers \PHPExif\Mapper\Exiftool::mapRawData
122+
*/
123+
public function testMapRawDataCorrectlyIgnoresIncorrectCreationDate()
124+
{
125+
$rawData = array(
126+
\PHPExif\Mapper\Exiftool::CREATEDATE => '2015:04:01',
127+
);
128+
129+
$mapped = $this->mapper->mapRawData($rawData);
130+
131+
$this->assertEquals(false, reset($mapped));
132+
}
133+
119134
/**
120135
* @group mapper
121136
* @covers \PHPExif\Mapper\Exiftool::mapRawData

Diff for: tests/PHPExif/Mapper/NativeMapperTest.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ public function testMapRawDataCorrectlyFormatsDateTimeOriginal()
8686
);
8787
}
8888

89+
/**
90+
* @group mapper
91+
* @covers \PHPExif\Mapper\Native::mapRawData
92+
*/
93+
public function testMapRawDataCorrectlyIgnoresIncorrectDateTimeOriginal()
94+
{
95+
$rawData = array(
96+
\PHPExif\Mapper\Native::DATETIMEORIGINAL => '2015:04:01',
97+
);
98+
99+
$mapped = $this->mapper->mapRawData($rawData);
100+
101+
$this->assertEquals(false, reset($mapped));
102+
}
103+
89104
/**
90105
* @group mapper
91106
* @covers \PHPExif\Mapper\Native::mapRawData
@@ -175,17 +190,31 @@ public function testMapRawDataFlattensRawDataWithSections()
175190
*/
176191
public function testMapRawDataCorrectlyFormatsGPSData()
177192
{
178-
$result = $this->mapper->mapRawData(
179-
array(
193+
$expected = array(
194+
'40.333452380952,-20.167314814815' => array(
180195
'GPSLatitude' => array('40/1', '20/1', '15/35'),
181196
'GPSLatitudeRef' => 'N',
182197
'GPSLongitude' => array('20/1', '10/1', '35/15'),
183198
'GPSLongitudeRef' => 'W',
184-
)
199+
),
200+
'0,-0' => array(
201+
'GPSLatitude' => array('0/0', '0/0', '0/0'),
202+
'GPSLatitudeRef' => 'N',
203+
'GPSLongitude' => array('0/0', '0/0', '0/0'),
204+
'GPSLongitudeRef' => 'W',
205+
),
206+
'71.706936,-42.604303' => array(
207+
'GPSLatitude' => array('71.706936'),
208+
'GPSLatitudeRef' => 'N',
209+
'GPSLongitude' => array('42.604303'),
210+
'GPSLongitudeRef' => 'W',
211+
),
185212
);
186213

187-
$expected = '40.333452380952,-20.167314814815';
188-
$this->assertEquals($expected, reset($result));
214+
foreach ($expected as $key => $value) {
215+
$result = $this->mapper->mapRawData($value);
216+
$this->assertEquals($key, reset($result));
217+
}
189218
}
190219

191220
public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()

0 commit comments

Comments
 (0)