-
Notifications
You must be signed in to change notification settings - Fork 105
/
template_track.cpp
502 lines (424 loc) · 13.9 KB
/
template_track.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2013-2017 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "template_track.h"
#include <QCommandLinkButton>
#include <QMessageBox>
#include <QPainter>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include "core/georeferencing.h"
#include "core/map.h"
#include "core/objects/object.h"
#include "core/symbols/line_symbol.h"
#include "core/symbols/point_symbol.h"
#include "gui/georeferencing_dialog.h"
#include "gui/task_dialog.h"
#include "undo/object_undo.h"
#include "util/util.h"
namespace OpenOrienteering {
const std::vector<QByteArray>& TemplateTrack::supportedExtensions()
{
static std::vector<QByteArray> extensions = { "gpx" };
return extensions;
}
TemplateTrack::TemplateTrack(const QString& path, Map* map)
: Template(path, map)
{
// set default value
track_crs_spec = Georeferencing::geographic_crs_spec;
const Georeferencing& georef = map->getGeoreferencing();
connect(&georef, &Georeferencing::projectionChanged, this, &TemplateTrack::updateGeoreferencing);
connect(&georef, &Georeferencing::transformationChanged, this, &TemplateTrack::updateGeoreferencing);
connect(&georef, &Georeferencing::stateChanged, this, &TemplateTrack::updateGeoreferencing);
connect(&georef, &Georeferencing::declinationChanged, this, &TemplateTrack::updateGeoreferencing);
}
TemplateTrack::~TemplateTrack()
{
if (template_state == Loaded)
unloadTemplateFile();
}
void TemplateTrack::saveTypeSpecificTemplateConfiguration(QXmlStreamWriter& xml) const
{
if (preserved_georef)
{
// Preserve explicit georeferencing from OgrTemplate.
preserved_georef->save(xml);
return;
}
// Follow map georeferencing XML structure
xml.writeStartElement(QString::fromLatin1("crs_spec"));
xml.writeCharacters(track_crs_spec);
xml.writeEndElement(/*crs_spec*/);
if (!projected_crs_spec.isEmpty())
{
Q_ASSERT(!is_georeferenced);
xml.writeStartElement(QString::fromLatin1("projected_crs_spec"));
xml.writeCharacters(projected_crs_spec);
xml.writeEndElement(/*crs_spec*/);
}
}
bool TemplateTrack::loadTypeSpecificTemplateConfiguration(QXmlStreamReader& xml)
{
if (xml.name() == QLatin1String("crs_spec"))
{
track_crs_spec = xml.readElementText();
}
else if (xml.name() == QLatin1String("projected_crs_spec"))
{
Q_ASSERT(!is_georeferenced);
projected_crs_spec = xml.readElementText();
}
else if (xml.name() == QLatin1String("georeferencing"))
{
// Preserve explicit georeferencing from OgrTemplate.
preserved_georef.reset(new Georeferencing());
preserved_georef->load(xml, false);
}
else
{
xml.skipCurrentElement(); // unsupported
}
return true;
}
bool TemplateTrack::saveTemplateFile() const
{
return track.saveTo(template_path);
}
bool TemplateTrack::loadTemplateFileImpl(bool configuring)
{
if (preserved_georef)
{
setErrorString(tr("This template must be loaded with GDAL/OGR."));
return false;
}
if (!track_crs_spec.isEmpty() && track_crs_spec != Georeferencing::geographic_crs_spec)
{
setErrorString(tr("This template must be loaded with GDAL/OGR."));
return false;
}
if (!track.loadFrom(template_path, false))
return false;
if (!configuring)
{
if (!is_georeferenced)
{
if (projected_crs_spec.isEmpty())
projected_crs_spec = calculateLocalGeoreferencing();
applyProjectedCrsSpec();
}
else
{
projected_crs_spec.clear();
track.changeMapGeoreferencing(map->getGeoreferencing());
}
}
return true;
}
bool TemplateTrack::postLoadConfiguration(QWidget* dialog_parent, bool& /*out_center_in_view*/)
{
is_georeferenced = true;
// If the CRS is geographic, ask if track should be loaded using map georeferencing or ad-hoc georeferencing
/** \todo Remove historical scope */
{
TaskDialog georef_dialog(dialog_parent, tr("Opening track ..."),
tr("Load the track in georeferenced or non-georeferenced mode?"),
QDialogButtonBox::Abort);
QString georef_text = tr("Positions the track according to the map's georeferencing settings.");
if (!map->getGeoreferencing().isValid())
georef_text += QLatin1Char(' ') + tr("These are not configured yet, so they will be shown as the next step.");
QAbstractButton* georef_button = georef_dialog.addCommandButton(tr("Georeferenced"), georef_text);
QAbstractButton* non_georef_button = georef_dialog.addCommandButton(tr("Non-georeferenced"), tr("Projects the track using an orthographic projection with center at the track's coordinate average. Allows adjustment of the transformation and setting the map georeferencing using the adjusted track position."));
georef_dialog.exec();
if (georef_dialog.clickedButton() == georef_button)
is_georeferenced = true;
else if (georef_dialog.clickedButton() == non_georef_button)
is_georeferenced = false;
else // abort
return false;
}
// If the track is loaded as georeferenced and the transformation parameters
// were not set yet, it must be done now
if (is_georeferenced &&
(!map->getGeoreferencing().isValid() || map->getGeoreferencing().isLocal()))
{
// Set default for real world reference point as some average of the track coordinates
Georeferencing georef(map->getGeoreferencing());
georef.setGeographicRefPoint(track.calcAveragePosition());
// Show the parameter dialog
GeoreferencingDialog dialog(dialog_parent, map, &georef);
dialog.setKeepGeographicRefCoords();
if (dialog.exec() == QDialog::Rejected || map->getGeoreferencing().isLocal())
return false;
}
// If the track is loaded as not georeferenced,
// the map coords for the track coordinates have to be calculated
if (!is_georeferenced)
{
if (projected_crs_spec.isEmpty())
projected_crs_spec = calculateLocalGeoreferencing();
applyProjectedCrsSpec();
}
else
{
projected_crs_spec.clear();
track.changeMapGeoreferencing(map->getGeoreferencing());
}
return true;
}
void TemplateTrack::unloadTemplateFileImpl()
{
track.clear();
}
void TemplateTrack::drawTemplate(QPainter* painter, const QRectF& clip_rect, double scale, bool on_screen, float opacity) const
{
Q_UNUSED(clip_rect);
Q_UNUSED(scale);
painter->save();
painter->setOpacity(opacity);
drawTracks(painter, on_screen);
drawWaypoints(painter);
painter->restore();
}
void TemplateTrack::drawTracks(QPainter* painter, bool on_screen) const
{
painter->save();
if (!is_georeferenced)
applyTemplateTransform(painter);
// Tracks
QPen pen(qRgb(212, 0, 244));
if (on_screen)
pen.setCosmetic(true);
else
pen.setWidthF(0.1); // = 0.1 mm at 100%
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
// TODO: could speed that up by storing the template coords of the GPS points in a separate vector or caching the painter paths
for (int i = 0; i < track.getNumSegments(); ++i)
{
QPainterPath path;
int size = track.getSegmentPointCount(i);
for (int k = 0; k < size; ++k)
{
const TrackPoint& point = track.getSegmentPoint(i, k);
if (k > 0)
path.lineTo(point.map_coord.x(), point.map_coord.y());
else
path.moveTo(point.map_coord.x(), point.map_coord.y());
}
painter->drawPath(path);
}
painter->restore();
}
void TemplateTrack::drawWaypoints(QPainter* painter) const
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(qRgb(255, 0, 0)));
QFont font = painter->font();
font.setPixelSize(2); // 2 mm at 100%
painter->setFont(font);
int height = painter->fontMetrics().height();
int size = track.getNumWaypoints();
for (int i = 0; i < size; ++i)
{
const TrackPoint& point = track.getWaypoint(i);
const QString& point_name = track.getWaypointName(i);
double const radius = 0.25;
painter->drawEllipse(point.map_coord, radius, radius);
if (!point_name.isEmpty())
{
painter->setPen(qRgb(255, 0, 0));
int width = painter->fontMetrics().width(point_name);
painter->drawText(QRect(point.map_coord.x() - 0.5*width,
point.map_coord.y() - height,
width,
height),
Qt::AlignCenter,
point_name);
painter->setPen(Qt::NoPen);
}
}
painter->restore();
}
QRectF TemplateTrack::getTemplateExtent() const
{
// Infinite because the extent of the waypoint texts is unknown
return infiniteRectF();
}
QRectF TemplateTrack::calculateTemplateBoundingBox() const
{
QRectF bbox;
int size = track.getNumWaypoints();
for (int i = 0; i < size; ++i)
{
const TrackPoint& track_point = track.getWaypoint(i);
MapCoordF point = track_point.map_coord;
rectIncludeSafe(bbox, is_georeferenced ? point : templateToMap(point));
}
for (int i = 0; i < track.getNumSegments(); ++i)
{
size = track.getSegmentPointCount(i);
for (int k = 0; k < size; ++k)
{
const TrackPoint& track_point = track.getSegmentPoint(i, k);
MapCoordF point = track_point.map_coord;
rectIncludeSafe(bbox, is_georeferenced ? point : templateToMap(point));
}
}
return bbox;
}
int TemplateTrack::getTemplateBoundingBoxPixelBorder()
{
// As we don't estimate the extent of the widest waypoint text,
// return a "very big" number to cover everything
return 10e8;
}
bool TemplateTrack::hasAlpha() const
{
return false;
}
Template* TemplateTrack::duplicateImpl() const
{
TemplateTrack* copy = new TemplateTrack(template_path, map);
copy->track = track;
return copy;
}
PathObject* TemplateTrack::importPathStart()
{
PathObject* path = new PathObject();
path->setSymbol(map->getUndefinedLine(), true);
return path;
}
void TemplateTrack::importPathEnd(PathObject* path)
{
map->addObject(path);
map->addObjectToSelection(path, false);
}
PointObject* TemplateTrack::importWaypoint(const MapCoordF& position, const QString& name)
{
PointObject* point = new PointObject(map->getUndefinedPoint());
point->setPosition(position);
point->setTag(QStringLiteral("name"), name);
map->addObject(point);
map->addObjectToSelection(point, false);
return point;
}
bool TemplateTrack::import(QWidget* dialog_parent)
{
if (track.getNumWaypoints() == 0 && track.getNumSegments() == 0)
{
QMessageBox::critical(dialog_parent, tr("Error"), tr("The path is empty, there is nothing to import!"));
return false;
}
DeleteObjectsUndoStep* undo_step = new DeleteObjectsUndoStep(map);
MapPart* part = map->getCurrentPart();
std::vector< Object* > result;
// clazy:excludeall=reserve-candidates
map->clearObjectSelection(false);
if (track.getNumWaypoints() > 0)
{
int res = QMessageBox::question(dialog_parent, tr("Question"), tr("Should the waypoints be imported as a line going through all points?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (res == QMessageBox::No)
{
for (int i = 0; i < track.getNumWaypoints(); i++)
result.push_back(importWaypoint(templateToMap(track.getWaypoint(i).map_coord), track.getWaypointName(i)));
}
else
{
PathObject* path = importPathStart();
for (int i = 0; i < track.getNumWaypoints(); i++)
path->addCoordinate(MapCoord(templateToMap(track.getWaypoint(i).map_coord)));
importPathEnd(path);
path->setTag(QStringLiteral("name"), QString{});
result.push_back(path);
}
}
int skipped_paths = 0;
for (int i = 0; i < track.getNumSegments(); i++)
{
const int segment_size = track.getSegmentPointCount(i);
if (segment_size == 0)
{
++skipped_paths;
continue; // Don't create path without objects.
}
PathObject* path = importPathStart();
for (int j = 0; j < segment_size; j++)
{
const TrackPoint& track_point = track.getSegmentPoint(i, j);
auto coord = MapCoord { templateToMap(track_point.map_coord) };
path->addCoordinate(coord);
}
if (track.getSegmentPoint(i, 0).latlon == track.getSegmentPoint(i, segment_size-1).latlon)
{
path->closeAllParts();
}
importPathEnd(path);
result.push_back(path);
}
for (const auto& object : result) // keep as separate loop to get the correct (final) indices
undo_step->addObject(part->findObjectIndex(object));
map->setObjectsDirty();
map->push(undo_step);
map->emitSelectionChanged();
map->emitSelectionEdited(); // TODO: is this necessary here?
if (skipped_paths)
{
QMessageBox::information(
dialog_parent,
tr("Import problems"),
tr("%n path object(s) could not be imported (reason: missing coordinates).", "", skipped_paths) );
}
return true;
}
void TemplateTrack::configureForGPSTrack()
{
is_georeferenced = true;
track_crs_spec = Georeferencing::geographic_crs_spec;
projected_crs_spec.clear();
track.changeMapGeoreferencing(map->getGeoreferencing());
template_state = Template::Loaded;
}
void TemplateTrack::updateGeoreferencing()
{
if (is_georeferenced && template_state == Template::Loaded)
{
projected_crs_spec.clear();
track.changeMapGeoreferencing(map->getGeoreferencing());
map->updateAllMapWidgets();
}
}
QString TemplateTrack::calculateLocalGeoreferencing() const
{
LatLon proj_center = track.calcAveragePosition();
return QString::fromLatin1("+proj=ortho +datum=WGS84 +lat_0=%1 +lon_0=%2")
.arg(proj_center.latitude(), 0, 'f')
.arg(proj_center.longitude(), 0, 'f');
}
void TemplateTrack::applyProjectedCrsSpec()
{
Georeferencing georef;
georef.setScaleDenominator(int(map->getScaleDenominator()));
georef.setProjectedCRS(QString{}, projected_crs_spec);
georef.setProjectedRefPoint({});
track.changeMapGeoreferencing(georef);
}
} // namespace OpenOrienteering