Skip to content

Commit

Permalink
places: switch geometry (#1067)
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <[email protected]>
  • Loading branch information
pulsejet committed Mar 11, 2024
1 parent 2b2d781 commit 00f1a97
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]


- **Important**: It is recommended to run `occ memories:places-setup` again if you use MySQL or MariaDB
- **Important**: You must run `occ memories:places-setup` again after this update
- Hide files starting with `.` in the timeline
- Prevent automatically retrying failed indexing jobs
- Support for 3GP videos ([#1055](https://github.com/pulsejet/memories/issues/1055))
Expand Down
2 changes: 2 additions & 0 deletions lib/Db/TimelineWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OCP\Files\File;
use OCP\IDBConnection;
use OCP\Lock\ILockingProvider;
use Psr\Log\LoggerInterface;

const DELETE_TABLES = ['memories', 'memories_livephoto', 'memories_places', 'memories_failures'];
const TRUNCATE_TABLES = ['memories_mapclusters'];
Expand All @@ -25,6 +26,7 @@ public function __construct(
protected IDBConnection $connection,
protected LivePhoto $livePhoto,
protected ILockingProvider $lockingProvider,
protected LoggerInterface $logger,
) {}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/Db/TimelineWritePlaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
trait TimelineWritePlaces
{
protected IDBConnection $connection;
protected LoggerInterface $logger;

/**
* Add places data for a file.
Expand Down Expand Up @@ -48,7 +49,15 @@ public function updatePlacesData(int $fileId, ?float $lat, ?float $lon): array
}

// Get places
$rows = \OC::$server->get(\OCA\Memories\Service\Places::class)->queryPoint($lat, $lon);
try {
$rows = \OC::$server->get(\OCA\Memories\Service\Places::class)
->queryPoint($lat, $lon)
;
} catch (\Exception $e) {
$this->logger->error("Error querying places: {$e->getMessage()}", ['app' => 'memories']);

return [];
}

// Get last ID, i.e. the ID with highest admin_level but <= 8
$crows = array_filter($rows, static fn ($row) => $row['admin_level'] <= 8);
Expand Down
5 changes: 5 additions & 0 deletions lib/Migration/Version602003Date20240310203729.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
$table->addIndex(['fileid', 'mtime'], 'memories_fail_fid_mt_idx');
}

// This version changes the geometry of planet
\OC::$server->get(\OCA\Memories\Settings\SystemConfig::class)
->set('memories.gis_type', -1)
;

return $schema;
}

Expand Down
26 changes: 17 additions & 9 deletions lib/Service/Places.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public function queryPoint(float $lat, float $lon): array
// Construct WHERE clause depending on GIS type
$where = null;
if (GIS_TYPE_MYSQL === $gisType) {
$where = "ST_Contains(geometry, ST_GeomFromText('POINT({$lon} {$lat})', 4326))";
$where = "ST_Contains(geometry, ST_GeomFromText('POINT({$lat} {$lon})', 4326))";
} elseif (GIS_TYPE_POSTGRES === $gisType) {
$where = "POINT('{$lon},{$lat}') <@ geometry";
$where = "POINT('{$lat},{$lon}') <@ geometry";
} else {
return [];
}
Expand Down Expand Up @@ -325,27 +325,35 @@ public function importPlanet(string $datafile): void
++$idx;
$geometry = '';

// Every polygon must have at least 3 points
if (\count($coords) < 3) {
echo "ERROR: Invalid polygon {$polyid}\n";

continue;
}

// Check if coordinates are valid
foreach ($coords as [$lon, $lat]) {
if ($lon < -180 || $lon > 180 || $lat < -90 || $lat > 90) {
echo "ERROR: Invalid coordinates for polygon {$polyid}\n";

continue 2;
}
}

if (GIS_TYPE_MYSQL === $gis) {
$points = implode(',', array_map(static function (array $point) {
$x = $point[0];
$y = $point[1];
[$lon, $lat] = $point;

return "{$x} {$y}";
return "{$lat} {$lon}";
}, $coords));

$geometry = "POLYGON(({$points}))";
} elseif (GIS_TYPE_POSTGRES === $gis) {
$geometry = implode(',', array_map(static function (array $point) {
$x = $point[0];
$y = $point[1];
[$lon, $lat] = $point;

return "({$x},{$y})";
return "({$lat},{$lon})";
}, $coords));
}

Expand Down Expand Up @@ -382,7 +390,7 @@ public function importPlanet(string $datafile): void
// Mark success
echo "Planet database imported successfully!\n";
flush();
$this->config->setSystemValue('memories.gis_type', $gis);
SystemConfig::set('memories.gis_type', $gis);

// Delete data file
@unlink($datafile);
Expand Down

0 comments on commit 00f1a97

Please sign in to comment.