Skip to content

Commit

Permalink
Ignore empty GPS data (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Nagel <[email protected]>
  • Loading branch information
kamil4 and nagmat84 authored Jul 18, 2022
1 parent 33fb4bd commit ef77c37
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 19 deletions.
10 changes: 6 additions & 4 deletions lib/PHPExif/Mapper/Exiftool.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,13 @@ public function mapRawData(array $data) : array
}
break;
case self::GPSLATITUDE:
$latitudeRef = !array_key_exists('GPS:GPSLatitudeRef', $data) ?
'N' : $data['GPS:GPSLatitudeRef'][0];
$value = $this->extractGPSCoordinates($value);
if ($value === false) {
continue 2;
}
$latitudeRef = !array_key_exists('GPS:GPSLatitudeRef', $data)
|| $data['GPS:GPSLatitudeRef'] === null || $data['GPS:GPSLatitudeRef'] === '' ?
'N' : $data['GPS:GPSLatitudeRef'][0];
$value *= strtoupper($latitudeRef) === 'S' ? -1 : 1;
break;
case self::GPSLONGITUDE_QUICKTIME:
Expand All @@ -301,12 +302,13 @@ public function mapRawData(array $data) : array
}
break;
case self::GPSLONGITUDE:
$longitudeRef = !array_key_exists('GPS:GPSLongitudeRef', $data) ?
'E' : $data['GPS:GPSLongitudeRef'][0];
$value = $this->extractGPSCoordinates($value);
if ($value === false) {
continue 2;
}
$longitudeRef = !array_key_exists('GPS:GPSLongitudeRef', $data)
|| $data['GPS:GPSLongitudeRef'] === null || $data['GPS:GPSLongitudeRef'] === '' ?
'E' : $data['GPS:GPSLongitudeRef'][0];
$value *= strtoupper($longitudeRef) === 'W' ? -1 : 1;
break;
case self::GPSALTITUDE:
Expand Down
10 changes: 6 additions & 4 deletions lib/PHPExif/Mapper/ImageMagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,23 @@ public function mapRawData(array $data) : array
$value = preg_split('/([\s,]+)/', $value)[0];
break;
case self::GPSLATITUDE:
$latitudeRef = !array_key_exists('exif:GPSLatitudeRef', $data) ?
'N' : $data['exif:GPSLatitudeRef'][0];
$value = $this->extractGPSCoordinates($value);
if ($value === false) {
continue 2;
}
$latitudeRef = !array_key_exists('exif:GPSLatitudeRef', $data)
|| $data['exif:GPSLatitudeRef'] === null || $data['exif:GPSLatitudeRef'] === '' ?
'N' : $data['exif:GPSLatitudeRef'][0];
$value *= strtoupper($latitudeRef) === 'S' ? -1 : 1;
break;
case self::GPSLONGITUDE:
$longitudeRef = !array_key_exists('exif:GPSLongitudeRef', $data) ?
'E' : $data['exif:GPSLongitudeRef'][0];
$value = $this->extractGPSCoordinates($value);
if ($value === false) {
continue 2;
}
$longitudeRef = !array_key_exists('exif:GPSLongitudeRef', $data)
|| $data['exif:GPSLongitudeRef'] === null || $data['exif:GPSLongitudeRef'] === '' ?
'E' : $data['exif:GPSLongitudeRef'][0];
$value *= strtoupper($longitudeRef) === 'W' ? -1 : 1;
break;
case self::GPSALTITUDE:
Expand Down
18 changes: 10 additions & 8 deletions lib/PHPExif/Mapper/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,9 @@ public function mapRawData(array $data) : array
$value = (int) reset($resolutionParts);
break;
case self::GPSLATITUDE:
$GPSLatitudeRef = '';
if (array_key_exists('GPSLatitudeRef', $data) && $data['GPSLatitudeRef'][0] !== '') {
$GPSLatitudeRef = 'N';
if (array_key_exists('GPSLatitudeRef', $data)
&& $data['GPSLatitudeRef'] !== null && $data['GPSLatitudeRef'][0] !== '') {
$GPSLatitudeRef = $data['GPSLatitudeRef'][0];
}
$value = $this->extractGPSCoordinate((array)$value, $GPSLatitudeRef);
Expand All @@ -245,8 +246,9 @@ public function mapRawData(array $data) : array
}
break;
case self::GPSLONGITUDE:
$GPSLongitudeRef = '';
if (array_key_exists('GPSLongitudeRef', $data) && $data['GPSLongitudeRef'][0] !== '') {
$GPSLongitudeRef = 'E';
if (array_key_exists('GPSLongitudeRef', $data)
&& $data['GPSLongitudeRef'] !== null && $data['GPSLongitudeRef'][0] !== '') {
$GPSLongitudeRef = $data['GPSLongitudeRef'][0];
}
$value = $this->extractGPSCoordinate((array)$value, $GPSLongitudeRef);
Expand All @@ -255,17 +257,17 @@ public function mapRawData(array $data) : array
}
break;
case self::GPSALTITUDE:
$value = $this->normalizeComponent($value);
if ($value === false) {
continue 2;
}
$flp = 1;
if (array_key_exists('GPSAltitudeRef', $data) && $data['GPSAltitudeRef'][0] !== '') {
$flp = (
$data['GPSAltitudeRef'][0] === '1'
|| $data['GPSAltitudeRef'][0] === "\u{0001}"
) ? -1 : 1;
}
$value = $this->normalizeComponent($value);
if ($value === false) {
continue 2;
}
$value *= $flp;
break;
case self::IMGDIRECTION:
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPExif/Mapper/ExiftoolMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,24 @@ public function testMapRawDataOnlyLatitude()
$this->assertCount(1, $result);
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Exiftool::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresEmptyGPSData()
{
$result = $this->mapper->mapRawData(
array(
\PHPExif\Mapper\Exiftool::GPSLATITUDE => '',
'GPS:GPSLatitudeRef' => '',
\PHPExif\Mapper\Exiftool::GPSLONGITUDE => '',
'GPS:GPSLongitudeRef' => '',
)
);

$this->assertEquals(false, reset($result));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Exiftool::mapRawData
Expand Down Expand Up @@ -512,6 +530,21 @@ public function testMapRawDataCorrectlyNegativeAltitude()
$this->assertEquals($expected, reset($result));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Exiftool::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresIncorrectAltitude()
{
$result = $this->mapper->mapRawData(
array(
\PHPExif\Mapper\Exiftool::GPSALTITUDE => 'undef',
'GPS:GPSAltitudeRef' => '0',
)
);
$this->assertEquals(false, reset($result));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Exiftool::mapRawData
Expand Down
39 changes: 36 additions & 3 deletions tests/PHPExif/Mapper/ImageMagickMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ public function testMapRawDataIncorrectlyFormatedGPSData()
$result = $this->mapper->mapRawData(
array(
\PHPExif\Mapper\ImageMagick::GPSLATITUDE => '40/1 20/1 42857/100000',
'GPS:GPSLatitudeRef' => 'N',
'exif:GPSLatitudeRef' => 'N',
\PHPExif\Mapper\ImageMagick::GPSLONGITUDE => '20/1 10/1 233333/100000',
'GPS:GPSLongitudeRef' => 'W',
'exif:GPSLongitudeRef' => 'W',
)
);
$this->assertCount(0, $result);
Expand Down Expand Up @@ -402,13 +402,31 @@ public function testMapRawDataOnlyLatitude()
$result = $this->mapper->mapRawData(
array(
\PHPExif\Mapper\ImageMagick::GPSLATITUDE => '40.333452381',
'GPS:GPSLatitudeRef' => 'North',
'exif:GPSLatitudeRef' => 'North',
)
);

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

/**
* @group mapper
* @covers \PHPExif\Mapper\ImageMagick::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresEmptyGPSData()
{
$result = $this->mapper->mapRawData(
array(
\PHPExif\Mapper\ImageMagick::GPSLATITUDE => '0/0, 0/0, 0/0',
'exif:GPSLatitudeRef' => '',
\PHPExif\Mapper\ImageMagick::GPSLONGITUDE => '0/0, 0/0, 0/0',
'exif:GPSLongitudeRef' => '',
)
);

$this->assertEquals(false, reset($result));
}


public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
{
Expand Down Expand Up @@ -477,6 +495,21 @@ public function testMapRawDataCorrectlyNegativeAltitude()
$this->assertEquals($expected, reset($result));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\ImageMagick::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresIncorrectAltitude()
{
$result = $this->mapper->mapRawData(
array(
\PHPExif\Mapper\ImageMagick::GPSALTITUDE => '0/0',
'exif:GPSAltitudeRef' => '0',
)
);
$this->assertEquals(false, reset($result));
}


/**
* @group mapper
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPExif/Mapper/NativeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,24 @@ public function testMapRawDataCorrectlyFormatsGPSData()
}
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresEmptyGPSData()
{
$result = $this->mapper->mapRawData(
array(
'GPSLatitude' => array('0/0', '0/0', '0/0'),
'GPSLatitudeRef' => null,
'GPSLongitude' => array('0/0', '0/0', '0/0'),
'GPSLongitudeRef' => null,
)
);

$this->assertEquals(false, reset($result));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
Expand All @@ -368,6 +386,21 @@ public function testMapRawDataCorrectlyFormatsAltitudeData()
}
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresIncorrectAltitude()
{
$result = $this->mapper->mapRawData(
array(
'GPSAltitude' => "0/0",
'GPSAltitudeRef' => chr(0),
)
);
$this->assertEquals(false, reset($result));
}

public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
{
$rawData = array(
Expand Down

0 comments on commit ef77c37

Please sign in to comment.