1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-19 20:12:10 +01:00
GPXSee/src/data/poi.cpp

183 lines
3.4 KiB
C++
Raw Normal View History

2015-10-05 01:43:48 +02:00
#include <QFile>
#include <QDir>
#include "common/rectc.h"
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
}
bool POI::loadFile(const QString &path, bool dir)
{
2016-10-23 11:09:20 +02:00
Data data;
FileIndex index;
index.enabled = true;
index.start = _data.size();
2015-10-05 01:43:48 +02:00
if (!data.loadFile(path)) {
if (dir) {
if (data.errorLine())
_errorString += QString("%1:%2: %3\n").arg(path)
.arg(data.errorLine()).arg(data.errorString());
else
_errorString += path + ": " + data.errorString() + "\n";
} else {
_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));
index.end = _data.size() - 1;
2015-10-05 01:43:48 +02:00
for (int i = index.start; i <= index.end; i++) {
const Coordinates &p = _data.at(i).coordinates();
2015-10-05 01:43:48 +02:00
qreal c[2];
c[0] = p.lon();
c[1] = p.lat();
_tree.Insert(c, c, i);
2015-10-05 01:43:48 +02:00
}
_files.append(path);
_indexes.append(index);
2016-10-23 11:09:20 +02:00
emit pointsChanged();
2015-10-05 01:43:48 +02:00
return true;
}
bool POI::loadFile(const QString &path)
{
_errorString.clear();
_errorLine = 0;
return loadFile(path, false);
}
bool POI::loadDir(const QString &path)
{
QDir md(path);
md.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList fl = md.entryInfoList();
bool ret = true;
_errorString.clear();
_errorLine = 0;
for (int i = 0; i < fl.size(); i++) {
const QFileInfo &fi = fl.at(i);
if (fi.isDir()) {
if (!loadDir(fi.absoluteFilePath()))
ret = false;
} else {
if (!loadFile(fi.absoluteFilePath(), true))
ret = false;
}
}
return ret;
}
static bool cb(size_t data, void* context)
2015-10-05 01:43:48 +02: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
{
2017-11-26 18:54:03 +01:00
QList<Waypoint> ret;
QSet<int> set;
qreal min[2], max[2];
2017-11-26 18:54:03 +01:00
QSet<int>::const_iterator it;
for (int i = 0; i < path.count(); i++) {
RectC br(path.at(i).coordinates(), _radius);
min[0] = br.topLeft().lon();
min[1] = br.bottomRight().lat();
max[0] = br.bottomRight().lon();
max[1] = br.topLeft().lat();
2016-12-06 01:48:26 +01: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
RectC br(point.coordinates(), _radius);
min[0] = br.topLeft().lon();
min[1] = br.bottomRight().lat();
max[0] = br.bottomRight().lon();
max[1] = br.topLeft().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;
}
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++) {
const Coordinates &p = _data.at(j).coordinates();
qreal c[2];
c[0] = p.lon();
c[1] = p.lat();
_tree.Insert(c, c, j);
}
}
2016-10-09 23:46:30 +02:00
2016-10-11 00:19:42 +02:00
emit pointsChanged();
}
2015-10-05 01:43:48 +02:00
void POI::clear()
{
_tree.RemoveAll();
_data.clear();
_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
}