MauiKit Image Tools

geolocation/citiesdb.cpp
1#include "citiesdb.h"
2
3#include "city.h"
4#include <QDebug>
5
6#include <QFile>
7#include <QDir>
8
9#include <QStandardPaths>
10#include <QUuid>
11
12#include <QSqlDriver>
13#include <QSqlError>
14#include <QSqlQuery>
15
16
17static QString resolveDBFile()
18{
19#if defined(Q_OS_ANDROID)
20
21 QFile file(QStandardPaths::locate(QStandardPaths::GenericDataLocation, "/org/mauikit/imagetools/cities.db"));
22 if(!file.exists())
23 {
24 if(QFile::copy(":/android_rcc_bundle/qml/org/mauikit/imagetools/cities.db", QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/cities.db"))
25 {
26 qDebug() << "Cities DB File was copied to";
27 }else
28 {
29 qDebug() << "Cities DB File was NOT copied to";
30
31 }
32 }
34#else
35 return QStandardPaths::locate(QStandardPaths::GenericDataLocation, "/org/mauikit/imagetools/cities.db");
36#endif
37}
38
39CitiesDB::CitiesDB(QObject * parent) : QObject(parent)
40{
41 QString DBFile = resolveDBFile();
42
43 if(QSqlDatabase::isDriverAvailable(QStringLiteral("QSQLITE")))
44 {
45 qDebug() << "opening Cities DB";
46 m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), QUuid::createUuid().toString());
47
48 m_db.setDatabaseName(DBFile);
49 qDebug() << "Cities DB NAME" << m_db.connectionName();
50 qDebug() << DBFile;
51
52 if(!m_db.open())
53 {
54 qWarning() << "Cities::DatabaseConnect - ERROR: " << m_db.lastError().text();
55 m_error = true;
56 }else
57 {
58 m_error = false;
59 }
60 }
61 else
62 {
63 qWarning() << "Cities::DatabaseConnect - ERROR: no driver " << QStringLiteral("QSQLITE") << " available";
64 m_error = true;
65 }
66}
67
68City CitiesDB::findCity(double latitude, double longitude)
69{
70 if(m_error)
71 {
72 return City();
73 }
74
75 QSqlQuery query(m_db);
76 query.prepare("SELECT * FROM CITIES where lat = ? and lon = ?");
77 query.addBindValue(latitude);
78 query.addBindValue(longitude);
79
80 if(!query.exec())
81 {
82 qWarning() << "Cities::FindCity - ERROR: " << query.lastError().text();
83 }
84
85 if(query.first())
86 {
87 return City(query.value("id").toString(), query.value("name").toString(), query.value("tz").toString(), query.value("country").toString(),query.value("lat").toDouble(),query.value("lon").toDouble());
88 }
89
90 qWarning() << "City not found";
91
92 return City();
93}
94
95City CitiesDB::city(const QString &cityId)
96{
97 if(m_error)
98 {
99 return City();
100 }
101
102 QSqlQuery query(m_db);
103 query.prepare("SELECT c.id, c.name, co.name as country, c.lat, c.lon, c.tz FROM CITIES c inner join COUNTRIES co on c.country = co.id where c.id = ?");
104 query.addBindValue(cityId);
105
106 if(!query.exec())
107 {
108 qWarning() << "Cities::city - ERROR: " << query.lastError().text();
109 }
110
111 if(query.first())
112 {
113 return City(query.value("id").toString(), query.value("name").toString(), query.value("tz").toString(), query.value("country").toString(),query.value("lat").toDouble(),query.value("lon").toDouble());
114 }
115
116 return City();
117}
118
119std::vector< point_t > CitiesDB::cities()
120{
121 std::vector< point_t > res;
122 QSqlQuery query(m_db);
123 query.prepare("SELECT lat, lon FROM CITIES");
124
125 if(!query.exec())
126 {
127 qWarning() << "Cities::ParsingCities - ERROR: " << query.lastError().text();
128 m_error = true;
129 }
130
131 while(query.next())
132 {
133 double lat = query.value("lat").toDouble();
134 double lon = query.value("lon").toDouble();
135 res.push_back({lat, lon});
136 }
137
138 m_error = false;
139 return res;
140}
141
142
143bool CitiesDB::error() const
144{
145 return m_error;
146}
147
A class for representing the GPS coordinates and information of a city.
char * toString(const EngineQuery &query)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
bool copy(const QString &fileName, const QString &newName)
T & first()
T value(qsizetype i) const const
QSqlDatabase addDatabase(QSqlDriver *driver, const QString &connectionName)
bool isDriverAvailable(const QString &name)
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QString writableLocation(StandardLocation type)
QUuid createUuid()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:32:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.