-
Notifications
You must be signed in to change notification settings - Fork 743
/
coordinateoperationfactory.cpp
6628 lines (5938 loc) · 279 KB
/
coordinateoperationfactory.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
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
/******************************************************************************
*
* Project: PROJ
* Purpose: ISO19111:2019 implementation
* Author: Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef FROM_PROJ_CPP
#define FROM_PROJ_CPP
#endif
#include "proj/common.hpp"
#include "proj/coordinateoperation.hpp"
#include "proj/coordinates.hpp"
#include "proj/crs.hpp"
#include "proj/io.hpp"
#include "proj/metadata.hpp"
#include "proj/util.hpp"
#include "proj/internal/internal.hpp"
#include "proj/internal/io_internal.hpp"
#include "proj/internal/tracing.hpp"
#include "coordinateoperation_internal.hpp"
#include "coordinateoperation_private.hpp"
#include "oputils.hpp"
// PROJ include order is sensitive
// clang-format off
#include "proj.h"
#include "proj_internal.h" // M_PI
// clang-format on
#include "proj_constants.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <memory>
#include <set>
#include <string>
#include <vector>
// #define TRACE_CREATE_OPERATIONS
// #define DEBUG_SORT
// #define DEBUG_CONCATENATED_OPERATION
#if defined(DEBUG_SORT) || defined(DEBUG_CONCATENATED_OPERATION)
#include <iostream>
void dumpWKT(const NS_PROJ::crs::CRS *crs);
void dumpWKT(const NS_PROJ::crs::CRS *crs) {
auto f(NS_PROJ::io::WKTFormatter::create(
NS_PROJ::io::WKTFormatter::Convention::WKT2_2019));
std::cerr << crs->exportToWKT(f.get()) << std::endl;
}
void dumpWKT(const NS_PROJ::crs::CRSPtr &crs);
void dumpWKT(const NS_PROJ::crs::CRSPtr &crs) { dumpWKT(crs.get()); }
void dumpWKT(const NS_PROJ::crs::CRSNNPtr &crs);
void dumpWKT(const NS_PROJ::crs::CRSNNPtr &crs) {
dumpWKT(crs.as_nullable().get());
}
void dumpWKT(const NS_PROJ::crs::GeographicCRSPtr &crs);
void dumpWKT(const NS_PROJ::crs::GeographicCRSPtr &crs) { dumpWKT(crs.get()); }
void dumpWKT(const NS_PROJ::crs::GeographicCRSNNPtr &crs);
void dumpWKT(const NS_PROJ::crs::GeographicCRSNNPtr &crs) {
dumpWKT(crs.as_nullable().get());
}
#endif
using namespace NS_PROJ::internal;
// ---------------------------------------------------------------------------
NS_PROJ_START
namespace operation {
// ---------------------------------------------------------------------------
#ifdef TRACE_CREATE_OPERATIONS
//! @cond Doxygen_Suppress
static std::string objectAsStr(const common::IdentifiedObject *obj) {
std::string ret(obj->nameStr());
const auto &ids = obj->identifiers();
if (!ids.empty()) {
ret += " (";
ret += (*ids[0]->codeSpace()) + ":" + ids[0]->code();
ret += ")";
}
return ret;
}
//! @endcond
#endif
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static double getPseudoArea(const metadata::ExtentPtr &extent) {
if (!extent)
return 0.0;
const auto &geographicElements = extent->geographicElements();
if (geographicElements.empty())
return 0.0;
auto bbox = dynamic_cast<const metadata::GeographicBoundingBox *>(
geographicElements[0].get());
if (!bbox)
return 0;
double w = bbox->westBoundLongitude();
double s = bbox->southBoundLatitude();
double e = bbox->eastBoundLongitude();
double n = bbox->northBoundLatitude();
if (w > e) {
e += 360.0;
}
// Integrate cos(lat) between south_lat and north_lat
return (e - w) * (std::sin(common::Angle(n).getSIValue()) -
std::sin(common::Angle(s).getSIValue()));
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct CoordinateOperationContext::Private {
io::AuthorityFactoryPtr authorityFactory_{};
metadata::ExtentPtr extent_{};
double accuracy_ = 0.0;
SourceTargetCRSExtentUse sourceAndTargetCRSExtentUse_ =
CoordinateOperationContext::SourceTargetCRSExtentUse::SMALLEST;
SpatialCriterion spatialCriterion_ =
CoordinateOperationContext::SpatialCriterion::STRICT_CONTAINMENT;
bool usePROJNames_ = true;
GridAvailabilityUse gridAvailabilityUse_ =
GridAvailabilityUse::USE_FOR_SORTING;
IntermediateCRSUse allowUseIntermediateCRS_ = CoordinateOperationContext::
IntermediateCRSUse::IF_NO_DIRECT_TRANSFORMATION;
std::vector<std::pair<std::string, std::string>>
intermediateCRSAuthCodes_{};
bool discardSuperseded_ = true;
bool allowBallpark_ = true;
std::shared_ptr<util::optional<common::DataEpoch>> sourceCoordinateEpoch_{
std::make_shared<util::optional<common::DataEpoch>>()};
std::shared_ptr<util::optional<common::DataEpoch>> targetCoordinateEpoch_{
std::make_shared<util::optional<common::DataEpoch>>()};
Private() = default;
Private(const Private &) = default;
};
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CoordinateOperationContext::~CoordinateOperationContext() = default;
//! @endcond
// ---------------------------------------------------------------------------
CoordinateOperationContext::CoordinateOperationContext()
: d(internal::make_unique<Private>()) {}
// ---------------------------------------------------------------------------
CoordinateOperationContext::CoordinateOperationContext(
const CoordinateOperationContext &other)
: d(internal::make_unique<Private>(*(other.d))) {}
// ---------------------------------------------------------------------------
/** \brief Return the authority factory, or null */
const io::AuthorityFactoryPtr &
CoordinateOperationContext::getAuthorityFactory() const {
return d->authorityFactory_;
}
// ---------------------------------------------------------------------------
/** \brief Return the desired area of interest, or null */
const metadata::ExtentPtr &
CoordinateOperationContext::getAreaOfInterest() const {
return d->extent_;
}
// ---------------------------------------------------------------------------
/** \brief Set the desired area of interest, or null */
void CoordinateOperationContext::setAreaOfInterest(
const metadata::ExtentPtr &extent) {
d->extent_ = extent;
}
// ---------------------------------------------------------------------------
/** \brief Return the desired accuracy (in metre), or 0 */
double CoordinateOperationContext::getDesiredAccuracy() const {
return d->accuracy_;
}
// ---------------------------------------------------------------------------
/** \brief Set the desired accuracy (in metre), or 0 */
void CoordinateOperationContext::setDesiredAccuracy(double accuracy) {
d->accuracy_ = accuracy;
}
// ---------------------------------------------------------------------------
/** \brief Return whether ballpark transformations are allowed */
bool CoordinateOperationContext::getAllowBallparkTransformations() const {
return d->allowBallpark_;
}
// ---------------------------------------------------------------------------
/** \brief Set whether ballpark transformations are allowed */
void CoordinateOperationContext::setAllowBallparkTransformations(bool allow) {
d->allowBallpark_ = allow;
}
// ---------------------------------------------------------------------------
/** \brief Set how source and target CRS extent should be used
* when considering if a transformation can be used (only takes effect if
* no area of interest is explicitly defined).
*
* The default is
* CoordinateOperationContext::SourceTargetCRSExtentUse::SMALLEST.
*/
void CoordinateOperationContext::setSourceAndTargetCRSExtentUse(
SourceTargetCRSExtentUse use) {
d->sourceAndTargetCRSExtentUse_ = use;
}
// ---------------------------------------------------------------------------
/** \brief Return how source and target CRS extent should be used
* when considering if a transformation can be used (only takes effect if
* no area of interest is explicitly defined).
*
* The default is
* CoordinateOperationContext::SourceTargetCRSExtentUse::SMALLEST.
*/
CoordinateOperationContext::SourceTargetCRSExtentUse
CoordinateOperationContext::getSourceAndTargetCRSExtentUse() const {
return d->sourceAndTargetCRSExtentUse_;
}
// ---------------------------------------------------------------------------
/** \brief Set the spatial criterion to use when comparing the area of
* validity
* of coordinate operations with the area of interest / area of validity of
* source and target CRS.
*
* The default is STRICT_CONTAINMENT.
*/
void CoordinateOperationContext::setSpatialCriterion(
SpatialCriterion criterion) {
d->spatialCriterion_ = criterion;
}
// ---------------------------------------------------------------------------
/** \brief Return the spatial criterion to use when comparing the area of
* validity
* of coordinate operations with the area of interest / area of validity of
* source and target CRS.
*
* The default is STRICT_CONTAINMENT.
*/
CoordinateOperationContext::SpatialCriterion
CoordinateOperationContext::getSpatialCriterion() const {
return d->spatialCriterion_;
}
// ---------------------------------------------------------------------------
/** \brief Set whether PROJ alternative grid names should be substituted to
* the official authority names.
*
* This only has effect is an authority factory with a non-null database context
* has been attached to this context.
*
* If set to false, it is still possible to
* obtain later the substitution by using io::PROJStringFormatter::create()
* with a non-null database context.
*
* The default is true.
*/
void CoordinateOperationContext::setUsePROJAlternativeGridNames(
bool usePROJNames) {
d->usePROJNames_ = usePROJNames;
}
// ---------------------------------------------------------------------------
/** \brief Return whether PROJ alternative grid names should be substituted to
* the official authority names.
*
* The default is true.
*/
bool CoordinateOperationContext::getUsePROJAlternativeGridNames() const {
return d->usePROJNames_;
}
// ---------------------------------------------------------------------------
/** \brief Return whether transformations that are superseded (but not
* deprecated)
* should be discarded.
*
* The default is true.
*/
bool CoordinateOperationContext::getDiscardSuperseded() const {
return d->discardSuperseded_;
}
// ---------------------------------------------------------------------------
/** \brief Set whether transformations that are superseded (but not deprecated)
* should be discarded.
*
* The default is true.
*/
void CoordinateOperationContext::setDiscardSuperseded(bool discard) {
d->discardSuperseded_ = discard;
}
// ---------------------------------------------------------------------------
/** \brief Set how grid availability is used.
*
* The default is USE_FOR_SORTING.
*/
void CoordinateOperationContext::setGridAvailabilityUse(
GridAvailabilityUse use) {
d->gridAvailabilityUse_ = use;
}
// ---------------------------------------------------------------------------
/** \brief Return how grid availability is used.
*
* The default is USE_FOR_SORTING.
*/
CoordinateOperationContext::GridAvailabilityUse
CoordinateOperationContext::getGridAvailabilityUse() const {
return d->gridAvailabilityUse_;
}
// ---------------------------------------------------------------------------
/** \brief Set whether an intermediate pivot CRS can be used for researching
* coordinate operations between a source and target CRS.
*
* Concretely if in the database there is an operation from A to C
* (or C to A), and another one from C to B (or B to C), but no direct
* operation between A and B, setting this parameter to
* ALWAYS/IF_NO_DIRECT_TRANSFORMATION, allow chaining both operations.
*
* The current implementation is limited to researching one intermediate
* step.
*
* By default, with the IF_NO_DIRECT_TRANSFORMATION strategy, all potential
* C candidates will be used if there is no direct transformation.
*/
void CoordinateOperationContext::setAllowUseIntermediateCRS(
IntermediateCRSUse use) {
d->allowUseIntermediateCRS_ = use;
}
// ---------------------------------------------------------------------------
/** \brief Return whether an intermediate pivot CRS can be used for researching
* coordinate operations between a source and target CRS.
*
* Concretely if in the database there is an operation from A to C
* (or C to A), and another one from C to B (or B to C), but no direct
* operation between A and B, setting this parameter to
* ALWAYS/IF_NO_DIRECT_TRANSFORMATION, allow chaining both operations.
*
* The default is IF_NO_DIRECT_TRANSFORMATION.
*/
CoordinateOperationContext::IntermediateCRSUse
CoordinateOperationContext::getAllowUseIntermediateCRS() const {
return d->allowUseIntermediateCRS_;
}
// ---------------------------------------------------------------------------
/** \brief Restrict the potential pivot CRSs that can be used when trying to
* build a coordinate operation between two CRS that have no direct operation.
*
* @param intermediateCRSAuthCodes a vector of (auth_name, code) that can be
* used as potential pivot RS
*/
void CoordinateOperationContext::setIntermediateCRS(
const std::vector<std::pair<std::string, std::string>>
&intermediateCRSAuthCodes) {
d->intermediateCRSAuthCodes_ = intermediateCRSAuthCodes;
}
// ---------------------------------------------------------------------------
/** \brief Return the potential pivot CRSs that can be used when trying to
* build a coordinate operation between two CRS that have no direct operation.
*
*/
const std::vector<std::pair<std::string, std::string>> &
CoordinateOperationContext::getIntermediateCRS() const {
return d->intermediateCRSAuthCodes_;
}
// ---------------------------------------------------------------------------
/** \brief Set the source coordinate epoch.
*/
void CoordinateOperationContext::setSourceCoordinateEpoch(
const util::optional<common::DataEpoch> &epoch) {
d->sourceCoordinateEpoch_ =
std::make_shared<util::optional<common::DataEpoch>>(epoch);
}
// ---------------------------------------------------------------------------
/** \brief Return the source coordinate epoch.
*/
const util::optional<common::DataEpoch> &
CoordinateOperationContext::getSourceCoordinateEpoch() const {
return *(d->sourceCoordinateEpoch_);
}
// ---------------------------------------------------------------------------
/** \brief Set the target coordinate epoch.
*/
void CoordinateOperationContext::setTargetCoordinateEpoch(
const util::optional<common::DataEpoch> &epoch) {
d->targetCoordinateEpoch_ =
std::make_shared<util::optional<common::DataEpoch>>(epoch);
}
// ---------------------------------------------------------------------------
/** \brief Return the target coordinate epoch.
*/
const util::optional<common::DataEpoch> &
CoordinateOperationContext::getTargetCoordinateEpoch() const {
return *(d->targetCoordinateEpoch_);
}
// ---------------------------------------------------------------------------
/** \brief Creates a context for a coordinate operation.
*
* If a non null authorityFactory is provided, the resulting context should
* not be used simultaneously by more than one thread.
*
* If authorityFactory->getAuthority() is the empty string, then coordinate
* operations from any authority will be searched, with the restrictions set
* in the authority_to_authority_preference database table.
* If authorityFactory->getAuthority() is set to "any", then coordinate
* operations from any authority will be searched
* If authorityFactory->getAuthority() is a non-empty string different of "any",
* then coordinate operations will be searched only in that authority namespace.
*
* @param authorityFactory Authority factory, or null if no database lookup
* is allowed.
* Use io::authorityFactory::create(context, std::string()) to allow all
* authorities to be used.
* @param extent Area of interest, or null if none is known.
* @param accuracy Maximum allowed accuracy in metre, as specified in or
* 0 to get best accuracy.
* @return a new context.
*/
CoordinateOperationContextNNPtr CoordinateOperationContext::create(
const io::AuthorityFactoryPtr &authorityFactory,
const metadata::ExtentPtr &extent, double accuracy) {
auto ctxt = NN_NO_CHECK(
CoordinateOperationContext::make_unique<CoordinateOperationContext>());
ctxt->d->authorityFactory_ = authorityFactory;
ctxt->d->extent_ = extent;
ctxt->d->accuracy_ = accuracy;
return ctxt;
}
// ---------------------------------------------------------------------------
/** \brief Clone a coordinate operation context.
*
* @return a new context.
* @since 9.2
*/
CoordinateOperationContextNNPtr CoordinateOperationContext::clone() const {
return NN_NO_CHECK(
CoordinateOperationContext::make_unique<CoordinateOperationContext>(
*this));
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct CoordinateOperationFactory::Private {
struct Context {
// This is the extent of the source CRS and target CRS of the initial
// CoordinateOperationFactory::createOperations() public call, not
// necessarily the ones of intermediate
// CoordinateOperationFactory::Private::createOperations() calls.
// This is used to compare transformations area of use against the
// area of use of the source & target CRS.
const metadata::ExtentPtr &extent1;
const metadata::ExtentPtr &extent2;
const CoordinateOperationContextNNPtr &context;
bool inCreateOperationsWithDatumPivotAntiRecursion = false;
bool inCreateOperationsGeogToVertWithAlternativeGeog = false;
bool inCreateOperationsGeogToVertWithIntermediateVert = false;
bool skipHorizontalTransformation = false;
int nRecLevelCreateOperations = 0;
std::map<std::pair<io::AuthorityFactory::ObjectType, std::string>,
std::list<std::pair<std::string, std::string>>>
cacheNameToCRS{};
Context(const metadata::ExtentPtr &extent1In,
const metadata::ExtentPtr &extent2In,
const CoordinateOperationContextNNPtr &contextIn)
: extent1(extent1In), extent2(extent2In), context(contextIn) {}
};
static std::vector<CoordinateOperationNNPtr>
createOperations(const crs::CRSNNPtr &sourceCRS,
const crs::CRSNNPtr &targetCRS, Context &context);
private:
static constexpr bool disallowEmptyIntersection = true;
static void
buildCRSIds(const crs::CRSNNPtr &crs, Private::Context &context,
std::list<std::pair<std::string, std::string>> &ids);
static std::vector<CoordinateOperationNNPtr> findOpsInRegistryDirect(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, bool &resNonEmptyBeforeFiltering);
static std::vector<CoordinateOperationNNPtr>
findOpsInRegistryDirectTo(const crs::CRSNNPtr &targetCRS,
Private::Context &context);
static std::vector<CoordinateOperationNNPtr>
findsOpsInRegistryWithIntermediate(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context,
bool useCreateBetweenGeodeticCRSWithDatumBasedIntermediates);
static void createOperationsFromProj4Ext(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
const crs::BoundCRS *boundSrc, const crs::BoundCRS *boundDst,
std::vector<CoordinateOperationNNPtr> &res);
static bool createOperationsFromDatabase(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::GeodeticCRS *geodSrc,
const crs::GeodeticCRS *geodDst, const crs::GeographicCRS *geogSrc,
const crs::GeographicCRS *geogDst, const crs::VerticalCRS *vertSrc,
const crs::VerticalCRS *vertDst,
std::vector<CoordinateOperationNNPtr> &res);
static std::vector<CoordinateOperationNNPtr>
createOperationsGeogToVertFromGeoid(const crs::CRSNNPtr &sourceCRS,
const crs::CRSNNPtr &targetCRS,
const crs::VerticalCRS *vertDst,
Context &context);
static std::vector<CoordinateOperationNNPtr>
createOperationsGeogToVertWithIntermediateVert(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
const crs::VerticalCRS *vertDst, Context &context);
static std::vector<CoordinateOperationNNPtr>
createOperationsGeogToVertWithAlternativeGeog(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Context &context);
static void createOperationsFromDatabaseWithVertCRS(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::GeographicCRS *geogSrc,
const crs::GeographicCRS *geogDst, const crs::VerticalCRS *vertSrc,
const crs::VerticalCRS *vertDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsGeodToGeod(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::GeodeticCRS *geodSrc,
const crs::GeodeticCRS *geodDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsFromSphericalPlanetocentric(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::GeodeticCRS *geodSrc,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsFromBoundOfSphericalPlanetocentric(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::BoundCRS *boundSrc,
const crs::GeodeticCRSNNPtr &geodSrcBase,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsDerivedTo(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::DerivedCRS *derivedSrc,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsBoundToGeog(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::BoundCRS *boundSrc,
const crs::GeographicCRS *geogDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsBoundToVert(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::BoundCRS *boundSrc,
const crs::VerticalCRS *vertDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsVertToVert(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::VerticalCRS *vertSrc,
const crs::VerticalCRS *vertDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsVertToGeog(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::VerticalCRS *vertSrc,
const crs::GeographicCRS *geogDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsVertToGeogBallpark(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::VerticalCRS *vertSrc,
const crs::GeographicCRS *geogDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsBoundToBound(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::BoundCRS *boundSrc,
const crs::BoundCRS *boundDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsCompoundToGeog(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::CompoundCRS *compoundSrc,
const crs::GeographicCRS *geogDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsToGeod(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::GeodeticCRS *geodDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsCompoundToCompound(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::CompoundCRS *compoundSrc,
const crs::CompoundCRS *compoundDst,
std::vector<CoordinateOperationNNPtr> &res);
static void createOperationsBoundToCompound(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::BoundCRS *boundSrc,
const crs::CompoundCRS *compoundDst,
std::vector<CoordinateOperationNNPtr> &res);
static std::vector<CoordinateOperationNNPtr> createOperationsGeogToGeog(
std::vector<CoordinateOperationNNPtr> &res,
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
Private::Context &context, const crs::GeographicCRS *geogSrc,
const crs::GeographicCRS *geogDst);
static void createOperationsWithDatumPivot(
std::vector<CoordinateOperationNNPtr> &res,
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS,
const crs::GeodeticCRS *geodSrc, const crs::GeodeticCRS *geodDst,
Context &context);
static bool
hasPerfectAccuracyResult(const std::vector<CoordinateOperationNNPtr> &res,
const Context &context);
static void setCRSs(CoordinateOperation *co, const crs::CRSNNPtr &sourceCRS,
const crs::CRSNNPtr &targetCRS);
};
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CoordinateOperationFactory::~CoordinateOperationFactory() = default;
//! @endcond
// ---------------------------------------------------------------------------
CoordinateOperationFactory::CoordinateOperationFactory() : d(nullptr) {}
// ---------------------------------------------------------------------------
/** \brief Find a CoordinateOperation from sourceCRS to targetCRS.
*
* This is a helper of createOperations(), using a coordinate operation
* context
* with no authority factory (so no catalog searching is done), no desired
* accuracy and no area of interest.
* This returns the first operation of the result set of createOperations(),
* or null if none found.
*
* @param sourceCRS source CRS.
* @param targetCRS source CRS.
* @return a CoordinateOperation or nullptr.
*/
CoordinateOperationPtr CoordinateOperationFactory::createOperation(
const crs::CRSNNPtr &sourceCRS, const crs::CRSNNPtr &targetCRS) const {
auto res = createOperations(
sourceCRS, targetCRS,
CoordinateOperationContext::create(nullptr, nullptr, 0.0));
if (!res.empty()) {
return res[0];
}
return nullptr;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
// ---------------------------------------------------------------------------
struct PrecomputedOpCharacteristics {
double area_{};
double accuracy_{};
bool isPROJExportable_ = false;
bool hasGrids_ = false;
bool gridsAvailable_ = false;
bool gridsKnown_ = false;
size_t stepCount_ = 0;
size_t projStepCount_ = 0;
bool isApprox_ = false;
bool hasBallparkVertical_ = false;
bool isNullTransformation_ = false;
PrecomputedOpCharacteristics() = default;
PrecomputedOpCharacteristics(double area, double accuracy,
bool isPROJExportable, bool hasGrids,
bool gridsAvailable, bool gridsKnown,
size_t stepCount, size_t projStepCount,
bool isApprox, bool hasBallparkVertical,
bool isNullTransformation)
: area_(area), accuracy_(accuracy), isPROJExportable_(isPROJExportable),
hasGrids_(hasGrids), gridsAvailable_(gridsAvailable),
gridsKnown_(gridsKnown), stepCount_(stepCount),
projStepCount_(projStepCount), isApprox_(isApprox),
hasBallparkVertical_(hasBallparkVertical),
isNullTransformation_(isNullTransformation) {}
};
// ---------------------------------------------------------------------------
// We could have used a lambda instead of this old-school way, but
// filterAndSort() is already huge.
struct SortFunction {
const std::map<CoordinateOperation *, PrecomputedOpCharacteristics> ↦
const std::string BALLPARK_GEOGRAPHIC_OFFSET_FROM;
explicit SortFunction(const std::map<CoordinateOperation *,
PrecomputedOpCharacteristics> &mapIn)
: map(mapIn), BALLPARK_GEOGRAPHIC_OFFSET_FROM(
std::string(BALLPARK_GEOGRAPHIC_OFFSET) + " from ") {}
// Sorting function
// Return true if a < b
bool compare(const CoordinateOperationNNPtr &a,
const CoordinateOperationNNPtr &b) const {
auto iterA = map.find(a.get());
assert(iterA != map.end());
auto iterB = map.find(b.get());
assert(iterB != map.end());
// CAUTION: the order of the comparisons is extremely important
// to get the intended result.
if (iterA->second.isPROJExportable_ &&
!iterB->second.isPROJExportable_) {
return true;
}
if (!iterA->second.isPROJExportable_ &&
iterB->second.isPROJExportable_) {
return false;
}
if (!iterA->second.isApprox_ && iterB->second.isApprox_) {
return true;
}
if (iterA->second.isApprox_ && !iterB->second.isApprox_) {
return false;
}
if (!iterA->second.hasBallparkVertical_ &&
iterB->second.hasBallparkVertical_) {
return true;
}
if (iterA->second.hasBallparkVertical_ &&
!iterB->second.hasBallparkVertical_) {
return false;
}
if (!iterA->second.isNullTransformation_ &&
iterB->second.isNullTransformation_) {
return true;
}
if (iterA->second.isNullTransformation_ &&
!iterB->second.isNullTransformation_) {
return false;
}
// Operations where grids are all available go before other
if (iterA->second.gridsAvailable_ && !iterB->second.gridsAvailable_) {
return true;
}
if (iterB->second.gridsAvailable_ && !iterA->second.gridsAvailable_) {
return false;
}
// Operations where grids are all known in our DB go before other
if (iterA->second.gridsKnown_ && !iterB->second.gridsKnown_) {
return true;
}
if (iterB->second.gridsKnown_ && !iterA->second.gridsKnown_) {
return false;
}
// Operations with known accuracy go before those with unknown accuracy
const double accuracyA = iterA->second.accuracy_;
const double accuracyB = iterB->second.accuracy_;
if (accuracyA >= 0 && accuracyB < 0) {
return true;
}
if (accuracyB >= 0 && accuracyA < 0) {
return false;
}
if (accuracyA < 0 && accuracyB < 0) {
// unknown accuracy ? then prefer operations with grids, which
// are likely to have best practical accuracy
if (iterA->second.hasGrids_ && !iterB->second.hasGrids_) {
return true;
}
if (!iterA->second.hasGrids_ && iterB->second.hasGrids_) {
return false;
}
}
// Operations with larger non-zero area of use go before those with
// lower one
const double areaA = iterA->second.area_;
const double areaB = iterB->second.area_;
if (areaA > 0) {
if (areaA > areaB) {
return true;
}
if (areaA < areaB) {
return false;
}
} else if (areaB > 0) {
return false;
}
// Operations with better accuracy go before those with worse one
if (accuracyA >= 0 && accuracyA < accuracyB) {
return true;
}
if (accuracyB >= 0 && accuracyB < accuracyA) {
return false;
}
if (accuracyA >= 0 && accuracyA == accuracyB) {
// same accuracy ? then prefer operations without grids
if (!iterA->second.hasGrids_ && iterB->second.hasGrids_) {
return true;
}
if (iterA->second.hasGrids_ && !iterB->second.hasGrids_) {
return false;
}
}
// The less intermediate steps, the better
if (iterA->second.stepCount_ < iterB->second.stepCount_) {
return true;
}
if (iterB->second.stepCount_ < iterA->second.stepCount_) {
return false;
}
// Compare number of steps in PROJ pipeline, and prefer the ones
// with less operations.
if (iterA->second.projStepCount_ != 0 &&
iterB->second.projStepCount_ != 0) {
if (iterA->second.projStepCount_ < iterB->second.projStepCount_) {
return true;
}
if (iterB->second.projStepCount_ < iterA->second.projStepCount_) {
return false;
}
}
const auto &a_name = a->nameStr();
const auto &b_name = b->nameStr();
// Make sure that
// "Ballpark geographic offset from NAD83(CSRS)v6 to NAD83(CSRS)"
// has more priority than
// "Ballpark geographic offset from ITRF2008 to NAD83(CSRS)"
const auto posA = a_name.find(BALLPARK_GEOGRAPHIC_OFFSET_FROM);
const auto posB = b_name.find(BALLPARK_GEOGRAPHIC_OFFSET_FROM);
if (posA != std::string::npos && posB != std::string::npos) {
const auto pos2A = a_name.find(" to ", posA);
const auto pos2B = b_name.find(" to ", posB);
if (pos2A != std::string::npos && pos2B != std::string::npos) {
const auto pos3A = a_name.find(" + ", pos2A);
const auto pos3B = b_name.find(" + ", pos2B);
const std::string fromA = a_name.substr(
posA + BALLPARK_GEOGRAPHIC_OFFSET_FROM.size(),
pos2A - (posA + BALLPARK_GEOGRAPHIC_OFFSET_FROM.size()));
const std::string toA =
a_name.substr(pos2A + strlen(" to "),
pos3A == std::string::npos
? pos3A
: pos3A - (pos2A + strlen(" to ")));
const std::string fromB = b_name.substr(
posB + BALLPARK_GEOGRAPHIC_OFFSET_FROM.size(),
pos2B - (posB + BALLPARK_GEOGRAPHIC_OFFSET_FROM.size()));
const std::string toB =
b_name.substr(pos2B + strlen(" to "),
pos3B == std::string::npos
? pos3B
: pos3B - (pos2B + strlen(" to ")));
const bool similarCRSInA =
(fromA.find(toA) == 0 || toA.find(fromA) == 0);
const bool similarCRSInB =
(fromB.find(toB) == 0 || toB.find(fromB) == 0);
if (similarCRSInA && !similarCRSInB) {
return true;
}
if (!similarCRSInA && similarCRSInB) {
return false;
}
}
}
// The shorter name, the better ?
if (a_name.size() < b_name.size()) {
return true;
}
if (b_name.size() < a_name.size()) {
return false;
}
// Arbitrary final criterion. We actually return the greater element
// first, so that "Amersfoort to WGS 84 (4)" is presented before
// "Amersfoort to WGS 84 (3)", which is probably a better guess.