2015-10-05 01:43:48 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QList>
|
|
|
|
#include "ll.h"
|
|
|
|
#include "poi.h"
|
|
|
|
|
|
|
|
|
2015-11-25 23:58:46 +01:00
|
|
|
#define BOUNDING_RECT_SIZE 0.01
|
|
|
|
|
2015-10-05 01:43:48 +02:00
|
|
|
bool POI::loadFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
QFile file(fileName);
|
|
|
|
bool ret;
|
2015-11-26 19:13:59 +01:00
|
|
|
int ln = 1, cnt = _data.size();
|
2015-10-05 01:43:48 +02:00
|
|
|
|
|
|
|
_error.clear();
|
2015-11-26 19:13:59 +01:00
|
|
|
_errorLine = 0;
|
2015-10-05 01:43:48 +02:00
|
|
|
|
|
|
|
if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
|
|
|
_error = qPrintable(file.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
QList<QByteArray> list = line.split(',');
|
|
|
|
if (list.size() < 3) {
|
2015-11-26 19:13:59 +01:00
|
|
|
_error = "Parse error";
|
|
|
|
_errorLine = ln;
|
2015-10-05 01:43:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal lat = list[0].trimmed().toDouble(&ret);
|
|
|
|
if (!ret) {
|
2015-11-26 19:13:59 +01:00
|
|
|
_error = "Invalid latitude";
|
|
|
|
_errorLine = ln;
|
2015-10-05 01:43:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qreal lon = list[1].trimmed().toDouble(&ret);
|
|
|
|
if (!ret) {
|
2015-11-26 19:13:59 +01:00
|
|
|
_error = "Invalid longitude";
|
|
|
|
_errorLine = ln;
|
2015-10-05 01:43:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray ba = list[2].trimmed();
|
|
|
|
|
|
|
|
Entry entry;
|
|
|
|
entry.description = QString::fromUtf8(ba.data(), ba.size());
|
2015-11-23 02:37:08 +01:00
|
|
|
entry.coordinates = ll2mercator(QPointF(lon, lat));
|
2015-10-05 01:43:48 +02:00
|
|
|
|
|
|
|
_data.append(entry);
|
|
|
|
ln++;
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:13:59 +01:00
|
|
|
for (int i = cnt; i < _data.size(); ++i) {
|
2015-10-05 01:43:48 +02:00
|
|
|
qreal c[2];
|
|
|
|
c[0] = _data.at(i).coordinates.x();
|
|
|
|
c[1] = _data.at(i).coordinates.y();
|
2015-11-26 19:13:59 +01:00
|
|
|
_tree.Insert(c, c, i);
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:13:59 +01:00
|
|
|
static bool cb(size_t data, void* context)
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
2015-11-26 19:13:59 +01:00
|
|
|
QSet<int> *set = (QSet<int>*) context;
|
|
|
|
set->insert((int)data);
|
2015-10-05 01:43:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<Entry> POI::points(const QVector<QPointF> &path) const
|
|
|
|
{
|
|
|
|
QVector<Entry> ret;
|
2015-11-26 19:13:59 +01:00
|
|
|
QSet<int> set;
|
2015-10-05 01:43:48 +02:00
|
|
|
qreal min[2], max[2];
|
|
|
|
|
|
|
|
for (int i = 0; i < path.count(); i++) {
|
2015-11-25 23:58:46 +01:00
|
|
|
min[0] = path.at(i).x() - BOUNDING_RECT_SIZE;
|
|
|
|
min[1] = path.at(i).y() - BOUNDING_RECT_SIZE;
|
|
|
|
max[0] = path.at(i).x() + BOUNDING_RECT_SIZE;
|
|
|
|
max[1] = path.at(i).y() + BOUNDING_RECT_SIZE;
|
2015-10-05 01:43:48 +02:00
|
|
|
_tree.Search(min, max, cb, &set);
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:13:59 +01:00
|
|
|
QSet<int>::const_iterator i = set.constBegin();
|
2015-10-05 01:43:48 +02:00
|
|
|
while (i != set.constEnd()) {
|
2015-11-26 19:13:59 +01:00
|
|
|
ret.append(_data.at(*i));
|
|
|
|
++i;
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void POI::clear()
|
|
|
|
{
|
|
|
|
_tree.RemoveAll();
|
|
|
|
_data.clear();
|
|
|
|
}
|