2023-02-09 05:55:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
2023-10-22 20:37:50 +00:00
|
|
|
use OCA\Memories\Settings\SystemConfig;
|
2023-02-09 05:55:12 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
2023-03-28 00:50:10 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-04-14 07:00:19 +00:00
|
|
|
const LAT_KEY = 'GPSLatitude';
|
|
|
|
const LON_KEY = 'GPSLongitude';
|
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
trait TimelineWritePlaces
|
|
|
|
{
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add places data for a file.
|
2023-03-08 19:32:57 +00:00
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @param int $fileId The file ID
|
|
|
|
* @param ?float $lat The latitude of the file
|
|
|
|
* @param ?float $lon The longitude of the file
|
2023-03-28 00:50:10 +00:00
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @return int[] The list of osm_id of the places
|
2023-02-09 05:55:12 +00:00
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function updatePlacesData(int $fileId, ?float $lat, ?float $lon): array
|
2023-02-09 05:55:12 +00:00
|
|
|
{
|
|
|
|
// Get GIS type
|
2023-10-22 20:37:50 +00:00
|
|
|
$gisType = SystemConfig::gisType();
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-03-08 19:32:57 +00:00
|
|
|
// Check if valid
|
2023-03-08 19:48:36 +00:00
|
|
|
if ($gisType <= 0) {
|
2023-03-28 00:50:10 +00:00
|
|
|
return [];
|
2023-03-08 19:48:36 +00:00
|
|
|
}
|
2023-03-08 19:32:57 +00:00
|
|
|
|
|
|
|
// Delete previous records
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('memories_places')
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
|
|
|
|
// Just remove from if the point is no longer valid
|
|
|
|
if (null === $lat || null === $lon) {
|
2023-03-28 00:50:10 +00:00
|
|
|
return [];
|
2023-03-08 19:32:57 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 06:12:49 +00:00
|
|
|
// Get places
|
|
|
|
$rows = \OC::$server->get(\OCA\Memories\Service\Places::class)->queryPoint($lat, $lon);
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-05-30 06:12:49 +00:00
|
|
|
// Get last ID, i.e. the ID with highest admin_level but <= 8
|
2023-08-30 18:10:08 +00:00
|
|
|
$crows = array_filter($rows, static fn ($row) => $row['admin_level'] <= 8);
|
2023-07-03 19:02:52 +00:00
|
|
|
$markRow = array_pop($crows);
|
2023-03-24 01:12:39 +00:00
|
|
|
|
|
|
|
// Insert records in transaction
|
|
|
|
$this->connection->beginTransaction();
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
foreach ($rows as $row) {
|
2023-05-30 06:12:49 +00:00
|
|
|
$isMark = $markRow && $row['osm_id'] === $markRow['osm_id'];
|
|
|
|
|
|
|
|
// Insert the place
|
2023-02-09 05:55:12 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert('memories_places')
|
|
|
|
->values([
|
|
|
|
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
|
|
|
|
'osm_id' => $query->createNamedParameter($row['osm_id'], IQueryBuilder::PARAM_INT),
|
2023-05-30 06:12:49 +00:00
|
|
|
'mark' => $query->createNamedParameter($isMark, IQueryBuilder::PARAM_BOOL),
|
2023-02-09 05:55:12 +00:00
|
|
|
])
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
}
|
2023-03-24 01:12:39 +00:00
|
|
|
|
|
|
|
$this->connection->commit();
|
2023-03-28 00:50:10 +00:00
|
|
|
|
|
|
|
// Return list of osm_id
|
2023-10-15 19:46:35 +00:00
|
|
|
return array_map(static fn ($row) => (int) $row['osm_id'], $rows);
|
2023-03-28 00:50:10 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 02:53:06 +00:00
|
|
|
/**
|
|
|
|
* Process the location part of exif data.
|
|
|
|
*
|
|
|
|
* Also update the exif data with the tzid from location (LocationTZID)
|
|
|
|
* Performs an in-place update of the exif data.
|
|
|
|
*
|
2023-10-14 09:07:18 +00:00
|
|
|
* @param int $fileId The file ID
|
|
|
|
* @param array $exif The exif data (will change)
|
|
|
|
* @param ?array $prevRow The previous row of data
|
2023-04-17 02:53:06 +00:00
|
|
|
*
|
|
|
|
* @return array Update values
|
|
|
|
*/
|
2023-10-14 09:07:18 +00:00
|
|
|
protected function processExifLocation(int $fileId, array &$exif, ?array $prevRow): array
|
2023-04-17 02:53:06 +00:00
|
|
|
{
|
|
|
|
// Store location data
|
|
|
|
[$lat, $lon] = self::readCoord($exif);
|
|
|
|
$oldLat = $prevRow ? (float) $prevRow['lat'] : null;
|
|
|
|
$oldLon = $prevRow ? (float) $prevRow['lon'] : null;
|
|
|
|
$mapCluster = $prevRow ? (int) $prevRow['mapcluster'] : -1;
|
|
|
|
$osmIds = [];
|
|
|
|
|
|
|
|
if ($lat || $lon || $oldLat || $oldLon) {
|
|
|
|
try {
|
|
|
|
$mapCluster = $this->mapGetCluster($mapCluster, $lat, $lon, $oldLat, $oldLon);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$logger = \OC::$server->get(LoggerInterface::class);
|
|
|
|
$logger->log(3, 'Error updating map cluster data: '.$e->getMessage(), ['app' => 'memories']);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$osmIds = $this->updatePlacesData($fileId, $lat, $lon);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$logger = \OC::$server->get(LoggerInterface::class);
|
|
|
|
$logger->log(3, 'Error updating places data: '.$e->getMessage(), ['app' => 'memories']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NULL if invalid
|
|
|
|
$mapCluster = $mapCluster <= 0 ? null : $mapCluster;
|
|
|
|
|
|
|
|
// Set tzid from location if not present
|
|
|
|
$this->setTzidFromLocation($exif, $osmIds);
|
|
|
|
|
|
|
|
// Return update values
|
|
|
|
return [$lat, $lon, $mapCluster, $osmIds];
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:50:10 +00:00
|
|
|
/**
|
|
|
|
* Set timezone offset from location if not present.
|
|
|
|
*
|
|
|
|
* @param array $exif The exif data
|
|
|
|
* @param array $osmIds The list of osm_id of the places
|
|
|
|
*/
|
|
|
|
private function setTzidFromLocation(array &$exif, array $osmIds): void
|
|
|
|
{
|
|
|
|
// Make sure we have some places
|
|
|
|
if (empty($osmIds)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get timezone offset from places
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('name')
|
|
|
|
->from('memories_planet')
|
|
|
|
->where($query->expr()->in('osm_id', $query->createNamedParameter($osmIds, IQueryBuilder::PARAM_INT_ARRAY)))
|
2023-04-25 05:07:58 +00:00
|
|
|
->andWhere($query->expr()->eq('admin_level', $query->expr()->literal(-7, IQueryBuilder::PARAM_INT)))
|
2023-03-28 00:50:10 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
// Get name of timezone
|
|
|
|
$tzName = $query->executeQuery()->fetchOne();
|
|
|
|
if ($tzName) {
|
|
|
|
$exif['LocationTZID'] = $tzName;
|
|
|
|
}
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|
2023-04-14 07:00:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read coordinates from array and round to 6 decimal places.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* Modifies the EXIF array to remove invalid coordinates.
|
2023-10-14 08:25:50 +00:00
|
|
|
*
|
|
|
|
* @return (null|float)[]
|
|
|
|
*
|
|
|
|
* @psalm-return list{float|null, float|null}
|
2023-04-14 07:00:19 +00:00
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
private static function readCoord(array &$exif): array
|
2023-04-14 07:00:19 +00:00
|
|
|
{
|
2023-04-14 07:02:16 +00:00
|
|
|
$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;
|
2023-04-14 07:00:19 +00:00
|
|
|
|
|
|
|
// Make sure we have valid coordinates
|
2023-05-17 04:22:26 +00:00
|
|
|
if (null === $lat || null === $lon
|
|
|
|
|| abs($lat) > 90 || abs($lon) > 180
|
|
|
|
|| (abs($lat) < 0.00001 && abs($lon) < 0.00001)) {
|
2023-04-14 07:00:19 +00:00
|
|
|
$lat = $lon = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove invalid coordinates
|
2023-04-14 07:02:16 +00:00
|
|
|
if (null === $lat && \array_key_exists(LAT_KEY, $exif)) {
|
2023-04-14 07:00:19 +00:00
|
|
|
unset($exif[LAT_KEY]);
|
|
|
|
}
|
2023-04-14 07:02:16 +00:00
|
|
|
if (null === $lon && \array_key_exists(LON_KEY, $exif)) {
|
2023-04-14 07:00:19 +00:00
|
|
|
unset($exif[LON_KEY]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$lat, $lon];
|
|
|
|
}
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|