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

273 lines
5.1 KiB
C++
Raw Normal View History

2015-10-05 01:43:48 +02:00
#include <QFile>
#include <QSet>
#include <QList>
2016-10-08 14:53:10 +02:00
#include "pathitem.h"
#include "waypointitem.h"
2015-10-05 01:43:48 +02:00
#include "ll.h"
#include "gpx.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;
_radius = 0.01;
}
2015-10-05 01:43:48 +02:00
bool POI::loadFile(const QString &fileName)
{
QString error;
int errorLine;
_error.clear();
_errorLine = 0;
2016-10-09 23:46:30 +02:00
if (loadCSVFile(fileName)) {
emit reloadRequired();
return true;
2016-10-09 23:46:30 +02:00
} else {
error = _error;
errorLine = _errorLine;
}
2016-10-09 23:46:30 +02:00
if (loadGPXFile(fileName)) {
emit reloadRequired();
return true;
2016-10-09 23:46:30 +02:00
}
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;
FileIndex index;
index.enabled = true;
index.start = _data.size();
if (gpx.loadFile(fileName)) {
for (int i = 0; i < gpx.waypoints().size(); i++)
2016-03-19 09:06:43 +01:00
_data.append(gpx.waypoints().at(i));
index.end = _data.size() - 1;
for (int i = index.start; i <= index.end; i++) {
const QPointF &p = _data.at(i).coordinates();
qreal c[2];
2016-03-19 09:06:43 +01:00
c[0] = p.x();
c[1] = p.y();
_tree.Insert(c, c, i);
}
_files.append(fileName);
_indexes.append(index);
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);
FileIndex index;
2015-10-05 01:43:48 +02:00
bool ret;
int ln = 1;
index.enabled = true;
index.start = _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;
}
Waypoint wp(QPointF(lon, lat));
2015-10-05 01:43:48 +02:00
QByteArray ba = list[2].trimmed();
2016-07-25 19:32:36 +02:00
QString name = QString::fromUtf8(ba.data(), ba.size());
wp.setName(name);
2016-07-25 19:32:36 +02:00
if (list.size() > 3) {
ba = list[3].trimmed();
wp.setDescription(QString::fromUtf8(ba.data(), ba.size()));
2016-07-25 19:32:36 +02:00
}
2015-10-05 01:43:48 +02:00
_data.append(wp);
2015-10-05 01:43:48 +02:00
ln++;
}
index.end = _data.size() - 1;
2015-10-05 01:43:48 +02:00
for (int i = index.start; i <= index.end; i++) {
const QPointF &p = _data.at(i).coordinates();
2015-10-05 01:43:48 +02:00
qreal c[2];
2016-03-19 09:06:43 +01:00
c[0] = p.x();
c[1] = p.y();
_tree.Insert(c, c, i);
2015-10-05 01:43:48 +02:00
}
_files.append(fileName);
_indexes.append(index);
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-10-09 23:46:30 +02:00
QVector<Waypoint> POI::points(const PathItem *path) const
{
QVector<Waypoint> ret;
QSet<int> set;
qreal min[2], max[2];
2016-10-08 14:53:10 +02:00
const QPainterPath &pp = path->path();
2016-10-08 14:53:10 +02:00
for (int i = 0; i < pp.elementCount(); i++) {
QPointF p = mercator2ll(pp.elementAt(i));
2016-10-09 23:46:30 +02:00
min[0] = p.x() - _radius;
min[1] = -p.y() - _radius;
max[0] = p.x() + _radius;
max[1] = -p.y() + _radius;
_tree.Search(min, max, cb, &set);
}
QSet<int>::const_iterator i = set.constBegin();
while (i != set.constEnd()) {
ret.append(_data.at(*i));
++i;
}
return ret;
}
2016-10-09 23:46:30 +02:00
QVector<Waypoint> POI::points(const QList<WaypointItem*> &list)
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 < list.count(); i++) {
const QPointF &p = list.at(i)->waypoint().coordinates();
2016-10-09 23:46:30 +02:00
min[0] = p.x() - _radius;
min[1] = p.y() - _radius;
max[0] = p.x() + _radius;
max[1] = p.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;
}
2016-10-09 23:46:30 +02:00
QVector<Waypoint> POI::points(const QList<Waypoint> &list) const
2016-10-08 14:53:10 +02:00
{
QVector<Waypoint> ret;
QSet<int> set;
qreal min[2], max[2];
for (int i = 0; i < list.count(); i++) {
const QPointF &p = list.at(i).coordinates();
2016-10-09 23:46:30 +02:00
min[0] = p.x() - _radius;
min[1] = p.y() - _radius;
max[0] = p.x() + _radius;
max[1] = p.y() + _radius;
2016-10-08 14:53:10 +02:00
_tree.Search(min, max, cb, &set);
}
QSet<int>::const_iterator i = set.constBegin();
while (i != set.constEnd()) {
ret.append(_data.at(*i));
++i;
}
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++) {
2016-03-19 09:06:43 +01:00
const QPointF &p = _data.at(j).coordinates();
qreal c[2];
2016-03-19 09:06:43 +01:00
c[0] = p.x();
c[1] = p.y();
_tree.Insert(c, c, j);
}
}
2016-10-09 23:46:30 +02:00
emit reloadRequired();
}
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
emit reloadRequired();
}
void POI::setRadius(qreal radius)
{
_radius = radius;
emit reloadRequired();
2015-10-05 01:43:48 +02:00
}