Skip to content

Commit ef77c37

Browse files
kamil4nagmat84
andauthored
Ignore empty GPS data (#49)
Co-authored-by: Matthias Nagel <[email protected]>
1 parent 33fb4bd commit ef77c37

File tree

6 files changed

+124
-19
lines changed

6 files changed

+124
-19
lines changed

lib/PHPExif/Mapper/Exiftool.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,13 @@ public function mapRawData(array $data) : array
286286
}
287287
break;
288288
case self::GPSLATITUDE:
289-
$latitudeRef = !array_key_exists('GPS:GPSLatitudeRef', $data) ?
290-
'N' : $data['GPS:GPSLatitudeRef'][0];
291289
$value = $this->extractGPSCoordinates($value);
292290
if ($value === false) {
293291
continue 2;
294292
}
293+
$latitudeRef = !array_key_exists('GPS:GPSLatitudeRef', $data)
294+
|| $data['GPS:GPSLatitudeRef'] === null || $data['GPS:GPSLatitudeRef'] === '' ?
295+
'N' : $data['GPS:GPSLatitudeRef'][0];
295296
$value *= strtoupper($latitudeRef) === 'S' ? -1 : 1;
296297
break;
297298
case self::GPSLONGITUDE_QUICKTIME:
@@ -301,12 +302,13 @@ public function mapRawData(array $data) : array
301302
}
302303
break;
303304
case self::GPSLONGITUDE:
304-
$longitudeRef = !array_key_exists('GPS:GPSLongitudeRef', $data) ?
305-
'E' : $data['GPS:GPSLongitudeRef'][0];
306305
$value = $this->extractGPSCoordinates($value);
307306
if ($value === false) {
308307
continue 2;
309308
}
309+
$longitudeRef = !array_key_exists('GPS:GPSLongitudeRef', $data)
310+
|| $data['GPS:GPSLongitudeRef'] === null || $data['GPS:GPSLongitudeRef'] === '' ?
311+
'E' : $data['GPS:GPSLongitudeRef'][0];
310312
$value *= strtoupper($longitudeRef) === 'W' ? -1 : 1;
311313
break;
312314
case self::GPSALTITUDE:

lib/PHPExif/Mapper/ImageMagick.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,23 @@ public function mapRawData(array $data) : array
184184
$value = preg_split('/([\s,]+)/', $value)[0];
185185
break;
186186
case self::GPSLATITUDE:
187-
$latitudeRef = !array_key_exists('exif:GPSLatitudeRef', $data) ?
188-
'N' : $data['exif:GPSLatitudeRef'][0];
189187
$value = $this->extractGPSCoordinates($value);
190188
if ($value === false) {
191189
continue 2;
192190
}
191+
$latitudeRef = !array_key_exists('exif:GPSLatitudeRef', $data)
192+
|| $data['exif:GPSLatitudeRef'] === null || $data['exif:GPSLatitudeRef'] === '' ?
193+
'N' : $data['exif:GPSLatitudeRef'][0];
193194
$value *= strtoupper($latitudeRef) === 'S' ? -1 : 1;
194195
break;
195196
case self::GPSLONGITUDE:
196-
$longitudeRef = !array_key_exists('exif:GPSLongitudeRef', $data) ?
197-
'E' : $data['exif:GPSLongitudeRef'][0];
198197
$value = $this->extractGPSCoordinates($value);
199198
if ($value === false) {
200199
continue 2;
201200
}
201+
$longitudeRef = !array_key_exists('exif:GPSLongitudeRef', $data)
202+
|| $data['exif:GPSLongitudeRef'] === null || $data['exif:GPSLongitudeRef'] === '' ?
203+
'E' : $data['exif:GPSLongitudeRef'][0];
202204
$value *= strtoupper($longitudeRef) === 'W' ? -1 : 1;
203205
break;
204206
case self::GPSALTITUDE:

