1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/poi.cpp

151 lines
2.9 KiB
C++
Raw Normal View History

2015-10-05 01:43:48 +02:00
#include <QFile>
#include <QSet>
#include <QList>
#include "ll.h"
#include "gpx.h"
2015-10-05 01:43:48 +02:00
#include "poi.h"
bool POI::loadFile(const QString &fileName)
{
QString error;
int errorLine;
_error.clear();
_errorLine = 0;
if (loadCSVFile(fileName))
return true;
else {
error = _error;
errorLine = _errorLine;
}
if (loadGPXFile(fileName))
return true;
fprintf(stderr, "Error loading POI file: %s:\n", qPrintable(fileName));
fprintf(stderr, "CSV: line %d: %s\n", errorLine, qPrintable(error));
fprintf(stderr, "GPX: line %d: %s\n", _errorLine, qPrintable(_error));
if (errorLine > _errorLine) {
_errorLine = errorLine;
_error = error;
}
return false;
}
bool POI::loadGPXFile(const QString &fileName)
{
GPX gpx;
int cnt = _data.size();
if (gpx.loadFile(fileName)) {
for (int i = 0; i < gpx.waypoints().size(); i++)
2016-02-19 21:42:54 +01:00
_data.append(Waypoint(
ll2mercator(gpx.waypoints().at(i).coordinates()),
gpx.waypoints().at(i).description()));
for (int i = cnt; i < _data.size(); ++i) {
qreal c[2];
c[0] = _data.at(i).coordinates().x();
c[1] = _data.at(i).coordinates().y();
_tree.Insert(c, c, i);
}
return true;
} else {
_error = gpx.errorString();
_errorLine = gpx.errorLine();
}
return false;
}
bool POI::loadCSVFile(const QString &fileName)
2015-10-05 01:43:48 +02:00
{
QFile file(fileName);
bool ret;
int ln = 1, cnt = _data.size();
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) {
_error = "Parse error";
_errorLine = ln;
2015-10-05 01:43:48 +02:00
return false;
}
qreal lat = list[0].trimmed().toDouble(&ret);
if (!ret) {
_error = "Invalid latitude";
_errorLine = ln;
2015-10-05 01:43:48 +02:00
return false;
}
qreal lon = list[1].trimmed().toDouble(&ret);
if (!ret) {
_error = "Invalid longitude";
_errorLine = ln;
2015-10-05 01:43:48 +02:00
return false;
}
QByteArray ba = list[2].trimmed();
2016-02-19 21:42:54 +01:00
_data.append(Waypoint(ll2mercator(QPointF(lon, lat)),
QString::fromUtf8(ba.data(), ba.size())));
2015-10-05 01:43:48 +02:00
ln++;
}
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();
_tree.Insert(c, c, i);
2015-10-05 01:43:48 +02:00
}
return true;
}
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;
}
2016-02-19 21:42:54 +01:00
QVector<Waypoint> POI::points(const QVector<QPointF> &path, qreal radius) const
2015-10-05 01:43:48 +02:00
{
2016-02-19 21:42:54 +01:00
QVector<Waypoint> ret;
QSet<int> set;
2015-10-05 01:43:48 +02:00
qreal min[2], max[2];
for (int i = 0; i < path.count(); i++) {
2016-02-19 21:34:55 +01:00
min[0] = path.at(i).x() - radius;
min[1] = path.at(i).y() - radius;
max[0] = path.at(i).x() + radius;
max[1] = path.at(i).y() + radius;
2015-10-05 01:43:48 +02:00
_tree.Search(min, max, cb, &set);
}
QSet<int>::const_iterator i = set.constBegin();
2015-10-05 01:43:48 +02:00
while (i != set.constEnd()) {
ret.append(_data.at(*i));
++i;
2015-10-05 01:43:48 +02:00
}
return ret;
}
void POI::clear()
{
_tree.RemoveAll();
_data.clear();
}