2023-02-08 21:53:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2023-02-09 01:45:55 +00:00
|
|
|
use OCP\IDBConnection;
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
trait TimelineQueryMap
|
|
|
|
{
|
2023-04-02 01:14:37 +00:00
|
|
|
use TimelineQueryDays;
|
|
|
|
|
2023-02-09 01:45:55 +00:00
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
public function transformMapBoundsFilter(IQueryBuilder &$query, bool $aggregate, string $bounds, string $table = 'm'): void
|
2023-02-08 21:53:38 +00:00
|
|
|
{
|
|
|
|
$bounds = explode(',', $bounds);
|
|
|
|
$bounds = array_map('floatval', $bounds);
|
|
|
|
if (4 !== \count($bounds)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
$latCol = $table.'.lat';
|
|
|
|
$lonCol = $table.'.lon';
|
2023-02-08 21:53:38 +00:00
|
|
|
$query->andWhere(
|
|
|
|
$query->expr()->andX(
|
2023-02-09 05:55:12 +00:00
|
|
|
$query->expr()->gte($latCol, $query->createNamedParameter($bounds[0], IQueryBuilder::PARAM_STR)),
|
|
|
|
$query->expr()->lte($latCol, $query->createNamedParameter($bounds[1], IQueryBuilder::PARAM_STR)),
|
|
|
|
$query->expr()->gte($lonCol, $query->createNamedParameter($bounds[2], IQueryBuilder::PARAM_STR)),
|
2023-10-15 01:59:00 +00:00
|
|
|
$query->expr()->lte($lonCol, $query->createNamedParameter($bounds[3], IQueryBuilder::PARAM_STR)),
|
|
|
|
),
|
2023-02-08 21:53:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
public function getMapClusters(float $gridLen, string $bounds): array
|
|
|
|
{
|
2023-02-08 21:53:38 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
// Get the average location of each cluster
|
2023-08-17 17:48:43 +00:00
|
|
|
$query->selectAlias($query->createFunction('MAX(c.id)'), 'id')
|
|
|
|
->selectAlias($query->createFunction('COUNT(m.fileid)'), 'count')
|
|
|
|
->selectAlias($query->createFunction('AVG(c.lat)'), 'lat')
|
|
|
|
->selectAlias($query->createFunction('AVG(c.lon)'), 'lon')
|
2023-02-09 08:35:35 +00:00
|
|
|
->from('memories_mapclusters', 'c')
|
2023-02-08 21:53:38 +00:00
|
|
|
;
|
|
|
|
|
2023-02-10 04:30:25 +00:00
|
|
|
// Coarse grouping
|
2023-02-10 17:55:58 +00:00
|
|
|
$gridParam = $query->createNamedParameter($gridLen, IQueryBuilder::PARAM_STR);
|
2023-08-17 17:48:43 +00:00
|
|
|
$query->addGroupBy($query->createFunction("FLOOR(c.lat / {$gridParam})"))
|
|
|
|
->addGroupBy($query->createFunction("FLOOR(c.lon / {$gridParam})"))
|
|
|
|
;
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
// JOIN with memories for files from the current user
|
2023-02-09 08:35:35 +00:00
|
|
|
$query->innerJoin('c', 'memories', 'm', $query->expr()->eq('c.id', 'm.mapcluster'));
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-08 21:53:38 +00:00
|
|
|
// JOIN with filecache for existing files
|
2023-03-23 21:45:56 +00:00
|
|
|
$query = $this->joinFilecache($query);
|
2023-02-08 21:53:38 +00:00
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
// Bound the query to the map bounds
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->transformMapBoundsFilter($query, false, $bounds, 'c');
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
// Execute query
|
2023-03-24 01:12:39 +00:00
|
|
|
$res = $this->executeQueryWithCTEs($query)->fetchAll();
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
// Post-process results
|
|
|
|
$clusters = [];
|
2023-02-09 05:55:12 +00:00
|
|
|
foreach ($res as &$cluster) {
|
|
|
|
$c = [
|
2023-02-08 21:53:38 +00:00
|
|
|
'center' => [
|
2023-02-09 05:55:12 +00:00
|
|
|
(float) $cluster['lat'],
|
|
|
|
(float) $cluster['lon'],
|
2023-02-08 21:53:38 +00:00
|
|
|
],
|
|
|
|
'count' => (float) $cluster['count'],
|
|
|
|
];
|
2023-02-09 05:55:12 +00:00
|
|
|
if (\array_key_exists('id', $cluster)) {
|
|
|
|
$c['id'] = (int) $cluster['id'];
|
|
|
|
}
|
|
|
|
$clusters[] = $c;
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $clusters;
|
|
|
|
}
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
/**
|
|
|
|
* Gets previews for a list of map clusters.
|
|
|
|
*
|
|
|
|
* @param int[] $clusterIds
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public function getMapClusterPreviews(array $clusterIds): array
|
2023-02-09 05:55:12 +00:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
// SELECT all photos with this tag
|
2023-08-17 17:48:43 +00:00
|
|
|
$query->selectAlias($query->createFunction('MAX(m.fileid)'), 'fileid')
|
|
|
|
->from('memories', 'm')
|
|
|
|
->where($query->expr()->in('m.mapcluster', $query->createNamedParameter(
|
|
|
|
$clusterIds,
|
2023-10-15 01:59:00 +00:00
|
|
|
IQueryBuilder::PARAM_INT_ARRAY,
|
2023-08-17 17:48:43 +00:00
|
|
|
)))
|
|
|
|
;
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
2023-03-23 21:45:56 +00:00
|
|
|
$query = $this->joinFilecache($query);
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
// GROUP BY the cluster
|
|
|
|
$query->groupBy('m.mapcluster');
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
// Get the fileIds
|
2023-02-09 05:55:12 +00:00
|
|
|
$cursor = $this->executeQueryWithCTEs($query);
|
2023-02-10 01:27:03 +00:00
|
|
|
$fileIds = $cursor->fetchAll(\PDO::FETCH_COLUMN);
|
|
|
|
|
|
|
|
// SELECT these files from the filecache
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-06-04 17:44:35 +00:00
|
|
|
$query->select('m.fileid', 'm.dayid', 'm.mapcluster', 'm.h', 'm.w', 'f.etag')
|
2023-02-10 01:27:03 +00:00
|
|
|
->from('memories', 'm')
|
|
|
|
->innerJoin('m', 'filecache', 'f', $query->expr()->eq('m.fileid', 'f.fileid'))
|
2023-02-10 01:27:18 +00:00
|
|
|
->where($query->expr()->in('m.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)))
|
|
|
|
;
|
2023-02-10 01:27:03 +00:00
|
|
|
$files = $query->executeQuery()->fetchAll();
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
// Post-process
|
2023-02-10 01:27:03 +00:00
|
|
|
foreach ($files as &$row) {
|
2023-02-09 05:55:12 +00:00
|
|
|
$row['fileid'] = (int) $row['fileid'];
|
2023-02-10 01:27:03 +00:00
|
|
|
$row['mapcluster'] = (int) $row['mapcluster'];
|
|
|
|
$row['dayid'] = (int) $row['dayid'];
|
2023-06-04 17:44:35 +00:00
|
|
|
$row['h'] = (int) $row['h'];
|
|
|
|
$row['w'] = (int) $row['w'];
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
return $files;
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|
2023-04-02 01:14:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the suggested initial coordinates for the map.
|
|
|
|
* Uses the coordinates of the newest photo (by date).
|
2023-10-15 19:46:35 +00:00
|
|
|
*
|
|
|
|
* @psalm-return array{lat: float, lon: float}|null
|
2023-04-02 01:14:37 +00:00
|
|
|
*/
|
|
|
|
public function getMapInitialPosition(): ?array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
// SELECT coordinates
|
|
|
|
$query->select('m.lat', 'm.lon')->from('memories', 'm');
|
|
|
|
|
|
|
|
// WHERE this photo is in the user's requested folder recursively
|
|
|
|
$query = $this->joinFilecache($query);
|
|
|
|
|
|
|
|
// WHERE this photo has coordinates
|
|
|
|
$query->where($query->expr()->andX(
|
|
|
|
$query->expr()->isNotNull('m.lat'),
|
2023-10-15 01:59:00 +00:00
|
|
|
$query->expr()->isNotNull('m.lon'),
|
2023-04-02 01:14:37 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// ORDER BY datetaken DESC
|
|
|
|
$query->orderBy('m.datetaken', 'DESC');
|
|
|
|
|
|
|
|
// LIMIT 1
|
|
|
|
$query->setMaxResults(1);
|
|
|
|
|
|
|
|
// FETCH coordinates
|
|
|
|
$coords = $this->executeQueryWithCTEs($query)->fetch();
|
|
|
|
|
|
|
|
return $coords ?: null;
|
|
|
|
}
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|