2015-10-05 01:43:48 +02:00
|
|
|
#include <QFile>
|
2016-10-23 11:09:20 +02:00
|
|
|
#include "data.h"
|
2015-10-05 01:43:48 +02:00
|
|
|
#include "poi.h"
|
|
|
|
|
|
|
|
|
2016-10-09 23:46:30 +02:00
|
|
|
POI::POI(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
_errorLine = 0;
|
2016-12-06 01:48:26 +01:00
|
|
|
_radius = 1000;
|
2016-10-09 23:46:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-05 01:43:48 +02:00
|
|
|
bool POI::loadFile(const QString &fileName)
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
2016-10-23 11:09:20 +02:00
|
|
|
Data data;
|
2016-03-05 18:01:13 +01:00
|
|
|
FileIndex index;
|
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
_errorString.clear();
|
|
|
|
_errorLine = 0;
|
2016-03-05 18:01:13 +01:00
|
|
|
|
|
|
|
index.enabled = true;
|
|
|
|
index.start = _data.size();
|
2015-10-05 01:43:48 +02:00
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
if (!data.loadFile(fileName)) {
|
|
|
|
_errorString = data.errorString();
|
|
|
|
_errorLine = data.errorLine();
|
2015-10-05 01:43:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
for (int i = 0; i < data.waypoints().size(); i++)
|
|
|
|
_data.append(data.waypoints().at(i));
|
2016-03-05 18:01:13 +01:00
|
|
|
index.end = _data.size() - 1;
|
2015-10-05 01:43:48 +02:00
|
|
|
|
2016-03-05 18:01:13 +01:00
|
|
|
for (int i = index.start; i <= index.end; i++) {
|
2016-10-24 00:21:40 +02:00
|
|
|
const Coordinates &p = _data.at(i).coordinates();
|
2015-10-05 01:43:48 +02:00
|
|
|
qreal c[2];
|
2016-10-24 00:21:40 +02:00
|
|
|
c[0] = p.lon();
|
|
|
|
c[1] = p.lat();
|
2015-11-26 19:13:59 +01:00
|
|
|
_tree.Insert(c, c, i);
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
2016-03-05 18:01:13 +01:00
|
|
|
_files.append(fileName);
|
|
|
|
_indexes.append(index);
|
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
emit pointsChanged();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
QList<Waypoint> POI::points(const Path &path) const
|
2016-07-28 00:23:22 +02:00
|
|
|
{
|
2017-11-26 18:54:03 +01:00
|
|
|
QList<Waypoint> ret;
|
2016-07-28 00:23:22 +02:00
|
|
|
QSet<int> set;
|
|
|
|
qreal min[2], max[2];
|
2017-11-26 18:54:03 +01:00
|
|
|
QSet<int>::const_iterator it;
|
2016-07-28 00:23:22 +02:00
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
for (int i = 0; i < path.count(); i++) {
|
|
|
|
const Coordinates &c = path.at(i).coordinates();
|
|
|
|
QPair<Coordinates, Coordinates> br = c.boundingRect(_radius);
|
2016-12-06 01:48:26 +01:00
|
|
|
min[0] = br.first.lon();
|
|
|
|
min[1] = br.first.lat();
|
|
|
|
max[0] = br.second.lon();
|
|
|
|
max[1] = br.second.lat();
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
_tree.Search(min, max, cb, &set);
|
|
|
|
}
|
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
for (it = set.constBegin(); it != set.constEnd(); ++it)
|
|
|
|
ret.append(_data.at(*it));
|
2015-10-05 01:43:48 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
QList<Waypoint> POI::points(const Waypoint &point) const
|
2016-10-08 14:53:10 +02:00
|
|
|
{
|
2017-11-26 18:54:03 +01:00
|
|
|
QList<Waypoint> ret;
|
2016-10-08 14:53:10 +02:00
|
|
|
QSet<int> set;
|
|
|
|
qreal min[2], max[2];
|
2017-11-26 18:54:03 +01:00
|
|
|
QSet<int>::const_iterator it;
|
2016-10-08 14:53:10 +02:00
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
const Coordinates &c = point.coordinates();
|
2016-12-06 01:48:26 +01:00
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
QPair<Coordinates, Coordinates> br = c.boundingRect(_radius);
|
|
|
|
min[0] = br.first.lon();
|
|
|
|
min[1] = br.first.lat();
|
|
|
|
max[0] = br.second.lon();
|
|
|
|
max[1] = br.second.lat();
|
2016-12-06 01:48:26 +01:00
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
_tree.Search(min, max, cb, &set);
|
2016-10-08 14:53:10 +02:00
|
|
|
|
2017-11-26 18:54:03 +01:00
|
|
|
for (it = set.constBegin(); it != set.constEnd(); ++it)
|
|
|
|
ret.append(_data.at(*it));
|
2016-10-08 14:53:10 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-05 18:01:13 +01:00
|
|
|
void POI::enableFile(const QString &fileName, bool enable)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = _files.indexOf(fileName);
|
|
|
|
Q_ASSERT(i >= 0);
|
|
|
|
_indexes[i].enabled = enable;
|
|
|
|
|
|
|
|
_tree.RemoveAll();
|
|
|
|
for (int i = 0; i < _indexes.count(); i++) {
|
|
|
|
FileIndex idx = _indexes.at(i);
|
|
|
|
if (!idx.enabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int j = idx.start; j <= idx.end; j++) {
|
2016-10-24 00:21:40 +02:00
|
|
|
const Coordinates &p = _data.at(j).coordinates();
|
2016-03-05 18:01:13 +01:00
|
|
|
qreal c[2];
|
2016-10-24 00:21:40 +02:00
|
|
|
c[0] = p.lon();
|
|
|
|
c[1] = p.lat();
|
2016-03-05 18:01:13 +01:00
|
|
|
_tree.Insert(c, c, j);
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 23:46:30 +02:00
|
|
|
|
2016-10-11 00:19:42 +02:00
|
|
|
emit pointsChanged();
|
2016-03-05 18:01:13 +01:00
|
|
|
}
|
|
|
|
|
2015-10-05 01:43:48 +02:00
|
|
|
void POI::clear()
|
|
|
|
{
|
|
|
|
_tree.RemoveAll();
|
|
|
|
_data.clear();
|
2016-03-05 18:01:13 +01:00
|
|
|
_files.clear();
|
|
|
|
_indexes.clear();
|
2016-10-09 23:46:30 +02:00
|
|
|
|
2016-10-11 00:19:42 +02:00
|
|
|
emit pointsChanged();
|
2016-10-09 23:46:30 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:48:26 +01:00
|
|
|
void POI::setRadius(unsigned radius)
|
2016-10-09 23:46:30 +02:00
|
|
|
{
|
|
|
|
_radius = radius;
|
|
|
|
|
2016-10-11 00:19:42 +02:00
|
|
|
emit pointsChanged();
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|