lib/PHPExif/Mapper/Native.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ public function mapRawData(array $data) : array
235235
$value = (int) reset($resolutionParts);
236236
break;
237237
case self::GPSLATITUDE:
238-
$GPSLatitudeRef = '';
239-
if (array_key_exists('GPSLatitudeRef', $data) && $data['GPSLatitudeRef'][0] !== '') {
238+
$GPSLatitudeRef = 'N';
239+
if (array_key_exists('GPSLatitudeRef', $data)
240+
&& $data['GPSLatitudeRef'] !== null && $data['GPSLatitudeRef'][0] !== '') {
240241
$GPSLatitudeRef = $data['GPSLatitudeRef'][0];
241242
}
242243
$value = $this->extractGPSCoordinate((array)$value, $GPSLatitudeRef);
@@ -245,8 +246,9 @@ public function mapRawData(array $data) : array
245246
}
246247
break;
247248
case self::GPSLONGITUDE:
248-
$GPSLongitudeRef = '';
249-
if (array_key_exists('GPSLongitudeRef', $data) && $data['GPSLongitudeRef'][0] !== '') {
249+
$GPSLongitudeRef = 'E';
250+
if (array_key_exists('GPSLongitudeRef', $data)
251+
&& $data['GPSLongitudeRef'] !== null && $data['GPSLongitudeRef'][0] !== '') {
250252
$GPSLongitudeRef = $data['GPSLongitudeRef'][0];
251253
}
252254
$value = $this->extractGPSCoordinate((array)$value, $GPSLongitudeRef);
@@ -255,17 +257,17 @@ public function mapRawData(array $data) : array
255257
}
256258
break;
257259
case self::GPSALTITUDE:
260+
$value = $this->normalizeComponent($value);
261+
if ($value === false) {
262+
continue 2;
263+
}
258264
$flp = 1;
259265
if (array_key_exists('GPSAltitudeRef', $data) && $data['GPSAltitudeRef'][0] !== '') {
260266
$flp = (
261267
$data['GPSAltitudeRef'][0] === '1'
262268
|| $data['GPSAltitudeRef'][0] === "\u{0001}"
263269
) ? -1 : 1;
264270
}
265-
$value = $this->normalizeComponent($value);
266-
if ($value === false) {
267-
continue 2;
268-
}
269271
$value *= $flp;
270272
break;
271273
case self::IMGDIRECTION:

tests/PHPExif/Mapper/ExiftoolMapperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,24 @@ public function testMapRawDataOnlyLatitude()
400400
$this->assertCount(1, $result);
401401
}
402402

403+
/**
404+
* @group mapper
405+
* @covers \PHPExif\Mapper\Exiftool::mapRawData
406+
*/
407+
public function testMapRawDataCorrectlyIgnoresEmptyGPSData()
408+
{
409+
$result = $this->mapper->mapRawData(
410+
array(
411+
\PHPExif\Mapper\Exiftool::GPSLATITUDE => '',
412+
'GPS:GPSLatitudeRef' => '',
413+
\PHPExif\Mapper\Exiftool::GPSLONGITUDE => '',
414+
'GPS:GPSLongitudeRef' => '',
415+
)
416+
);
417+
418+
$this->assertEquals(false, reset($result));
419+
}
420+
403421
/**
404422
* @group mapper
405423
* @covers \PHPExif\Mapper\Exiftool::mapRawData
@@ -512,6 +530,21 @@ public function testMapRawDataCorrectlyNegativeAltitude()
512530
$this->assertEquals($expected, reset($result));
513531
}
514532

533+
/**
534+
* @group mapper
535+
* @covers \PHPExif\Mapper\Exiftool::mapRawData
536+
*/
537+
public function testMapRawDataCorrectlyIgnoresIncorrectAltitude()
538+
{
539+
$result = $this->mapper->mapRawData(
540+
array(
541+
\PHPExif\Mapper\Exiftool::GPSALTITUDE => 'undef',
542+
'GPS:GPSAltitudeRef' => '0',
543+
)
544+
);
545+
$this->assertEquals(false, reset($result));
546+
}
547+
515548
/**
516549
* @group mapper
517550
* @covers \PHPExif\Mapper\Exiftool::mapRawData

tests/PHPExif/Mapper/ImageMagickMapperTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,9 @@ public function testMapRawDataIncorrectlyFormatedGPSData()
361361
$result = $this->mapper->mapRawData(
362362
array(
363363
\PHPExif\Mapper\ImageMagick::GPSLATITUDE => '40/1 20/1 42857/100000',
364-
'GPS:GPSLatitudeRef' => 'N',
364+
'exif:GPSLatitudeRef' => 'N',
365365
\PHPExif\Mapper\ImageMagick::GPSLONGITUDE => '20/1 10/1 233333/100000',
366-
'GPS:GPSLongitudeRef' => 'W',
366+
'exif:GPSLongitudeRef' => 'W',
367367
)
368368
);
369369
$this->assertCount(0, $result);
@@ -402,13 +402,31 @@ public function testMapRawDataOnlyLatitude()
402402
$result = $this->mapper->mapRawData(
403403
array(
404404
\PHPExif\Mapper\ImageMagick::GPSLATITUDE => '40.333452381',
405-
'GPS:GPSLatitudeRef' => 'North',
405+
'exif:GPSLatitudeRef' => 'North',
406406
)
407407
);
408408

409409
$this->assertCount(1, $result);
410410
}
411411

412+
/**
413+
* @group mapper
414+
* @covers \PHPExif\Mapper\ImageMagick::mapRawData
415+
*/
416+
public function testMapRawDataCorrectlyIgnoresEmptyGPSData()
417+
{
418+
$result = $this->mapper->mapRawData(
419+
array(
420+
\PHPExif\Mapper\ImageMagick::GPSLATITUDE => '0/0, 0/0, 0/0',
421+
'exif:GPSLatitudeRef' => '',
422+
\PHPExif\Mapper\ImageMagick::GPSLONGITUDE => '0/0, 0/0, 0/0',
423+
'exif:GPSLongitudeRef' => '',
424+
)
425+
);
426+
427+
$this->assertEquals(false, reset($result));
428+
}
429+
412430

