memories/lib/Db/TimelineQueryMap.php

163 lines
5.4 KiB
PHP
Raw Permalink Normal View History

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
{
use TimelineQueryDays;
2023-02-09 01:45:55 +00:00
protected IDBConnection $connection;
public function transformMapBoundsFilter(IQueryBuilder &$query, bool $aggregate, string $bounds, string $table = 'm')
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)),
$query->expr()->lte($lonCol, $query->createNamedParameter($bounds[3], IQueryBuilder::PARAM_STR))
2023-02-08 21:53:38 +00:00
)
);
}
public function getMapClusters(
float $gridLen,
string $bounds
2023-02-08 21:53:38 +00:00
): array {
$query = $this->connection->getQueryBuilder();
// Get the average location of each cluster
$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);
$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
$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
$this->transformMapBoundsFilter($query, false, $bounds, 'c');
2023-02-08 21:53:38 +00:00
// Execute query
$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
public function getMapClusterPreviews(array $clusterIds)
2023-02-09 05:55:12 +00:00
{
$query = $this->connection->getQueryBuilder();
// SELECT all photos with this tag
$query->selectAlias($query->createFunction('MAX(m.fileid)'), 'fileid')
->from('memories', 'm')
->where($query->expr()->in('m.mapcluster', $query->createNamedParameter(
$clusterIds,
IQueryBuilder::PARAM_INT_ARRAY
)))
;
2023-02-09 05:55:12 +00:00
// WHERE these photos are in the user's requested folder recursively
$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();
$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'];
$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
}
/**
* Gets the suggested initial coordinates for the map.
* Uses the coordinates of the newest photo (by date).
*/
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'),
$query->expr()->isNotNull('m.lon')
));
// 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
}