Skip to content

Commit 401686c

Browse files
author
Tom Van Herreweghe
committed
Unit tests for GPS stuff
Signed-off-by: Tom Van Herreweghe <[email protected]>
1 parent 1e2e623 commit 401686c

File tree

6 files changed

+86
-24
lines changed

6 files changed

+86
-24
lines changed

lib/PHPExif/Exif.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,10 @@ public function getGPS()
827827
/**
828828
* Sets the GPS coordinates
829829
*
830-
* @param array $value
830+
* @param string $value
831831
* @return \PHPExif\Exif
832832
*/
833-
public function setGPS(array $value)
833+
public function setGPS($value)
834834
{
835835
$this->data[self::GPS] = $value;
836836

lib/PHPExif/Mapper/Exiftool.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class Exiftool implements MapperInterface
5050
const TITLE = 'Title';
5151
const XRESOLUTION = 'XResolution';
5252
const YRESOLUTION = 'YResolution';
53-
const GPSLATITUDEREF = 'GPSLatitudeRef';
54-
const GPSLONGITUDEREF = 'GPSLongitudeRef';
53+
const GPSLATITUDE = 'GPSLatitude';
54+
const GPSLONGITUDE = 'GPSLongitude';
5555

5656
/**
5757
* Maps the ExifTool fields to the fields of
@@ -86,8 +86,8 @@ class Exiftool implements MapperInterface
8686
self::YRESOLUTION => Exif::VERTICAL_RESOLUTION,
8787
self::IMAGEWIDTH => Exif::WIDTH,
8888
self::CAPTIONABSTRACT => Exif::CAPTION,
89-
self::GPSLATITUDEREF => Exif::GPS,
90-
self::GPSLONGITUDEREF => Exif::GPS,
89+
self::GPSLATITUDE => Exif::GPS,
90+
self::GPSLONGITUDE => Exif::GPS,
9191
);
9292

9393
/**
@@ -145,10 +145,10 @@ public function mapRawData(array $data)
145145
$focalLengthParts = explode(' ', $value);
146146
$value = (int) reset($focalLengthParts);
147147
break;
148-
case self::GPSLATITUDEREF:
148+
case self::GPSLATITUDE:
149149
$gpsData['lat'] = $this->extractGPSCoordinates($value);
150150
break;
151-
case self::GPSLONGITUDEREF:
151+
case self::GPSLONGITUDE:
152152
$gpsData['lon'] = $this->extractGPSCoordinates($value);
153153
break;
154154
}
@@ -165,11 +165,11 @@ public function mapRawData(array $data)
165165
if ($latitude !== false && $longitude !== false) {
166166
$gpsLocation = sprintf(
167167
'%s,%s',
168-
(strtoupper($data[self::GPSLATITUDEREF][0]) === 'S' ? -1 : 1) * $latitude,
169-
(strtoupper($data[self::GPSLONGITUDEREF][0]) === 'W' ? -1 : 1) * $longitude
168+
(strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $latitude,
169+
(strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $longitude
170170
);
171171

172-
$key = $this->map[self::GPSLATITUDEREF];
172+
$key = $this->map[self::GPSLATITUDE];
173173

174174
$mappedData[$key] = $gpsLocation;
175175
}

lib/PHPExif/Mapper/Native.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class Native implements MapperInterface
4949
const WIDTH = 'Width';
5050
const XRESOLUTION = 'XResolution';
5151
const YRESOLUTION = 'YResolution';
52-
const GPSLATITUDEREF = 'GPSLatitudeRef';
53-
const GPSLONGITUDEREF = 'GPSLongitudeRef';
52+
const GPSLATITUDE = 'GPSLatitude';
53+
const GPSLONGITUDE = 'GPSLongitude';
5454

5555
const SECTION_FILE = 'FILE';
5656
const SECTION_COMPUTED = 'COMPUTED';
@@ -109,8 +109,8 @@ class Native implements MapperInterface
109109
self::SOFTWARE => Exif::SOFTWARE,
110110
self::XRESOLUTION => Exif::HORIZONTAL_RESOLUTION,
111111
self::YRESOLUTION => Exif::VERTICAL_RESOLUTION,
112-
self::GPSLATITUDEREF => Exif::GPS,
113-
self::GPSLONGITUDEREF => Exif::GPS,
112+
self::GPSLATITUDE => Exif::GPS,
113+
self::GPSLONGITUDE => Exif::GPS,
114114
);
115115

116116
/**
@@ -162,10 +162,10 @@ public function mapRawData(array $data)
162162
$resolutionParts = explode('/', $value);
163163
$value = (int)reset($resolutionParts);
164164
break;
165-
case self::GPSLATITUDEREF:
165+
case self::GPSLATITUDE:
166166
$gpsData['lat'] = $this->extractGPSCoordinate($value);
167167
break;
168-
case self::GPSLONGITUDEREF:
168+
case self::GPSLONGITUDE:
169169
$gpsData['lon'] = $this->extractGPSCoordinate($value);
170170
break;
171171
}
@@ -177,8 +177,8 @@ public function mapRawData(array $data)
177177
if (count($gpsData) === 2) {
178178
$gpsLocation = sprintf(
179179
'%s,%s',
180-
(strtoupper($data[self::GPSLATITUDEREF][0]) === 'S' ? -1 : 1) * $gpsData['lat'],
181-
(strtoupper($data[self::GPSLONGITUDEREF][0]) === 'W' ? -1 : 1) * $gpsData['lon']
180+
(strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'],
181+
(strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $gpsData['lon']
182182
);
183183
$mappedData[Exif::GPS] = $gpsLocation;
184184
}

tests/PHPExif/ExifTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,10 @@ public function testMutatorMethodsSetInProperty()
552552
break;
553553
case 'gps':
554554
$coords = '40.333452380556,-20.167314813889';
555+
$setter = 'setGPS';
555556
$this->exif->$setter($coords);
556557
$propertyValue = $reflProp->getValue($this->exif);
557-
$this->assertEquals($expected, $propertyValue[$value]);
558+
$this->assertEquals($coords, $propertyValue[$value]);
558559
break;
559560
case 'focalDistance':
560561
$setter = 'setFocusDistance';

tests/PHPExif/Mapper/ExiftoolMapperTest.php

+44-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function testMapRawDataMapsFieldsCorrectly()
4747
unset($map[\PHPExif\Mapper\Exiftool::CREATEDATE]);
4848
unset($map[\PHPExif\Mapper\Exiftool::EXPOSURETIME]);
4949
unset($map[\PHPExif\Mapper\Exiftool::FOCALLENGTH]);
50-
unset($map[\PHPExif\Mapper\Exiftool::GPSLATITUDEREF]);
51-
unset($map[\PHPExif\Mapper\Exiftool::GPSLONGITUDEREF]);
50+
unset($map[\PHPExif\Mapper\Exiftool::GPSLATITUDE]);
51+
unset($map[\PHPExif\Mapper\Exiftool::GPSLONGITUDE]);
5252

5353
// create raw data
5454
$keys = array_keys($map);
@@ -145,4 +145,46 @@ public function testMapRawDataCorrectlyFormatsFocalLength()
145145

146146
$this->assertEquals(15, reset($mapped));
147147
}
148+
149+
/**
150+
* @group mapper
151+
* @covers \PHPExif\Mapper\Exiftool::mapRawData
152+
*/
153+
public function testMapRawDataCorrectlyFormatsGPSData()
154+
{
155+
$this->mapper->setNumeric(false);
156+
$result = $this->mapper->mapRawData(
157+
array(
158+
'GPSLatitude' => '40 deg 20\' 0.42857" N',
159+
'GPSLatitudeRef' => 'North',
160+
'GPSLongitude' => '20 deg 10\' 2.33333" W',
161+
'GPSLongitudeRef' => 'West',
162+
)
163+
);
164+
165+
$expected = '40.333452380556,-20.167314813889';
166+
$this->assertCount(1, $result);
167+
$this->assertEquals($expected, reset($result));
168+
}
169+
170+
/**
171+
* @group mapper
172+
* @covers \PHPExif\Mapper\Exiftool::mapRawData
173+
*/
174+
public function testMapRawDataCorrectlyFormatsNumericGPSData()
175+
{
176+
$result = $this->mapper->mapRawData(
177+
array(
178+
'GPSLatitude' => '40.333452381',
179+
'GPSLatitudeRef' => 'North',
180+
'GPSLongitude' => '20.167314814',
181+
'GPSLongitudeRef' => 'West',
182+
)
183+
);
184+
185+
$expected = '40.333452381,-20.167314814';
186+
$this->assertCount(1, $result);
187+
$this->assertEquals($expected, reset($result));
188+
}
189+
148190
}