413431
public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
414432
{
@@ -477,6 +495,21 @@ public function testMapRawDataCorrectlyNegativeAltitude()
477495
$this->assertEquals($expected, reset($result));
478496
}
479497

498+
/**
499+
* @group mapper
500+
* @covers \PHPExif\Mapper\ImageMagick::mapRawData
501+
*/
502+
public function testMapRawDataCorrectlyIgnoresIncorrectAltitude()
503+
{
504+
$result = $this->mapper->mapRawData(
505+
array(
506+
\PHPExif\Mapper\ImageMagick::GPSALTITUDE => '0/0',
507+
'exif:GPSAltitudeRef' => '0',
508+
)
509+
);
510+
$this->assertEquals(false, reset($result));
511+
}
512+
480513

481514
/**
482515
* @group mapper

tests/PHPExif/Mapper/NativeMapperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,24 @@ public function testMapRawDataCorrectlyFormatsGPSData()
345345
}
346346
}
347347

348+
/**
349+
* @group mapper
350+
* @covers \PHPExif\Mapper\Native::mapRawData
351+
*/
352+
public function testMapRawDataCorrectlyIgnoresEmptyGPSData()
353+
{
354+
$result = $this->mapper->mapRawData(
355+
array(
356+
'GPSLatitude' => array('0/0', '0/0', '0/0'),
357+
'GPSLatitudeRef' => null,
358+
'GPSLongitude' => array('0/0', '0/0', '0/0'),
359+
'GPSLongitudeRef' => null,
360+
)
361+
);
362+
363+
$this->assertEquals(false, reset($result));
364+
}
365+
348366
/**
349367
* @group mapper
350368
* @covers \PHPExif\Mapper\Native::mapRawData
@@ -368,6 +386,21 @@ public function testMapRawDataCorrectlyFormatsAltitudeData()
368386
}
369387
}
370388

389+
/**
390+
* @group mapper
391+
* @covers \PHPExif\Mapper\Native::mapRawData
392+
*/
393+
public function testMapRawDataCorrectlyIgnoresIncorrectAltitude()
394+
{
395+
$result = $this->mapper->mapRawData(
396+
array(
397+
'GPSAltitude' => "0/0",
398+
'GPSAltitudeRef' => chr(0),
399+
)
400+
);
401+
$this->assertEquals(false, reset($result));
402+
}
403+
371404
public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
372405
{
373406
$rawData = array(

0 commit comments

Comments
 (0)