Calculate bounds on indexing

This commit is contained in:
2022-06-14 21:44:32 +02:00
parent 04e587b51c
commit 08e70cdb52
5 changed files with 83 additions and 25 deletions
+51 -3
View File
@@ -22,16 +22,32 @@ bool Database::init()
if(m_database.isValid())
{
m_database.setDatabaseName(dir.absoluteFilePath("database.db"));
m_database.setDatabaseName(dir.absoluteFilePath("database2.db"));
if(m_database.open())
{
m_database.exec("PRAGMA foreign_keys = ON");
int version = checkVersion();
if(version == 0)
{
m_database.exec("PRAGMA user_version = 1");
m_database.exec("CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE)");
m_database.exec("CREATE TABLE IF NOT EXISTS fits_files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE, mtime DATETIME)");
m_database.exec("CREATE TABLE IF NOT EXISTS fits_files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE, mtime DATETIME,"
" minRa REAL, maxRa REAL, minDec REAL, maxDec REAL)");
m_database.exec("CREATE TABLE IF NOT EXISTS fits_headers (id INTEGER PRIMARY KEY AUTOINCREMENT, id_file INTEGER,"
"key VARCHAR(81), value VARCHAR(81), comment VARCHAR(81), FOREIGN KEY(id_file) REFERENCES fits_files(id) ON DELETE CASCADE)");
m_database.exec("CREATE INDEX IF NOT EXISTS key_value ON fits_headers(key, value)");
m_database.exec("CREATE INDEX IF NOT EXISTS id_file ON fits_headers(id_file)");
m_database.exec("CREATE INDEX IF NOT EXISTS minRa_idx ON fits_files(minRa)");
m_database.exec("CREATE INDEX IF NOT EXISTS maxRa_idx ON fits_files(maxRa)");
m_database.exec("CREATE INDEX IF NOT EXISTS minDec_idx ON fits_files(minDec)");
m_database.exec("CREATE INDEX IF NOT EXISTS maxDec_idx ON fits_files(maxDec)");
}
else if(version > 1)
{
qDebug() << "Database version is too new";
return false;
}
QSqlError error = m_database.lastError();
if(error.type() == QSqlError::NoError)
@@ -45,6 +61,8 @@ bool Database::init()
m_insertFile = QSqlQuery(m_database);
m_insertFile.prepare("INSERT INTO fits_files (file, mtime) VALUES (?, ?)");
m_insertFileWcs = QSqlQuery(m_database);
m_insertFileWcs.prepare("INSERT INTO fits_files (file, mtime, minRa, maxRa, minDec, maxDec) VALUES (?, ?, ?, ?, ?, ?)");
m_insertFitsHeader = QSqlQuery(m_database);
m_insertFitsHeader.prepare("INSERT INTO fits_headers (id_file, key, value, comment) VALUES (?, ?, ?, ?)");
m_checkFile = QSqlQuery(m_database);
@@ -55,7 +73,6 @@ bool Database::init()
m_deleteFile.prepare("DELETE FROM fits_files WHERE id=?");
return true;
}
qDebug() << error.text();
}
}
@@ -129,6 +146,15 @@ bool Database::checkError(QSqlQuery &query)
}
}
int Database::checkVersion()
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query = db.exec("PRAGMA user_version");
if(query.next())
return query.value(0).toInt();
return -1;
}
static QStringList nameFilters = {"*.fit", "*.fits", "*.xisf"};
static int countFiles(const QDir &dir, int count = 0)
@@ -241,6 +267,26 @@ bool Database::indexFile(const QFileInfo &file)
qlonglong last_id = -1;
if(ok)
{
if(info.wcs)
{
double minRa, maxRa, minDec, maxDec;
info.wcs->calculateBounds(minRa, maxRa, minDec, maxDec);
qDebug() << "bounds" << minRa << maxRa << minDec << maxDec;
m_insertFileWcs.bindValue(0, filePath);
m_insertFileWcs.bindValue(1, mtime);
m_insertFileWcs.bindValue(2, minRa);
m_insertFileWcs.bindValue(3, maxRa);
m_insertFileWcs.bindValue(4, minDec);
m_insertFileWcs.bindValue(5, maxDec);
if(!m_insertFileWcs.exec())
{
qDebug() << m_insertFileWcs.lastError();
return false;
}
last_id = m_insertFileWcs.lastInsertId().toLongLong();
}
else
{
m_insertFile.bindValue(0, filePath);
m_insertFile.bindValue(1, mtime);
@@ -250,6 +296,8 @@ bool Database::indexFile(const QFileInfo &file)
return false;
}
last_id = m_insertFile.lastInsertId().toLongLong();
}
QVariantList file_id, keys, values, comments;
for(auto &record : info.fitsHeader)
{
+2
View File
@@ -15,6 +15,7 @@ class Database : public QObject
QSqlQuery m_isMarkedQuery;
QSqlQuery m_insertFile;
QSqlQuery m_insertFileWcs;
QSqlQuery m_insertFitsHeader;
QSqlQuery m_checkFile;
QSqlQuery m_headerKeywords;
@@ -39,6 +40,7 @@ protected:
bool indexDir2(const QDir &dir, QProgressDialog *progress);
bool indexFile(const QFileInfo &file);
bool checkError(QSqlQuery &query);
int checkVersion();
};
#endif // DATABASE_H
+9 -7
View File
@@ -58,7 +58,9 @@ void WCSData::freeWCS()
wcs = nullptr;
}
WCSData::WCSData(char *header, int nrec)
WCSData::WCSData(int width, int height, char *header, int nrec) :
width(width),
height(height)
{
int stat[NWCSFIX];
int nreject = 0;
@@ -117,7 +119,7 @@ bool WCSData::worldToPixel(const SkyPoint &point, QPointF &pixel) const
return false;
}
void WCSData::calculateBounds(int w, int h, double &minRa, double &maxRa, double &minDec, double &maxDec)
void WCSData::calculateBounds(double &minRa, double &maxRa, double &minDec, double &maxDec) const
{
if(wcs == nullptr)return;
@@ -136,21 +138,21 @@ void WCSData::calculateBounds(int w, int h, double &minRa, double &maxRa, double
maxDec = std::max(maxDec, point.DEC());
};
for(int x=0; x<w; x++)
for(int x=0; x<width; x++)
{
update(QPointF(x, 0));
update(QPointF(x, h - 1));
update(QPointF(x, height - 1));
}
for(int y=0; y<h; y++)
for(int y=0; y<height; y++)
{
update(QPointF(0, y));
update(QPointF(w - 1, y));
update(QPointF(width - 1, y));
}
QPointF ncp;
QPointF scp;
QRectF s(0, 0, w - 1, h - 1);
QRectF s(0, 0, width - 1, height - 1);
if(worldToPixel(SkyPoint(0, 90), ncp))
{
if(s.contains(ncp))
+5 -3
View File
@@ -30,14 +30,16 @@ class WCSData
{
int nwcs = 0;
struct wcsprm *wcs = nullptr;
int width;
int height;
void freeWCS();
public:
WCSData();
WCSData(char *header, int nrec);
WCSData(int width, int height, char *header, int nrec);
WCSData(const WCSData &) = delete;
~WCSData();
bool pixelToWorld(const QPointF &pixel, SkyPoint &point) const;
bool worldToPixel(const SkyPoint &point, QPointF &pixel) const;
void calculateBounds(int w, int h, double &minRa, double &maxRa, double &minDec, double &maxDec);
void calculateBounds(double &minRa, double &maxRa, double &minDec, double &maxDec) const;
bool valid() const { return wcs; };
};
+5 -1
View File
@@ -132,6 +132,9 @@ bool loadRAW(const QString path, ImageInfoData &info, RawImage **image)
int loadFITSHeader(fitsfile *file, ImageInfoData &info)
{
int imgtype;
int naxis;
long naxes[3] = {0};
int nexist;
int status = 0;
char key[FLEN_KEYWORD];
@@ -139,6 +142,7 @@ int loadFITSHeader(fitsfile *file, ImageInfoData &info)
char comm[FLEN_COMMENT];
char strval[FLEN_VALUE];
QVariant var;
fits_get_img_param(file, 3, &imgtype, &naxis, naxes, &status);
fits_get_hdrspace(file, &nexist, nullptr, &status);
for(int i=1; i<=nexist; i++)
{
@@ -173,7 +177,7 @@ int loadFITSHeader(fitsfile *file, ImageInfoData &info)
fits_hdr2str(file, TRUE, nullptr, 0, &header, &nrec, &status);
if(status == 0)
{
info.wcs = std::make_shared<WCSData>(header, nrec);
info.wcs = std::make_shared<WCSData>(naxes[0], naxes[1], header, nrec);
if(!info.wcs->valid())info.wcs.reset();
}
fits_free_memory(header, &status);