From 5438223b29958459453613e88ca8ad76639af5e8 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 14 Apr 2023 00:00:19 -0700 Subject: [PATCH] tw: drop invalid GPS data Signed-off-by: Varun Patil --- lib/Db/TimelineWrite.php | 1 + lib/Db/TimelineWritePlaces.php | 32 ++++++++++++++++++++++++++++++-- lib/Exif.php | 6 ------ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/Db/TimelineWrite.php b/lib/Db/TimelineWrite.php index b1918545..fd881626 100644 --- a/lib/Db/TimelineWrite.php +++ b/lib/Db/TimelineWrite.php @@ -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) diff --git a/lib/Db/TimelineWritePlaces.php b/lib/Db/TimelineWritePlaces.php index ba3489ed..bf07c4d0 100644 --- a/lib/Db/TimelineWritePlaces.php +++ b/lib/Db/TimelineWritePlaces.php @@ -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]; + } } diff --git a/lib/Exif.php b/lib/Exif.php index d5bf00c0..65b243e5 100644 --- a/lib/Exif.php +++ b/lib/Exif.php @@ -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; }