Kstars

import_skycomp.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Valentin Boettcher <hiro at protagon.space; @hiro98:tchncs.de>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "import_skycomp.h"
8 
9 QString db_path()
10 {
11  return QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation))
12  .filePath("skycomponents.sqlite");
13 }
14 
15 std::pair<bool, QSqlDatabase> SkyComponentsImport::get_skycomp_db(const QString &path)
16 {
17  if (!QFileInfo(path).exists())
18  return { false, {} };
19 
20  auto db = QSqlDatabase::addDatabase("QSQLITE");
21  db.setDatabaseName(path);
22 
23  if (!db.open())
24  return { false, {} };
25 
26  return { true, db };
27 }
28 
29 std::pair<bool, QSqlDatabase> SkyComponentsImport::get_skycomp_db()
30 {
31  return get_skycomp_db(db_path());
32 }
33 
34 std::tuple<bool, QString, CatalogsDB::CatalogObjectVector>
35 SkyComponentsImport::get_objects(QSqlDatabase db, const std::list<int> &ids)
36 {
37  QVector<QString> const placeholders(ids.size(), "?");
38  if (!db.open())
39  {
40  qDebug() << Q_FUNC_INFO << "here";
41  return { false, {}, {} };
42  }
43 
44  QSqlQuery query{ db };
45  query.prepare(
46  QString("SELECT UID, RA, Dec, Type, Magnitude, PositionAngle, MajorAxis, "
47  "MinorAxis, "
48  "Flux, LongName, id_Catalog FROM DSO INNER JOIN ObjectDesignation ON "
49  "DSO.UID "
50  "= "
51  "ObjectDesignation.UID_DSO WHERE id_Catalog IN (%1)")
52  .arg(QStringList::fromVector(placeholders).join(", ")));
53 
54  for (auto const &i : ids)
55  query.addBindValue(i);
56 
57  query.setForwardOnly(true);
58 
59  if (!query.exec())
60  return { false, query.lastError().text(), {} };
61 
62  CatalogsDB::CatalogObjectVector objs{};
63  while (query.next())
64  {
65  const SkyObject::TYPE type = static_cast<SkyObject::TYPE>(query.value(3).toInt());
66 
67  const double ra = query.value(1).toDouble();
68  const double dec = query.value(2).toDouble();
69  const float mag = query.isNull(4) ? NaN::f : query.value(4).toFloat();
70  const QString name = query.value(9).toString();
71  const QString long_name = "";
72  const QString catalog_identifier = "";
73  const float major = query.value(6).toFloat();
74  const float minor = query.value(7).toFloat();
75  const double position_angle = query.value(5).toDouble();
76  const float flux = query.value(8).toFloat();
77  const int catalog_id = 0;
78 
79  objs.emplace_back(CatalogObject::oid{}, type, dms(ra), dms(dec), mag, name,
80  long_name, catalog_identifier, catalog_id, major, minor,
81  position_angle, flux, "");
82  }
83 
84  db.close();
85  return { true, {}, objs };
86 };
QList< T > fromVector(const QVector< T > &vector)
std::tuple< bool, QString, CatalogsDB::CatalogObjectVector > get_objects(QSqlDatabase db, const std::list< int > &ids={ 1, 2 })
QSqlDatabase addDatabase(const QString &type, const QString &connectionName)
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:37
QString filePath(const QString &fileName) const const
std::pair< bool, QSqlDatabase > get_skycomp_db(const QString &path)
Get the skycomponent database from the specified path.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 04:06:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.