Skip to content

Fixed normalization of zero degrees coordinates #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions lib/PHPExif/Mapper/Exiftool.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,19 @@ public function mapRawData(array $data)
}

// add GPS coordinates, if available
if (count($gpsData) === 2) {
$latitude = $gpsData['lat'];
$longitude = $gpsData['lon'];

if ($latitude !== false && $longitude !== false) {
$gpsLocation = sprintf(
'%s,%s',
(strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $latitude,
(strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $longitude
);

$key = $this->map[self::GPSLATITUDE];

$mappedData[$key] = $gpsLocation;
} else {
unset($mappedData[$this->map[self::GPSLATITUDE]]);
}
if (count($gpsData) === 2 && $gpsData['lat'] !== false && $gpsData['lon'] !== false) {
$latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0];
$longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0];

$gpsLocation = sprintf(
'%s,%s',
(strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'],
(strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon']
);

$mappedData[Exif::GPS] = $gpsLocation;
} else {
unset($mappedData[$this->map[self::GPSLATITUDE]]);
unset($mappedData[Exif::GPS]);
}

return $mappedData;
Expand Down
27 changes: 23 additions & 4 deletions lib/PHPExif/Mapper/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,20 @@ public function mapRawData(array $data)
$mappedData[$key] = $value;
}

// add GPS coordinates, if available
if (count($gpsData) === 2) {
$latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0];
$longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0];

$gpsLocation = sprintf(
'%s,%s',
(strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'],
(strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $gpsData['lon']
(strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'],
(strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon']
);

$mappedData[Exif::GPS] = $gpsLocation;
} else {
unset($mappedData[Exif::GPS]);
}

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

return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600);
if (count($components) > 2) {
return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600);
}

return reset($components);
}

/**
Expand All @@ -225,6 +236,14 @@ protected function normalizeGPSComponent($component)
{
$parts = explode('/', $component);

return count($parts) === 1 ? $parts[0] : (int) reset($parts) / (int) end($parts);
if (count($parts) > 1) {
if ($parts[1]) {
return intval($parts[0]) / intval($parts[1]);
}

return 0;
}

return floatval(reset($parts));
}
}
15 changes: 15 additions & 0 deletions tests/PHPExif/Mapper/ExiftoolMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ public function testMapRawDataCorrectlyFormatsCreationDate()
);
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Exiftool::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresIncorrectCreationDate()
{
$rawData = array(
\PHPExif\Mapper\Exiftool::CREATEDATE => '2015:04:01',
);

$mapped = $this->mapper->mapRawData($rawData);

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

/**
* @group mapper
* @covers \PHPExif\Mapper\Exiftool::mapRawData
Expand Down
39 changes: 34 additions & 5 deletions tests/PHPExif/Mapper/NativeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ public function testMapRawDataCorrectlyFormatsDateTimeOriginal()
);
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
*/
public function testMapRawDataCorrectlyIgnoresIncorrectDateTimeOriginal()
{
$rawData = array(
\PHPExif\Mapper\Native::DATETIMEORIGINAL => '2015:04:01',
);

$mapped = $this->mapper->mapRawData($rawData);

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

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
Expand Down Expand Up @@ -175,17 +190,31 @@ public function testMapRawDataFlattensRawDataWithSections()
*/
public function testMapRawDataCorrectlyFormatsGPSData()
{
$result = $this->mapper->mapRawData(
array(
$expected = array(
'40.333452380952,-20.167314814815' => array(
'GPSLatitude' => array('40/1', '20/1', '15/35'),
'GPSLatitudeRef' => 'N',
'GPSLongitude' => array('20/1', '10/1', '35/15'),
'GPSLongitudeRef' => 'W',
)
),
'0,-0' => array(
'GPSLatitude' => array('0/0', '0/0', '0/0'),
'GPSLatitudeRef' => 'N',
'GPSLongitude' => array('0/0', '0/0', '0/0'),
'GPSLongitudeRef' => 'W',
),
'71.706936,-42.604303' => array(
'GPSLatitude' => array('71.706936'),
'GPSLatitudeRef' => 'N',
'GPSLongitude' => array('42.604303'),
'GPSLongitudeRef' => 'W',
),
);

$expected = '40.333452380952,-20.167314814815';
$this->assertEquals($expected, reset($result));
foreach ($expected as $key => $value) {
$result = $this->mapper->mapRawData($value);
$this->assertEquals($key, reset($result));
}
}

public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
Expand Down