tw: drop invalid GPS data

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/579/head
Varun Patil 2023-04-14 00:00:19 -07:00
parent 9ab896989f
commit 5438223b29
3 changed files with 31 additions and 8 deletions

View File

@ -86,6 +86,7 @@ class TimelineWrite
// Process location data
// This also modifies the exif array in-place to set the LocationTZID
// and drop the GPS data if it is not valid
[$lat, $lon, $mapCluster] = $this->processExifLocation($fileId, $exif, $prevRow);
// Get date parameters (after setting timezone offset)

View File

@ -8,6 +8,9 @@ use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
const LAT_KEY = 'GPSLatitude';
const LON_KEY = 'GPSLongitude';
trait TimelineWritePlaces
{
protected IDBConnection $connection;
@ -27,8 +30,7 @@ trait TimelineWritePlaces
protected function processExifLocation(int $fileId, array &$exif, $prevRow): array
{
// Store location data
$lat = \array_key_exists('GPSLatitude', $exif) ? (float) $exif['GPSLatitude'] : null;
$lon = \array_key_exists('GPSLongitude', $exif) ? (float) $exif['GPSLongitude'] : null;
[$lat, $lon] = self::readCoord($exif);
$oldLat = $prevRow ? (float) $prevRow['lat'] : null;
$oldLon = $prevRow ? (float) $prevRow['lon'] : null;
$mapCluster = $prevRow ? (int) $prevRow['mapcluster'] : -1;
@ -165,4 +167,30 @@ trait TimelineWritePlaces
$exif['LocationTZID'] = $tzName;
}
}
/**
* Read coordinates from array and round to 6 decimal places.
*
* Modifies the array to remove invalid coordinates.
*/
private static function readCoord(array &$exif)
{
$lat = array_key_exists(LAT_KEY, $exif) ? round((float) $exif[LAT_KEY], 6) : null;
$lon = array_key_exists(LON_KEY, $exif) ? round((float) $exif[LON_KEY], 6) : null;
// Make sure we have valid coordinates
if (null === $lat || null === $lon || abs($lat) > 90 || abs($lon) > 180 || ($lat < 0.00001 && $lon < 0.00001)) {
$lat = $lon = null;
}
// Remove invalid coordinates
if (null === $lat && array_key_exists(LAT_KEY, $exif)) {
unset($exif[LAT_KEY]);
}
if (null === $lon && array_key_exists(LON_KEY, $exif)) {
unset($exif[LON_KEY]);
}
return [$lat, $lon];
}
}

View File

@ -131,12 +131,6 @@ class Exif
}
}
// Ignore zero lat lng
if (\array_key_exists('GPSLatitude', $exif) && abs((float) $exif['GPSLatitude']) < 0.0001
&& \array_key_exists('GPSLongitude', $exif) && abs((float) $exif['GPSLongitude']) < 0.0001) {
unset($exif['GPSLatitude'], $exif['GPSLongitude']);
}
return $exif;
}