tests/PHPExif/Mapper/NativeMapperTest.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function testMapRawDataMapsFieldsCorrectly()
4747
unset($map[\PHPExif\Mapper\Native::FOCALLENGTH]);
4848
unset($map[\PHPExif\Mapper\Native::XRESOLUTION]);
4949
unset($map[\PHPExif\Mapper\Native::YRESOLUTION]);
50-
unset($map[\PHPExif\Mapper\Native::GPSLATITUDEREF]);
51-
unset($map[\PHPExif\Mapper\Native::GPSLONGITUDEREF]);
50+
unset($map[\PHPExif\Mapper\Native::GPSLATITUDE]);
51+
unset($map[\PHPExif\Mapper\Native::GPSLONGITUDE]);
5252

5353
// create raw data
5454
$keys = array_keys($map);
@@ -168,4 +168,23 @@ public function testMapRawDataFlattensRawDataWithSections()
168168
);
169169
$this->assertEquals($expected, $keys);
170170
}
171+
172+
/**
173+
* @group mapper
174+
* @covers \PHPExif\Mapper\Native::mapRawData
175+
*/
176+
public function testMapRawDataCorrectlyFormatsGPSData()
177+
{
178+
$result = $this->mapper->mapRawData(
179+
array(
180+
'GPSLatitude' => array('40/1', '20/1', '15/35'),
181+
'GPSLatitudeRef' => 'N',
182+
'GPSLongitude' => array('20/1', '10/1', '35/15'),
183+
'GPSLongitudeRef' => 'W',
184+
)
185+
);
186+
187+
$expected = '40.333452380952,-20.167314814815';
188+
$this->assertEquals($expected, reset($result));
189+
}
171190
}

0 commit comments

Comments
 (0)