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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 | /* ============================================================
*
* 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-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "GeoDataLatLonBox.h"
// Qt includes
#include <QDataStream>
// Local includes
#include "GeoDataLineString.h"
#include "GeoDataTypes.h"
#include "digikam_debug.h"
namespace Marble
{
const GeoDataLatLonBox GeoDataLatLonBox::empty = GeoDataLatLonBox();
class Q_DECL_HIDDEN GeoDataLatLonBoxPrivate
{
public:
GeoDataLatLonBoxPrivate()
: m_north(0.0),
m_south(0.0),
m_east(0.0),
m_west(0.0),
m_rotation(0.0)
{
}
qreal m_north;
qreal m_south;
qreal m_east;
qreal m_west;
qreal m_rotation; // NOT implemented yet!
};
bool operator==(GeoDataLatLonBox const& lhs, GeoDataLatLonBox const& rhs)
{
return lhs.d->m_west == rhs.d->m_west &&
lhs.d->m_east == rhs.d->m_east &&
lhs.d->m_north == rhs.d->m_north &&
lhs.d->m_south == rhs.d->m_south &&
lhs.d->m_rotation == rhs.d->m_rotation;
}
bool operator!=(GeoDataLatLonBox const& lhs, GeoDataLatLonBox const& rhs)
{
return !(lhs == rhs);
}
GeoDataLatLonBox::GeoDataLatLonBox()
: GeoDataObject(),
d(new GeoDataLatLonBoxPrivate)
{
}
GeoDataLatLonBox::GeoDataLatLonBox(qreal north, qreal south, qreal east, qreal west, GeoDataCoordinates::Unit unit)
: GeoDataObject(),
d(new GeoDataLatLonBoxPrivate)
{
setBoundaries(north, south, east, west, unit);
}
GeoDataLatLonBox::GeoDataLatLonBox(const GeoDataLatLonBox& other)
: GeoDataObject(other),
d(new GeoDataLatLonBoxPrivate(*other.d))
{
}
GeoDataLatLonBox::~GeoDataLatLonBox()
{
delete d;
}
const char* GeoDataLatLonBox::nodeType() const
{
return GeoDataTypes::GeoDataLatLonBoxType;
}
qreal GeoDataLatLonBox::north(GeoDataCoordinates::Unit unit) const
{
if (unit == GeoDataCoordinates::Degree)
{
return d->m_north * RAD2DEG;
}
return d->m_north;
}
void GeoDataLatLonBox::setNorth(const qreal north, GeoDataCoordinates::Unit unit)<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
d->m_north = GeoDataCoordinates::normalizeLat(north);
break;
case GeoDataCoordinates::Degree:
d->m_north = GeoDataCoordinates::normalizeLat(north * DEG2RAD);
break;
}
}
qreal GeoDataLatLonBox::south(GeoDataCoordinates::Unit unit) const
{
if (unit == GeoDataCoordinates::Degree)
{
return d->m_south * RAD2DEG;
}
return d->m_south;
}
void GeoDataLatLonBox::setSouth(const qreal south, GeoDataCoordinates::Unit unit)<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
d->m_south = GeoDataCoordinates::normalizeLat(south);
break;
case GeoDataCoordinates::Degree:
d->m_south = GeoDataCoordinates::normalizeLat(south * DEG2RAD);
break;
}
}
qreal GeoDataLatLonBox::east(GeoDataCoordinates::Unit unit) const
{
if (unit == GeoDataCoordinates::Degree)
{
return d->m_east * RAD2DEG;
}
return d->m_east;
}
void GeoDataLatLonBox::setEast(const qreal east, GeoDataCoordinates::Unit unit)<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
d->m_east = GeoDataCoordinates::normalizeLon(east);
break;
case GeoDataCoordinates::Degree:
d->m_east = GeoDataCoordinates::normalizeLon(east * DEG2RAD);
break;
}
}
qreal GeoDataLatLonBox::west(GeoDataCoordinates::Unit unit) const
{
if (unit == GeoDataCoordinates::Degree)
{
return d->m_west * RAD2DEG;
}
return d->m_west;
}
void GeoDataLatLonBox::setWest(const qreal west, GeoDataCoordinates::Unit unit)<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
d->m_west = GeoDataCoordinates::normalizeLon(west);
break;
case GeoDataCoordinates::Degree:
d->m_west = GeoDataCoordinates::normalizeLon(west * DEG2RAD);
break;
}
}
void GeoDataLatLonBox::setRotation(const qreal rotation, GeoDataCoordinates::Unit unit)<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
d->m_rotation = rotation;
break;
case GeoDataCoordinates::Degree:
d->m_rotation = rotation * DEG2RAD;
break;
}
}
qreal GeoDataLatLonBox::rotation(GeoDataCoordinates::Unit unit) const
{
if (unit == GeoDataCoordinates::Degree)
{
return d->m_rotation * RAD2DEG;
}
return d->m_rotation;
}
void GeoDataLatLonBox::boundaries(qreal& north, qreal& south, qreal& east, qreal& west, GeoDataCoordinates::Unit unit) const<--- Shadow argument<--- Shadow argument<--- Shadow argument<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
north = d->m_north;
south = d->m_south;
east = d->m_east;
west = d->m_west;
break;
case GeoDataCoordinates::Degree:
north = d->m_north * RAD2DEG;
south = d->m_south * RAD2DEG;
east = d->m_east * RAD2DEG;
west = d->m_west * RAD2DEG;
break;
}
}
void GeoDataLatLonBox::setBoundaries(qreal north, qreal south, qreal east, qreal west, GeoDataCoordinates::Unit unit)<--- Shadow argument<--- Shadow argument<--- Shadow argument<--- Shadow argument
{
switch (unit)
{
default:
case GeoDataCoordinates::Radian:
d->m_north = GeoDataCoordinates::normalizeLat(north);
d->m_south = GeoDataCoordinates::normalizeLat(south);
d->m_east = GeoDataCoordinates::normalizeLon(east);
d->m_west = GeoDataCoordinates::normalizeLon(west);
break;
case GeoDataCoordinates::Degree:
d->m_north = GeoDataCoordinates::normalizeLat(north * DEG2RAD);
d->m_south = GeoDataCoordinates::normalizeLat(south * DEG2RAD);
d->m_east = GeoDataCoordinates::normalizeLon(east * DEG2RAD);
d->m_west = GeoDataCoordinates::normalizeLon(west * DEG2RAD);
break;
}
}
void GeoDataLatLonBox::scale(qreal verticalFactor, qreal horizontalFactor) const
{
GeoDataCoordinates const middle = center();
qreal const deltaY = 0.5 * height() * verticalFactor;
qreal const deltaX = 0.5 * width() * horizontalFactor;
d->m_north = qMin((middle.latitude() + deltaY), static_cast<qreal>(M_PI / 2));
d->m_south = qMax((middle.latitude() - deltaY), static_cast<qreal>(-M_PI / 2));
if (deltaX > 180 * DEG2RAD)
{
d->m_east = M_PI;
d->m_west = -M_PI;
}
else
{
d->m_east = GeoDataCoordinates::normalizeLon(middle.longitude() + deltaX);
d->m_west = GeoDataCoordinates::normalizeLon(middle.longitude() - deltaX);
}
}
GeoDataLatLonBox GeoDataLatLonBox::scaled(qreal verticalFactor, qreal horizontalFactor) const
{
GeoDataLatLonBox result = *this;
result.scale(verticalFactor, horizontalFactor);
return result;
}
qreal GeoDataLatLonBox::width(GeoDataCoordinates::Unit unit) const
{
return GeoDataLatLonBox::width(d->m_east, d->m_west, unit);
}
qreal GeoDataLatLonBox::width(qreal east, qreal west, GeoDataCoordinates::Unit unit)<--- Shadow argument<--- Shadow argument
{
qreal width = fabs((qreal)(GeoDataLatLonBox::crossesDateLine(east, west)
? 2 * M_PI - west + east
: east - west));
// This also covers the case where this bounding box covers the whole
// longitude range ( -180 <= lon <= + 180 ).
if (width > 2 * M_PI)
{
width = 2 * M_PI;
}
if (unit == GeoDataCoordinates::Degree)
{
return width * RAD2DEG;
}
return width;
}
qreal GeoDataLatLonBox::height(GeoDataCoordinates::Unit unit) const
{
return GeoDataLatLonBox::height(d->m_north, d->m_south, unit);
}
qreal GeoDataLatLonBox::height(qreal north, qreal south, GeoDataCoordinates::Unit unit)<--- Shadow argument<--- Shadow argument
{
qreal height = fabs((qreal)(south - north));
if (unit == GeoDataCoordinates::Degree)
{
return height * RAD2DEG;
}
return height;
}
bool GeoDataLatLonBox::crossesDateLine() const
{
return GeoDataLatLonBox::crossesDateLine(d->m_east, d->m_west);
}
bool GeoDataLatLonBox::crossesDateLine(qreal east, qreal west)<--- Shadow argument<--- Shadow argument
{
return east < west || (east == M_PI && west == -M_PI);
}
GeoDataCoordinates GeoDataLatLonBox::center() const
{
if (isEmpty())
{
return GeoDataCoordinates();
}
if (crossesDateLine())
return GeoDataCoordinates(GeoDataCoordinates::normalizeLon(east() + 2 * M_PI - (east() + 2 * M_PI - west()) / 2),
north() - (north() - south()) / 2);
else
return GeoDataCoordinates(east() - (east() - west()) / 2,
north() - (north() - south()) / 2);
}
bool GeoDataLatLonBox::containsPole(Pole pole) const
{
switch (pole)
{
case NorthPole:
return (2 * north() == +M_PI);
case SouthPole:
return (2 * south() == -M_PI);
default:
case AnyPole:
return (2 * north() == +M_PI
|| 2 * south() == -M_PI);
}
qCDebug(DIGIKAM_GEOENGINE_LOG) << Q_FUNC_INFO << "Invalid pole";
return false;
}
bool GeoDataLatLonBox::contains(qreal lon, qreal lat) const
{
if (lat < d->m_south || lat > d->m_north)
{
return false;
}
// We need to take care of the normal case ...
if (((lon < d->m_west || lon > d->m_east) && (d->m_west < d->m_east)) ||
// ... and the case where the bounding box crosses the date line:
((lon < d->m_west && lon > d->m_east) && (d->m_west > d->m_east)))
{
return false;
}
return true;
}
bool GeoDataLatLonBox::contains(const GeoDataCoordinates& point) const
{
qreal lon, lat;
point.geoCoordinates(lon, lat);
return contains(lon, lat);
}
bool GeoDataLatLonBox::contains(const GeoDataLatLonBox& other) const
{
// check the contain criterion for the latitude first as this is trivial:
if (d->m_north >= other.north() && d->m_south <= other.south())
{
if (!crossesDateLine())
{
if (!other.crossesDateLine())
{
// "Normal" case: both bounding boxes don't cross the date line
if (d->m_west <= other.west() && d->m_east >= other.east())
{
return true;
}
}
else
{
// The other bounding box crosses the date line, "this" one does not:
// So the date line splits the other bounding box in two parts.
// Hence "this" bounding box could be fully contained by one of them.
// So for both cases we are able to ignore the "overhanging" portion
// and thereby basically reduce the problem to the "normal" case:
if ((other.west() <= d->m_west && d->m_east <= +M_PI)
|| (other.east() >= d->m_east && d->m_west >= -M_PI))
{
return true;
}
}
}
else
{
if (other.crossesDateLine())
{
// Other "Simple" case: both bounding boxes cross the date line
if (d->m_west <= other.west() && d->m_east >= other.east())
{
return true;
}
}
else
{
// "This" bounding box crosses the date line, the other one does not.
// So the date line splits "this" bounding box in two parts.
// Hence the other bounding box could be fully contained by one of them.
// So for both cases we are able to ignore the "overhanging" portion
// and thereby basically reduce the problem to the "normal" case:
if ((d->m_west <= other.west() && other.east() <= +M_PI)
|| (d->m_east >= other.east() && other.west() >= -M_PI))
{
return true;
}
// if this bounding box covers the whole longitude range ( -180 <= lon <= + 180 )
// then of course the "inner" bounding box is "inside"
if (d->m_west == -M_PI && d->m_east == +M_PI)
{
return true;
}
}
}
}
return false;
}
bool GeoDataLatLonBox::intersects(const GeoDataLatLonBox& other) const
{
if (isEmpty() || other.isEmpty())
{
return false;
}
// check the intersection criterion for the latitude first:
// Case 1: northern boundary of other box intersects:
if ((d->m_north >= other.d->m_north && d->m_south <= other.d->m_north)
// Case 2: northern boundary of this box intersects:
|| (other.d->m_north >= d->m_north && other.d->m_south <= d->m_north)
// Case 3: southern boundary of other box intersects:
|| (d->m_north >= other.d->m_south && d->m_south <= other.d->m_south)
// Case 4: southern boundary of this box intersects:
|| (other.d->m_north >= d->m_south && other.d->m_south <= d->m_south))
{
if (!crossesDateLine())
{
if (!other.crossesDateLine())
{
// "Normal" case: both bounding boxes don't cross the date line
// Case 1: eastern boundary of other box intersects:
if ((d->m_east >= other.d->m_east && d->m_west <= other.d->m_east)
// Case 2: eastern boundary of this box intersects:
|| (other.d->m_east >= d->m_east && other.d->m_west <= d->m_east)
// Case 3: western boundary of other box intersects:
|| (d->m_east >= other.d->m_west && d->m_west <= other.d->m_west)
// Case 4: western boundary of this box intersects:
|| (other.d->m_east >= d->m_west && other.d->m_west <= d->m_west))
{
return true;
}
}
else
{
// The other bounding box crosses the date line, "this" one does not:
// So the date line splits the other bounding box in two parts.
if (d->m_west <= other.d->m_east || d->m_east >= other.d->m_west)
{
return true;
}
}
}
else
{
if (other.crossesDateLine())
{
// The trivial case: both bounding boxes cross the date line and intersect
return true;
}
else
{
// "This" bounding box crosses the date line, the other one does not.
// So the date line splits "this" bounding box in two parts.
//
// This also covers the case where this bounding box covers the whole
// longitude range ( -180 <= lon <= + 180 ).
if (other.d->m_west <= d->m_east || other.d->m_east >= d->m_west)
{
return true;
}
}
}
}
return false;
}
GeoDataLatLonBox GeoDataLatLonBox::united(const GeoDataLatLonBox& other) const
{
if (isEmpty())
{
return other;
}
if (other.isEmpty())
{
return *this;
}
GeoDataLatLonBox result;
// use the position of the centers of the boxes to determine the "smallest"
// box (i.e. should the total box go through IDL or not). this
// determination does not depend on one box or the other crossing IDL too
GeoDataCoordinates c1 = center();
GeoDataCoordinates c2 = other.center();
// do latitude first, quite simple
result.setNorth(qMax(d->m_north, other.north()));
result.setSouth(qMin(d->m_south, other.south()));
qreal w1 = d->m_west;
qreal w2 = other.west();
qreal e1 = d->m_east;
qreal e2 = other.east();
bool const idl1 = d->m_east < d->m_west;
bool const idl2 = other.d->m_east < other.d->m_west;
if (idl1)
{
w1 += 2 * M_PI;
e1 += 2 * M_PI;
}
if (idl2)
{
w2 += 2 * M_PI;
e2 += 2 * M_PI;
}
// in the usual case, we take the maximum of east bounds, and
// the minimum of west bounds. The exceptions are:
// - centers of boxes are more than 180 apart
// (so the smallest box should go around the IDL)
//
// - 1 but not 2 boxes are crossing IDL
if (fabs(c2.longitude() - c1.longitude()) > M_PI
|| (idl1 ^ idl2))
{
// exceptions, we go the unusual way:
// min of east, max of west
result.setEast(qMin(e1, e2));
result.setWest(qMax(w1, w2));
}
else
{
// normal case, max of east, min of west
result.setEast(qMax(e1, e2));
result.setWest(qMin(w1, w2));
}
return result;
}
GeoDataLatLonBox GeoDataLatLonBox::toCircumscribedRectangle() const
{
QVector<GeoDataCoordinates> coordinates;
coordinates.reserve(4);
coordinates.append(GeoDataCoordinates(west(), north()));
coordinates.append(GeoDataCoordinates(west(), south()));
coordinates.append(GeoDataCoordinates(east() + (crossesDateLine() ? 2 * M_PI : 0), north()));
coordinates.append(GeoDataCoordinates(east() + (crossesDateLine() ? 2 * M_PI : 0), south()));
const qreal cosRotation = cos(rotation());
const qreal sinRotation = sin(rotation());
qreal centerLat = center().latitude();
qreal centerLon = center().longitude();
if (GeoDataLatLonBox(0, 0, center().longitude(), west()).crossesDateLine())
{
if (!centerLon)
{
centerLon += M_PI;
}
else
{
centerLon += 2 * M_PI;
}
}
GeoDataLatLonBox box;
bool northSet = false;
bool southSet = false;
bool eastSet = false;
bool westSet = false;
for (const GeoDataCoordinates& coord : coordinates)
{
const qreal lon = coord.longitude();
const qreal lat = coord.latitude();
const qreal rotatedLon = (lon - centerLon) * cosRotation - (lat - centerLat) * sinRotation + centerLon;
const qreal rotatedLat = (lon - centerLon) * sinRotation + (lat - centerLat) * cosRotation + centerLat;
if (!northSet || rotatedLat > box.north())
{
northSet = true;
box.setNorth(rotatedLat);
}
if (!southSet || rotatedLat < box.south())
{
southSet = true;
box.setSouth(rotatedLat);
}
if (!westSet || rotatedLon < box.west())
{
westSet = true;
box.setWest(rotatedLon);
}
if (!eastSet || rotatedLon > box.east())
{
eastSet = true;
box.setEast(rotatedLon);
}
}
box.setBoundaries(box.north(), box.south(), box.east(), box.west());
return box;
}
GeoDataLatLonBox& GeoDataLatLonBox::operator=(const GeoDataLatLonBox& other)
{
GeoDataObject::operator=(other);
*d = *other.d;
return *this;
}
GeoDataLatLonBox GeoDataLatLonBox::operator|(const GeoDataLatLonBox& other) const
{
return united(other);
}
GeoDataLatLonBox& GeoDataLatLonBox::operator|=(const GeoDataLatLonBox& other)
{
*this = united(other);
return *this;
}
void GeoDataLatLonBox::pack(QDataStream& stream) const
{
GeoDataObject::pack(stream);
stream << d->m_north << d->m_south << d->m_east << d->m_west << d->m_rotation;
}
void GeoDataLatLonBox::unpack(QDataStream& stream)
{
GeoDataObject::unpack(stream);
stream >> d->m_north >> d->m_south >> d->m_east >> d->m_west >> d->m_rotation;
}
GeoDataLatLonBox GeoDataLatLonBox::fromLineStringBase(const GeoDataLineString& lineString)
{
// If the line string is empty return an empty boundingbox
if (lineString.isEmpty())
{
return GeoDataLatLonBox();
}
qreal lon, lat;
lineString.first().geoCoordinates(lon, lat);
GeoDataCoordinates::normalizeLonLat(lon, lat);
qreal north = lat;<--- Shadow local variable
qreal south = lat;<--- Shadow local variable
qreal west = lon;<--- Shadow local variable
qreal east = lon;<--- Shadow local variable
// If there's only a single node stored then the boundingbox only contains that point
if (lineString.size() == 1)
{
return GeoDataLatLonBox(north, south, east, west);
}
// Specifies whether the polygon crosses the IDL
bool idlCrossed = false;
// "idlCrossState" specifies the state concerning IDL crossage.
// This is needed in order to create optimal bounding boxes in case of covering the IDL
// Every time the IDL gets crossed from east to west the idlCrossState value gets
// increased by one.
// Every time the IDL gets crossed from west to east the idlCrossState value gets
// decreased by one.
int idlCrossState = 0;
int idlMaxCrossState = 0;
int idlMinCrossState = 0;
// Holds values for east and west while idlCrossState != 0
qreal otherWest = lon;
qreal otherEast = lon;
qreal previousLon = lon;
int currentSign = (lon < 0) ? -1 : +1;
int previousSign = currentSign;
QVector<GeoDataCoordinates>::ConstIterator it(lineString.constBegin());
QVector<GeoDataCoordinates>::ConstIterator itEnd(lineString.constEnd());
bool processingLastNode = false;
while (it != itEnd)
{
// Get coordinates and normalize them to the desired range.
(it)->geoCoordinates(lon, lat);
GeoDataCoordinates::normalizeLonLat(lon, lat);
// Determining the maximum and minimum latitude
if (lat > north)
{
north = lat;
}
else if (lat < south)
{
south = lat;
}
currentSign = (lon < 0) ? -1 : +1;
// Once the polyline crosses the dateline the covered bounding box
// would cover the whole [-M_PI; M_PI] range.
// When looking separately at the longitude range that gets covered
// east and west from the IDL we get two bounding boxes (we prefix
// the resulting longitude range on the "other side" with "other").
// By picking the "inner" range values we get a more appropriate
// optimized single bounding box.
// IDL check
if (previousSign != currentSign
&& fabs(previousLon) + fabs(lon) > M_PI)
{
// Initialize values for otherWest and otherEast
if (idlCrossed == false)
{
otherWest = lon;
otherEast = lon;
idlCrossed = true;
}
// Determine the new IDL Cross State
if (previousLon < 0)
{
idlCrossState++;
if (idlCrossState > idlMaxCrossState)
{
idlMaxCrossState = idlCrossState;
}
}
else
{
idlCrossState--;
if (idlCrossState < idlMinCrossState)
{
idlMinCrossState = idlCrossState;
}
}
}
if (idlCrossState == 0)
{
if (lon > east)
{
east = lon;
}
if (lon < west)
{
west = lon;
}
}
else
{
if (lon > otherEast)
{
otherEast = lon;
}
if (lon < otherWest)
{
otherWest = lon;
}
}
previousLon = lon;
previousSign = currentSign;
if (processingLastNode)
{
break;
}
++it;
if (lineString.isClosed() && it == itEnd)
{
it = lineString.constBegin();
processingLastNode = true;
}
}
if (idlCrossed)
{
if (idlMinCrossState < 0)
{
east = otherEast;
}
if (idlMaxCrossState > 0)
{
west = otherWest;
}
if ((idlMinCrossState < 0 && idlMaxCrossState > 0)
|| idlMinCrossState < -1 || idlMaxCrossState > 1
|| west <= east)
{
east = +M_PI;
west = -M_PI;
// if polygon fully in south hemisphere, contain south pole
if (north < 0)
{
south = -M_PI / 2;
}
else
{
north = M_PI / 2;
}
}
}
return GeoDataLatLonBox(north, south, east, west);
}
bool GeoDataLatLonBox::isNull() const
{
return d->m_north == d->m_south && d->m_east == d->m_west;
}
bool GeoDataLatLonBox::isEmpty() const
{
return *this == empty;
}
bool GeoDataLatLonBox::fuzzyCompare(const GeoDataLatLonBox& lhs,
const GeoDataLatLonBox& rhs,
const qreal factor)
{
bool equal = true;
// Check the latitude for approximate equality
double latDelta = lhs.height() * factor;
if (fabs(lhs.north() - rhs.north()) > latDelta)
{
equal = false;
}
if (fabs(lhs.south() - rhs.south()) > latDelta)
{
equal = false;
}
// Check the longitude for approximate equality
double lonDelta = lhs.width() * factor;
double lhsEast = lhs.east();
double rhsEast = rhs.east();
if (!GeoDataLatLonBox::crossesDateLine(lhsEast, rhsEast))
{
if (fabs(lhsEast - rhsEast) > lonDelta)
{
equal = false;
}
}
else
{
if (lhsEast < 0 && rhsEast > 0)
{
lhsEast += 2 * M_PI;
if (fabs(lhsEast - rhsEast) > lonDelta)
{
equal = false;
}
}
if (lhsEast > 0 && rhsEast < 0)
{
rhsEast += 2 * M_PI;
if (fabs(lhsEast - rhsEast) > lonDelta)
{
equal = false;
}
}
}
double lhsWest = lhs.west();
double rhsWest = rhs.west();
if (!GeoDataLatLonBox::crossesDateLine(lhsWest, rhsWest))
{
if (fabs(lhsWest - rhsWest) > lonDelta)
{
equal = false;
}
}
else
{
if (lhsWest < 0 && rhsWest > 0)
{
lhsWest += 2 * M_PI;
if (fabs(lhsWest - rhsWest) > lonDelta)
{
equal = false;
}
}
if (lhsWest > 0 && rhsWest < 0)
{
rhsWest += 2 * M_PI;
if (fabs(lhsWest - rhsWest) > lonDelta)
{
equal = false;
}
}
}
return equal;
}
void GeoDataLatLonBox::clear()
{
*this = empty;
}
} // namespace Marble
|