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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "FileLoader.h"
// Qt includes
#include <QBuffer>
#include <QDataStream>
#include <QFile>
// Local includes
#include "GeoDataParser.h"
#include "GeoDataFolder.h"
#include "GeoDataGroundOverlay.h"
#include "GeoDataPlacemark.h"
#include "GeoDataData.h"
#include "GeoDataExtendedData.h"
#include "GeoDataStyle.h"
#include "GeoDataStyleMap.h"
#include "GeoDataPhotoOverlay.h"
#include "GeoDataPoint.h"
#include "GeoDataPolyStyle.h"
#include "GeoDataLineStyle.h"
#include "GeoDataPolygon.h"
#include "GeoDataRelation.h"
#include "GeoDataScreenOverlay.h"
#include "GeoDataTour.h"
#include "GeoDataTrack.h"
#include "MarbleDirs.h"
#include "MarbleModel.h"
#include "ParsingRunnerManager.h"
#include "digikam_debug.h"
namespace Marble
{
class Q_DECL_HIDDEN FileLoaderPrivate
{
public:
FileLoaderPrivate(FileLoader* parent, const PluginManager* pluginManager, bool recenter,
const QString& file, const QString& property, const GeoDataStyle::Ptr& style, DocumentRole role, int renderOrder) :
q(parent),
m_runner(pluginManager),
m_filepath(file),
m_property(property),
m_style(style),
m_styleMap(new GeoDataStyleMap),<--- Class 'FileLoaderPrivate' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'FileLoaderPrivate' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
m_document(nullptr),
m_renderOrder(renderOrder),
m_documentRole(role),
m_recenter(recenter)
{
if (m_style)
{
m_styleMap->setId(QStringLiteral("default-map"));
m_styleMap->insert(QStringLiteral("normal"), QLatin1Char('#') + m_style->id());
}
}
FileLoaderPrivate(FileLoader* parent, const PluginManager* pluginManager,<--- Member variable 'FileLoaderPrivate::m_renderOrder' is not initialized in the constructor. [+]Member variable 'FileLoaderPrivate::m_renderOrder' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
const QString& contents, const QString& file, DocumentRole role) :
q(parent),
m_runner(pluginManager),
m_filepath(file),
m_contents(contents),
m_styleMap(nullptr),
m_document(nullptr),
m_documentRole(role),
m_recenter(false)
{
}
~FileLoaderPrivate()
{
delete m_styleMap;
}
void createFilterProperties(GeoDataContainer* container);
static int cityPopIdx(qint64 population);
static int spacePopIdx(qint64 population);
static int areaPopIdx(qreal area);
void documentParsed(GeoDataDocument* doc, const QString& error);
public:
FileLoader* q = nullptr;
ParsingRunnerManager m_runner;
QString m_filepath;
QString m_contents;
QString m_property;
GeoDataStyle::Ptr m_style;
GeoDataStyleMap* m_styleMap = nullptr;
GeoDataDocument* m_document = nullptr;
QString m_error;
int m_renderOrder;
DocumentRole m_documentRole;
bool m_recenter;
};
FileLoader::FileLoader(QObject* parent, const PluginManager* pluginManager, bool recenter, const QString& file,
const QString& property, const GeoDataStyle::Ptr& style, DocumentRole role, int renderOrder)
: QThread(parent),
d(new FileLoaderPrivate(this, pluginManager, recenter, file, property, style, role, renderOrder))
{
}
FileLoader::FileLoader(QObject* parent, const PluginManager* pluginManager,
const QString& contents, const QString& file, DocumentRole role)
: QThread(parent),
d(new FileLoaderPrivate(this, pluginManager, contents, file, role))
{
}
FileLoader::~FileLoader()
{
delete d;
}
QString FileLoader::path() const
{
return d->m_filepath;
}
GeoDataDocument* FileLoader::document()
{
return d->m_document;
}
QString FileLoader::error() const
{
return d->m_error;
}
void FileLoader::run()
{
if (d->m_contents.isEmpty())
{
QString defaultSourceName;
qCDebug(DIGIKAM_MARBLE_LOG) << "starting parser for" << d->m_filepath;
QFileInfo fileinfo(d->m_filepath);
QString path = fileinfo.path();
if (path == QLatin1String("."))
{
path.clear();
}
QString name = fileinfo.completeBaseName();
QString suffix = fileinfo.suffix();
// determine source, cache names
if (fileinfo.isAbsolute())
{
// We got an _absolute_ path now: e.g. "/patrick.kml"
defaultSourceName = path + QLatin1Char('/') + name + QLatin1Char('.') + suffix;
}
else if (d->m_filepath.contains(QLatin1Char('/')))
{
// _relative_ path: "maps/mars/viking/patrick.kml"
defaultSourceName = MarbleDirs::path(path + QLatin1Char('/') + name + QLatin1Char('.') + suffix);
if (!QFile::exists(defaultSourceName))
{
defaultSourceName = MarbleDirs::path(path + QLatin1Char('/') + name + QLatin1String(".cache"));
}
}
else
{
// _standard_ shared placemarks: "placemarks/patrick.kml"
defaultSourceName = MarbleDirs::path(QLatin1String("placemarks/") + path + name + QLatin1Char('.') + suffix);
if (!QFile::exists(defaultSourceName))
{
defaultSourceName = MarbleDirs::path(QLatin1String("placemarks/") + path + name + QLatin1String(".cache"));
}
}
if (QFile::exists(defaultSourceName))
{
qCDebug(DIGIKAM_MARBLE_LOG) << "No recent Default Placemark Cache File available!";
// use runners: pnt, gpx, osm
connect(&d->m_runner, SIGNAL(parsingFinished(GeoDataDocument*, QString)),
this, SLOT(documentParsed(GeoDataDocument*, QString)));
d->m_runner.parseFile(defaultSourceName, d->m_documentRole);
}
else
{
qCDebug(DIGIKAM_MARBLE_LOG) << "No Default Placemark Source File for " << name;
}
// content is not empty, we load from data
}
else
{
// Read the KML Data
GeoDataParser parser(GeoData_KML);
QByteArray ba(d->m_contents.toUtf8());
QBuffer buffer(&ba);
buffer.open(QIODevice::ReadOnly);
if (!parser.read(&buffer))
{
qCWarning(DIGIKAM_MARBLE_LOG) << QString::fromUtf8("Could not import kml buffer!");
Q_EMIT loaderFinished(this);
return;
}
GeoDocument* document = parser.releaseDocument();
Q_ASSERT(document);
d->m_document = static_cast<GeoDataDocument*>(document);
d->m_document->setProperty(d->m_property);
d->m_document->setDocumentRole(d->m_documentRole);
d->createFilterProperties(d->m_document);
buffer.close();
qCDebug(DIGIKAM_MARBLE_LOG) << "newGeoDataDocumentAdded" << d->m_filepath;
Q_EMIT newGeoDataDocumentAdded(d->m_document);
Q_EMIT loaderFinished(this);
}
}
bool FileLoader::recenter() const
{
return d->m_recenter;
}
void FileLoaderPrivate::documentParsed(GeoDataDocument* doc, const QString& error)
{
m_error = error;
if (doc)
{
m_document = doc;
doc->setProperty(m_property);
if (m_style)
{
doc->addStyleMap(*m_styleMap);
doc->addStyle(m_style);
}
if (m_renderOrder != 0)
{
for (GeoDataPlacemark* placemark : doc->placemarkList())
{
if (GeoDataPolygon* polygon = geodata_cast<GeoDataPolygon>(placemark->geometry()))
{
polygon->setRenderOrder(m_renderOrder);
}
}
}
createFilterProperties(doc);
Q_EMIT q->newGeoDataDocumentAdded(m_document);
}
Q_EMIT q->loaderFinished(q);
}
void FileLoaderPrivate::createFilterProperties(GeoDataContainer* container)
{
const QString styleUrl = QLatin1Char('#') + m_styleMap->id();
QVector<GeoDataFeature*>::Iterator i = container->begin();
QVector<GeoDataFeature*>::Iterator const end = container->end();
for (; i != end; ++i)
{
if (auto child = dynamic_cast<GeoDataContainer*>(*i))
{
createFilterProperties(child);
}
else if (geodata_cast<GeoDataTour>(*i)
|| geodata_cast<GeoDataRelation>(*i)
|| geodata_cast<GeoDataGroundOverlay>(*i)
|| geodata_cast<GeoDataPhotoOverlay>(*i)
|| geodata_cast<GeoDataScreenOverlay>(*i))
{
/** @todo: How to handle this ? */
}
else if (auto placemark = geodata_cast<GeoDataPlacemark>(*i))
{
const QString placemarkRole = placemark->role();
Q_ASSERT(placemark->geometry());<--- Assert statement calls a function which may have desired side effects: 'geometry'. [+]Non-pure function: 'geometry' is called inside assert statement. Assert statements are removed from release builds so the code inside assert statement is not executed. If the code is needed also in release builds, this is a bug.
bool hasPopularity = false;
if (!geodata_cast<GeoDataTrack>(placemark->geometry()) &&
!geodata_cast<GeoDataPoint>(placemark->geometry())
&& m_documentRole == MapDocument
&& m_style)
{
placemark->setStyleUrl(styleUrl);
}
// Mountain (H), Volcano (V), Shipwreck (W)
if (placemarkRole == QLatin1String("H") ||
placemarkRole == QLatin1String("V") ||
placemarkRole == QLatin1String("W"))
{
qreal altitude = placemark->coordinate().altitude();
if (altitude != 0.0)
{
hasPopularity = true;
placemark->setPopularity((qint64)(altitude * 1000.0));
placemark->setZoomLevel(cityPopIdx(qAbs((qint64)(altitude * 1000.0))));
}
}
// Continent (K), Ocean (O), Nation (S)
else if (placemarkRole == QLatin1String("K") ||
placemarkRole == QLatin1String("O") ||
placemarkRole == QLatin1String("S"))
{
qreal area = placemark->area();
if (area >= 0.0)
{
hasPopularity = true;
// qCDebug(DIGIKAM_MARBLE_LOG) << placemark->name() << " " << (qint64)(area);
placemark->setPopularity((qint64)(area * 100));
placemark->setZoomLevel(areaPopIdx(area));
}
}
// Pole (P)
else if (placemarkRole == QLatin1String("P"))
{
placemark->setPopularity(1000000000);
placemark->setZoomLevel(1);
}
// Magnetic Pole (M)
else if (placemarkRole == QLatin1String("M"))
{
placemark->setPopularity(10000000);
placemark->setZoomLevel(3);
}
// MannedLandingSite (h)
else if (placemarkRole == QLatin1String("h"))
{
placemark->setPopularity(1000000000);
placemark->setZoomLevel(1);
}
// RoboticRover (r)
else if (placemarkRole == QLatin1String("r"))
{
placemark->setPopularity(10000000);
placemark->setZoomLevel(2);
}
// UnmannedSoftLandingSite (u)
else if (placemarkRole == QLatin1String("u"))
{
placemark->setPopularity(1000000);
placemark->setZoomLevel(3);
}
// UnmannedSoftLandingSite (i)
else if (placemarkRole == QLatin1String("i"))
{
placemark->setPopularity(1000000);
placemark->setZoomLevel(3);
}
// Space Terrain: Craters, Maria, Montes, Valleys, etc.
else if (placemarkRole == QLatin1String("m") ||
placemarkRole == QLatin1String("v") ||
placemarkRole == QLatin1String("o") ||
placemarkRole == QLatin1String("c") ||
placemarkRole == QLatin1String("a"))
{
qint64 diameter = placemark->population();
if (diameter >= 0)
{
hasPopularity = true;
placemark->setPopularity(diameter);
if (placemarkRole == QLatin1String("c"))
{
placemark->setZoomLevel(spacePopIdx(diameter));
if (placemark->name() == QLatin1String("Tycho") ||
placemark->name() == QLatin1String("Copernicus"))
{
placemark->setZoomLevel(1);
}
}
else
{
placemark->setZoomLevel(spacePopIdx(diameter));
}
if (placemarkRole == QLatin1String("a") && diameter == 0)
{
placemark->setPopularity(1000000000);
placemark->setZoomLevel(1);
}
}
}
else
{
qint64 population = placemark->population();
if (population >= 0)
{
hasPopularity = true;
placemark->setPopularity(population);
placemark->setZoomLevel(cityPopIdx(population));
}
}
// Then we set the visual category:
if (placemarkRole == QLatin1String("H"))
{
placemark->setVisualCategory(GeoDataPlacemark::Mountain);
}
else if (placemarkRole == QLatin1String("V"))
{
placemark->setVisualCategory(GeoDataPlacemark::Volcano);
}
else if (placemarkRole == QLatin1String("m"))
{
placemark->setVisualCategory(GeoDataPlacemark::Mons);
}
else if (placemarkRole == QLatin1String("v"))
{
placemark->setVisualCategory(GeoDataPlacemark::Valley);
}
else if (placemarkRole == QLatin1String("o"))
{
placemark->setVisualCategory(GeoDataPlacemark::OtherTerrain);
}
else if (placemarkRole == QLatin1String("c"))
{
placemark->setVisualCategory(GeoDataPlacemark::Crater);
}
else if (placemarkRole == QLatin1String("a"))
{
placemark->setVisualCategory(GeoDataPlacemark::Mare);
}
else if (placemarkRole == QLatin1String("P"))
{
placemark->setVisualCategory(GeoDataPlacemark::GeographicPole);
}
else if (placemarkRole == QLatin1String("M"))
{
placemark->setVisualCategory(GeoDataPlacemark::MagneticPole);
}
else if (placemarkRole == QLatin1String("W"))
{
placemark->setVisualCategory(GeoDataPlacemark::ShipWreck);
}
else if (placemarkRole == QLatin1String("F"))
{
placemark->setVisualCategory(GeoDataPlacemark::AirPort);
}
else if (placemarkRole == QLatin1String("A"))
{
placemark->setVisualCategory(GeoDataPlacemark::Observatory);
}
else if (placemarkRole == QLatin1String("K"))
{
placemark->setVisualCategory(GeoDataPlacemark::Continent);
}
else if (placemarkRole == QLatin1String("O"))
{
placemark->setVisualCategory(GeoDataPlacemark::Ocean);
}
else if (placemarkRole == QLatin1String("S"))
{
placemark->setVisualCategory(GeoDataPlacemark::Nation);
}
else if (placemarkRole == QLatin1String("PPL") ||
placemarkRole == QLatin1String("PPLF") ||
placemarkRole == QLatin1String("PPLG") ||
placemarkRole == QLatin1String("PPLL") ||
placemarkRole == QLatin1String("PPLQ") ||
placemarkRole == QLatin1String("PPLR") ||
placemarkRole == QLatin1String("PPLS") ||
placemarkRole == QLatin1String("PPLW"))
{
switch (placemark->zoomLevel())
{
case 3:
case 4:
placemark->setVisualCategory(GeoDataPlacemark::LargeCity);
break;
case 5:
case 6:
placemark->setVisualCategory(GeoDataPlacemark::BigCity);
break;
case 7:
case 8:
placemark->setVisualCategory(GeoDataPlacemark::MediumCity);
break;
default:
placemark->setVisualCategory(GeoDataPlacemark::SmallCity);
break;
}
}
else if (placemarkRole == QLatin1String("PPLA"))
{
switch (placemark->zoomLevel())
{
case 3:
case 4:
placemark->setVisualCategory(GeoDataPlacemark::LargeStateCapital);
break;
case 5:
case 6:
placemark->setVisualCategory(GeoDataPlacemark::BigStateCapital);
break;
case 7:
case 8:
placemark->setVisualCategory(GeoDataPlacemark::MediumStateCapital);
break;
default:
placemark->setVisualCategory(GeoDataPlacemark::SmallStateCapital);
break;
}
}
else if (placemarkRole == QLatin1String("PPLC"))
{
switch (placemark->zoomLevel())
{
case 3:
case 4:
placemark->setVisualCategory(GeoDataPlacemark::LargeNationCapital);
break;
case 5:
case 6:
placemark->setVisualCategory(GeoDataPlacemark::BigNationCapital);
break;
case 7:
case 8:
placemark->setVisualCategory(GeoDataPlacemark::MediumNationCapital);
break;
default:
placemark->setVisualCategory(GeoDataPlacemark::SmallNationCapital);
break;
}
}
else if (placemarkRole == QLatin1String("PPLA2") ||
placemarkRole == QLatin1String("PPLA3") ||
placemarkRole == QLatin1String("PPLA4"))
{
switch (placemark->zoomLevel())
{
case 3:
case 4:
placemark->setVisualCategory(GeoDataPlacemark::LargeCountyCapital);
break;
case 5:
case 6:
placemark->setVisualCategory(GeoDataPlacemark::BigCountyCapital);
break;
case 7:
case 8:
placemark->setVisualCategory(GeoDataPlacemark::MediumCountyCapital);
break;
default:
placemark->setVisualCategory(GeoDataPlacemark::SmallCountyCapital);
break;
}
}
else if (placemarkRole == QLatin1String(" ") && !hasPopularity && placemark->visualCategory() == GeoDataPlacemark::Unknown)
{
placemark->setVisualCategory(GeoDataPlacemark::Unknown); // default location
placemark->setZoomLevel(0);
}
else if (placemarkRole == QLatin1String("h"))
{
placemark->setVisualCategory(GeoDataPlacemark::MannedLandingSite);
}
else if (placemarkRole == QLatin1String("r"))
{
placemark->setVisualCategory(GeoDataPlacemark::RoboticRover);
}
else if (placemarkRole == QLatin1String("u"))
{
placemark->setVisualCategory(GeoDataPlacemark::UnmannedSoftLandingSite);
}
else if (placemarkRole == QLatin1String("i"))
{
placemark->setVisualCategory(GeoDataPlacemark::UnmannedHardLandingSite);
}
// At last fine-tune zoomlevel:
if (!placemark->isVisible())
{
placemark->setZoomLevel(18);
}
// Workaround: Emulate missing "setVisible" serialization by allowing for population
// values smaller than -1 which are considered invisible.
else if (placemark->population() < -1)
{
placemark->setZoomLevel(18);
}
else if (placemarkRole == QLatin1String("W"))
{
if (placemark->zoomLevel() < 4)
{
placemark->setZoomLevel(4);
}
}
else if (placemarkRole == QLatin1String("O"))
{
placemark->setZoomLevel(2);
}
else if (placemarkRole == QLatin1String("K"))
{
placemark->setZoomLevel(0);
}
}
else
{
qCWarning(DIGIKAM_MARBLE_LOG) << Q_FUNC_INFO << "Unknown feature" << (*i)->nodeType() << ". Skipping.";
}
}
}
int FileLoaderPrivate::cityPopIdx(qint64 population)
{
int popidx = 3;
if (population < 2500)
{
popidx = 10;
}
else if (population < 5000)
{
popidx = 9;
}
else if (population < 25000)
{
popidx = 8;
}
else if (population < 75000)
{
popidx = 7;
}
else if (population < 250000)
{
popidx = 6;
}
else if (population < 750000)
{
popidx = 5;
}
else if (population < 2500000)
{
popidx = 4;
}
return popidx;
}
int FileLoaderPrivate::spacePopIdx(qint64 population)
{
int popidx = 1;
if (population < 1000)
{
popidx = 10;
}
else if (population < 2000)
{
popidx = 9;
}
else if (population < 8000)
{
popidx = 8;
}
else if (population < 20000)
{
popidx = 7;
}
else if (population < 60000)
{
popidx = 6;
}
else if (population < 100000)
{
popidx = 5;
}
else if (population < 200000)
{
popidx = 4;
}
else if (population < 400000)
{
popidx = 2;
}
else if (population < 600000)
{
popidx = 1;
}
return popidx;
}
int FileLoaderPrivate::areaPopIdx(qreal area)
{
int popidx = 1;
if (area < 200000)
{
popidx = 5;
}
else if (area < 1000000)
{
popidx = 4;
}
else if (area < 2500000)
{
popidx = 3;
}
else if (area < 5000000)
{
popidx = 2;
}
return popidx;
}
} // namespace Marble
#include "moc_FileLoader.cpp"
|