/
crs.cpp
4949 lines (4264 loc) · 182 KB
/
crs.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
//! @cond Doxygen_Suppress
#define DO_NOT_DEFINE_EXTERN_DERIVED_CRS_TEMPLATE
//! @endcond
#include "proj/crs.hpp"
#include "proj/common.hpp"
#include "proj/coordinateoperation.hpp"
#include "proj/coordinatesystem.hpp"
#include "proj/io.hpp"
#include "proj/util.hpp"
#include "proj/internal/coordinatesystem_internal.hpp"
#include "proj/internal/internal.hpp"
#include "proj/internal/io_internal.hpp"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace NS_PROJ::internal;
#if 0
namespace dropbox{ namespace oxygen {
template<> nn<NS_PROJ::crs::CRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::SingleCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::GeodeticCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::GeographicCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::ProjectedCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::VerticalCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::CompoundCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::TemporalCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::EngineeringCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::ParametricCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::BoundCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedGeodeticCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedGeographicCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedProjectedCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedVerticalCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedTemporalCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedEngineeringCRSPtr>::~nn() = default;
template<> nn<NS_PROJ::crs::DerivedParametricCRSPtr>::~nn() = default;
}}
#endif
NS_PROJ_START
namespace crs {
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct CRS::Private {
BoundCRSPtr canonicalBoundCRS_{};
std::string extensionProj4_{};
bool implicitCS_ = false;
void setImplicitCS(const util::PropertyMap &properties) {
const auto pVal = properties.get("IMPLICIT_CS");
if (pVal) {
if (const auto genVal =
dynamic_cast<const util::BoxedValue *>(pVal->get())) {
if (genVal->type() == util::BoxedValue::Type::BOOLEAN &&
genVal->booleanValue()) {
implicitCS_ = true;
}
}
}
}
};
//! @endcond
// ---------------------------------------------------------------------------
CRS::CRS() : d(internal::make_unique<Private>()) {}
// ---------------------------------------------------------------------------
CRS::CRS(const CRS &other)
: ObjectUsage(other), d(internal::make_unique<Private>(*(other.d))) {}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CRS::~CRS() = default;
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the BoundCRS potentially attached to this CRS.
*
* In the case this method is called on a object returned by
* BoundCRS::baseCRSWithCanonicalBoundCRS(), this method will return this
* BoundCRS
*
* @return a BoundCRSPtr, that might be null.
*/
const BoundCRSPtr &CRS::canonicalBoundCRS() PROJ_PURE_DEFN {
return d->canonicalBoundCRS_;
}
// ---------------------------------------------------------------------------
/** \brief Return the GeodeticCRS of the CRS.
*
* Returns the GeodeticCRS contained in a CRS. This works currently with
* input parameters of type GeodeticCRS or derived, ProjectedCRS,
* CompoundCRS or BoundCRS.
*
* @return a GeodeticCRSPtr, that might be null.
*/
GeodeticCRSPtr CRS::extractGeodeticCRS() const {
auto raw = extractGeodeticCRSRaw();
if (raw) {
return std::dynamic_pointer_cast<GeodeticCRS>(
raw->shared_from_this().as_nullable());
}
return nullptr;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
const GeodeticCRS *CRS::extractGeodeticCRSRaw() const {
auto geodCRS = dynamic_cast<const GeodeticCRS *>(this);
if (geodCRS) {
return geodCRS;
}
auto projCRS = dynamic_cast<const ProjectedCRS *>(this);
if (projCRS) {
return projCRS->baseCRS()->extractGeodeticCRSRaw();
}
auto compoundCRS = dynamic_cast<const CompoundCRS *>(this);
if (compoundCRS) {
for (const auto &subCrs : compoundCRS->componentReferenceSystems()) {
auto retGeogCRS = subCrs->extractGeodeticCRSRaw();
if (retGeogCRS) {
return retGeogCRS;
}
}
}
auto boundCRS = dynamic_cast<const BoundCRS *>(this);
if (boundCRS) {
return boundCRS->baseCRS()->extractGeodeticCRSRaw();
}
return nullptr;
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
const std::string &CRS::getExtensionProj4() const noexcept {
return d->extensionProj4_;
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the GeographicCRS of the CRS.
*
* Returns the GeographicCRS contained in a CRS. This works currently with
* input parameters of type GeographicCRS or derived, ProjectedCRS,
* CompoundCRS or BoundCRS.
*
* @return a GeographicCRSPtr, that might be null.
*/
GeographicCRSPtr CRS::extractGeographicCRS() const {
auto raw = extractGeodeticCRSRaw();
if (raw) {
return std::dynamic_pointer_cast<GeographicCRS>(
raw->shared_from_this().as_nullable());
}
return nullptr;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static util::PropertyMap
createPropertyMap(const common::IdentifiedObject *obj) {
auto props = util::PropertyMap().set(common::IdentifiedObject::NAME_KEY,
obj->nameStr());
if (obj->isDeprecated()) {
props.set(common::IdentifiedObject::DEPRECATED_KEY, true);
}
return props;
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CRSNNPtr CRS::alterGeodeticCRS(const GeodeticCRSNNPtr &newGeodCRS) const {
auto geodCRS = dynamic_cast<const GeodeticCRS *>(this);
if (geodCRS) {
return newGeodCRS;
}
auto projCRS = dynamic_cast<const ProjectedCRS *>(this);
if (projCRS) {
return ProjectedCRS::create(createPropertyMap(this), newGeodCRS,
projCRS->derivingConversionRef(),
projCRS->coordinateSystem());
}
auto compoundCRS = dynamic_cast<const CompoundCRS *>(this);
if (compoundCRS) {
std::vector<CRSNNPtr> components;
for (const auto &subCrs : compoundCRS->componentReferenceSystems()) {
components.emplace_back(subCrs->alterGeodeticCRS(newGeodCRS));
}
return CompoundCRS::create(createPropertyMap(this), components);
}
return NN_NO_CHECK(
std::dynamic_pointer_cast<CRS>(shared_from_this().as_nullable()));
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CRSNNPtr CRS::alterCSLinearUnit(const common::UnitOfMeasure &unit) const {
{
auto projCRS = dynamic_cast<const ProjectedCRS *>(this);
if (projCRS) {
return ProjectedCRS::create(
createPropertyMap(this), projCRS->baseCRS(),
projCRS->derivingConversionRef(),
projCRS->coordinateSystem()->alterUnit(unit));
}
}
{
auto geodCRS = dynamic_cast<const GeodeticCRS *>(this);
if (geodCRS && geodCRS->isGeocentric()) {
auto cs = dynamic_cast<const cs::CartesianCS *>(
geodCRS->coordinateSystem().get());
assert(cs);
return GeodeticCRS::create(
createPropertyMap(this), geodCRS->datum(),
geodCRS->datumEnsemble(), cs->alterUnit(unit));
}
}
{
auto geogCRS = dynamic_cast<const GeographicCRS *>(this);
if (geogCRS && geogCRS->coordinateSystem()->axisList().size() == 3) {
return GeographicCRS::create(
createPropertyMap(this), geogCRS->datum(),
geogCRS->datumEnsemble(),
geogCRS->coordinateSystem()->alterLinearUnit(unit));
}
}
{
auto vertCRS = dynamic_cast<const VerticalCRS *>(this);
if (vertCRS) {
return VerticalCRS::create(
createPropertyMap(this), vertCRS->datum(),
vertCRS->datumEnsemble(),
vertCRS->coordinateSystem()->alterUnit(unit));
}
}
{
auto engCRS = dynamic_cast<const EngineeringCRS *>(this);
if (engCRS) {
auto cartCS = util::nn_dynamic_pointer_cast<cs::CartesianCS>(
engCRS->coordinateSystem());
if (cartCS) {
auto props = createPropertyMap(this);
props.set("FORCE_OUTPUT_CS", true);
return EngineeringCRS::create(props, engCRS->datum(),
cartCS->alterUnit(unit));
} else {
auto vertCS = util::nn_dynamic_pointer_cast<cs::VerticalCS>(
engCRS->coordinateSystem());
if (vertCS) {
auto props = createPropertyMap(this);
props.set("FORCE_OUTPUT_CS", true);
return EngineeringCRS::create(props, engCRS->datum(),
vertCS->alterUnit(unit));
}
}
}
}
return NN_NO_CHECK(
std::dynamic_pointer_cast<CRS>(shared_from_this().as_nullable()));
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the VerticalCRS of the CRS.
*
* Returns the VerticalCRS contained in a CRS. This works currently with
* input parameters of type VerticalCRS or derived, CompoundCRS or BoundCRS.
*
* @return a VerticalCRSPtr, that might be null.
*/
VerticalCRSPtr CRS::extractVerticalCRS() const {
auto vertCRS = dynamic_cast<const VerticalCRS *>(this);
if (vertCRS) {
return std::dynamic_pointer_cast<VerticalCRS>(
shared_from_this().as_nullable());
}
auto compoundCRS = dynamic_cast<const CompoundCRS *>(this);
if (compoundCRS) {
for (const auto &subCrs : compoundCRS->componentReferenceSystems()) {
auto retVertCRS = subCrs->extractVerticalCRS();
if (retVertCRS) {
return retVertCRS;
}
}
}
auto boundCRS = dynamic_cast<const BoundCRS *>(this);
if (boundCRS) {
return boundCRS->baseCRS()->extractVerticalCRS();
}
return nullptr;
}
// ---------------------------------------------------------------------------
/** \brief Returns potentially
* a BoundCRS, with a transformation to EPSG:4326, wrapping this CRS
*
* If no such BoundCRS is possible, the object will be returned.
*
* The purpose of this method is to be able to format a PROJ.4 string with
* a +towgs84 parameter or a WKT1:GDAL string with a TOWGS node.
*
* This method will fetch the GeographicCRS of this CRS and find a
* transformation to EPSG:4326 using the domain of the validity of the main CRS.
*
* @return a CRS.
*/
CRSNNPtr CRS::createBoundCRSToWGS84IfPossible(
const io::DatabaseContextPtr &dbContext,
operation::CoordinateOperationContext::IntermediateCRSUse
allowIntermediateCRSUse) const {
auto thisAsCRS = NN_NO_CHECK(
std::static_pointer_cast<CRS>(shared_from_this().as_nullable()));
auto boundCRS = util::nn_dynamic_pointer_cast<BoundCRS>(thisAsCRS);
if (!boundCRS) {
boundCRS = canonicalBoundCRS();
}
if (boundCRS) {
if (boundCRS->hubCRS()->_isEquivalentTo(
GeographicCRS::EPSG_4326.get(),
util::IComparable::Criterion::EQUIVALENT)) {
return NN_NO_CHECK(boundCRS);
}
}
auto geodCRS = util::nn_dynamic_pointer_cast<GeodeticCRS>(thisAsCRS);
auto geogCRS = extractGeographicCRS();
auto hubCRS = util::nn_static_pointer_cast<CRS>(GeographicCRS::EPSG_4326);
if (geodCRS && !geogCRS) {
if (geodCRS->_isEquivalentTo(
GeographicCRS::EPSG_4978.get(),
util::IComparable::Criterion::EQUIVALENT)) {
return thisAsCRS;
}
hubCRS = util::nn_static_pointer_cast<CRS>(GeodeticCRS::EPSG_4978);
} else if (!geogCRS ||
geogCRS->_isEquivalentTo(
GeographicCRS::EPSG_4326.get(),
util::IComparable::Criterion::EQUIVALENT)) {
return thisAsCRS;
} else {
geodCRS = geogCRS;
}
if (!dbContext) {
return thisAsCRS;
}
const auto &l_domains = domains();
metadata::ExtentPtr extent;
if (!l_domains.empty()) {
extent = l_domains[0]->domainOfValidity();
}
std::string crs_authority;
const auto &l_identifiers = identifiers();
// If the object has an authority, restrict the transformations to
// come from that codespace too. This avoids for example EPSG:4269
// (NAD83) to use a (dubious) ESRI transformation.
if (!l_identifiers.empty()) {
crs_authority = *(l_identifiers[0]->codeSpace());
}
auto authorities = dbContext->getAllowedAuthorities(crs_authority, "EPSG");
if (authorities.empty()) {
authorities.emplace_back();
}
for (const auto &authority : authorities) {
try {
auto authFactory = io::AuthorityFactory::create(
NN_NO_CHECK(dbContext),
authority == "any" ? std::string() : authority);
auto ctxt = operation::CoordinateOperationContext::create(
authFactory, extent, 0.0);
ctxt->setAllowUseIntermediateCRS(allowIntermediateCRSUse);
// ctxt->setSpatialCriterion(
// operation::CoordinateOperationContext::SpatialCriterion::PARTIAL_INTERSECTION);
auto list =
operation::CoordinateOperationFactory::create()
->createOperations(NN_NO_CHECK(geodCRS), hubCRS, ctxt);
for (const auto &op : list) {
auto transf =
util::nn_dynamic_pointer_cast<operation::Transformation>(
op);
if (transf && !starts_with(transf->nameStr(), "Ballpark geo")) {
try {
transf->getTOWGS84Parameters();
} catch (const std::exception &) {
continue;
}
return util::nn_static_pointer_cast<CRS>(BoundCRS::create(
thisAsCRS, hubCRS, NN_NO_CHECK(transf)));
} else {
auto concatenated =
dynamic_cast<const operation::ConcatenatedOperation *>(
op.get());
if (concatenated) {
// Case for EPSG:4807 / "NTF (Paris)" that is made of a
// longitude rotation followed by a Helmert
// The prime meridian shift will be accounted elsewhere
const auto &subops = concatenated->operations();
if (subops.size() == 2) {
auto firstOpIsTransformation =
dynamic_cast<const operation::Transformation *>(
subops[0].get());
auto firstOpIsConversion =
dynamic_cast<const operation::Conversion *>(
subops[0].get());
if ((firstOpIsTransformation &&
firstOpIsTransformation
->isLongitudeRotation()) ||
(dynamic_cast<DerivedCRS *>(thisAsCRS.get()) &&
firstOpIsConversion)) {
transf = util::nn_dynamic_pointer_cast<
operation::Transformation>(subops[1]);
if (transf &&
!starts_with(transf->nameStr(),
"Ballpark geo")) {
try {
transf->getTOWGS84Parameters();
} catch (const std::exception &) {
continue;
}
return util::nn_static_pointer_cast<CRS>(
BoundCRS::create(thisAsCRS, hubCRS,
NN_NO_CHECK(transf)));
}
}
}
}
}
}
} catch (const std::exception &) {
}
}
return thisAsCRS;
}
// ---------------------------------------------------------------------------
/** \brief Returns a CRS whose coordinate system does not contain a vertical
* component
*
* @return a CRS.
*/
CRSNNPtr CRS::stripVerticalComponent() const {
auto self = NN_NO_CHECK(
std::dynamic_pointer_cast<CRS>(shared_from_this().as_nullable()));
auto geogCRS = dynamic_cast<const GeographicCRS *>(this);
if (geogCRS) {
const auto &axisList = geogCRS->coordinateSystem()->axisList();
if (axisList.size() == 3) {
auto cs = cs::EllipsoidalCS::create(util::PropertyMap(),
axisList[0], axisList[1]);
return util::nn_static_pointer_cast<CRS>(GeographicCRS::create(
util::PropertyMap().set(common::IdentifiedObject::NAME_KEY,
nameStr()),
geogCRS->datum(), geogCRS->datumEnsemble(), cs));
}
}
auto projCRS = dynamic_cast<const ProjectedCRS *>(this);
if (projCRS) {
const auto &axisList = projCRS->coordinateSystem()->axisList();
if (axisList.size() == 3) {
auto cs = cs::CartesianCS::create(util::PropertyMap(), axisList[0],
axisList[1]);
return util::nn_static_pointer_cast<CRS>(ProjectedCRS::create(
util::PropertyMap().set(common::IdentifiedObject::NAME_KEY,
nameStr()),
projCRS->baseCRS(), projCRS->derivingConversion(), cs));
}
}
return self;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
/** \brief Return a shallow clone of this object. */
CRSNNPtr CRS::shallowClone() const { return _shallowClone(); }
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CRSNNPtr CRS::alterName(const std::string &newName) const {
auto crs = shallowClone();
auto newNameMod(newName);
auto props = util::PropertyMap();
if (ends_with(newNameMod, " (deprecated)")) {
newNameMod.resize(newNameMod.size() - strlen(" (deprecated)"));
props.set(common::IdentifiedObject::DEPRECATED_KEY, true);
}
props.set(common::IdentifiedObject::NAME_KEY, newNameMod);
crs->setProperties(props);
return crs;
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
CRSNNPtr CRS::alterId(const std::string &authName,
const std::string &code) const {
auto crs = shallowClone();
auto props = util::PropertyMap();
props.set(metadata::Identifier::CODESPACE_KEY, authName)
.set(metadata::Identifier::CODE_KEY, code);
crs->setProperties(props);
return crs;
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Identify the CRS with reference CRSs.
*
* The candidate CRSs are either hard-coded, or looked in the database when
* authorityFactory is not null.
*
* The method returns a list of matching reference CRS, and the percentage
* (0-100) of confidence in the match. The list is sorted by decreasing
* confidence.
*
* 100% means that the name of the reference entry
* perfectly matches the CRS name, and both are equivalent. In which case a
* single result is returned.
* 90% means that CRS are equivalent, but the names are not exactly the same.
* 70% means that CRS are equivalent), but the names do not match at all.
* 25% means that the CRS are not equivalent, but there is some similarity in
* the names.
* Other confidence values may be returned by some specialized implementations.
*
* This is implemented for GeodeticCRS, ProjectedCRS, VerticalCRS and
* CompoundCRS.
*
* @param authorityFactory Authority factory (or null, but degraded
* functionality)
* @return a list of matching reference CRS, and the percentage (0-100) of
* confidence in the match.
*/
std::list<std::pair<CRSNNPtr, int>>
CRS::identify(const io::AuthorityFactoryPtr &authorityFactory) const {
return _identify(authorityFactory);
}
// ---------------------------------------------------------------------------
/** \brief Return CRSs that are non-deprecated substitutes for the current CRS.
*/
std::list<CRSNNPtr>
CRS::getNonDeprecated(const io::DatabaseContextNNPtr &dbContext) const {
std::list<CRSNNPtr> res;
const auto &l_identifiers = identifiers();
if (l_identifiers.empty()) {
return res;
}
const char *tableName = nullptr;
if (dynamic_cast<const GeodeticCRS *>(this)) {
tableName = "geodetic_crs";
} else if (dynamic_cast<const ProjectedCRS *>(this)) {
tableName = "projected_crs";
} else if (dynamic_cast<const VerticalCRS *>(this)) {
tableName = "vertical_crs";
} else if (dynamic_cast<const CompoundCRS *>(this)) {
tableName = "compound_crs";
}
if (!tableName) {
return res;
}
const auto &id = l_identifiers[0];
auto tmpRes =
dbContext->getNonDeprecated(tableName, *(id->codeSpace()), id->code());
for (const auto &pair : tmpRes) {
res.emplace_back(io::AuthorityFactory::create(dbContext, pair.first)
->createCoordinateReferenceSystem(pair.second));
}
return res;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
std::list<std::pair<CRSNNPtr, int>>
CRS::_identify(const io::AuthorityFactoryPtr &) const {
return {};
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct SingleCRS::Private {
datum::DatumPtr datum{};
datum::DatumEnsemblePtr datumEnsemble{};
cs::CoordinateSystemNNPtr coordinateSystem;
Private(const datum::DatumPtr &datumIn,
const datum::DatumEnsemblePtr &datumEnsembleIn,
const cs::CoordinateSystemNNPtr &csIn)
: datum(datumIn), datumEnsemble(datumEnsembleIn),
coordinateSystem(csIn) {
if ((datum ? 1 : 0) + (datumEnsemble ? 1 : 0) != 1) {
throw util::Exception("datum or datumEnsemble should be set");
}
}
};
//! @endcond
// ---------------------------------------------------------------------------
SingleCRS::SingleCRS(const datum::DatumPtr &datumIn,
const datum::DatumEnsemblePtr &datumEnsembleIn,
const cs::CoordinateSystemNNPtr &csIn)
: d(internal::make_unique<Private>(datumIn, datumEnsembleIn, csIn)) {}
// ---------------------------------------------------------------------------
SingleCRS::SingleCRS(const SingleCRS &other)
: CRS(other), d(internal::make_unique<Private>(*other.d)) {}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
SingleCRS::~SingleCRS() = default;
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the datum::Datum associated with the CRS.
*
* This might be null, in which case datumEnsemble() return will not be null.
*
* @return a Datum that might be null.
*/
const datum::DatumPtr &SingleCRS::datum() PROJ_PURE_DEFN { return d->datum; }
// ---------------------------------------------------------------------------
/** \brief Return the datum::DatumEnsemble associated with the CRS.
*
* This might be null, in which case datum() return will not be null.
*
* @return a DatumEnsemble that might be null.
*/
const datum::DatumEnsemblePtr &SingleCRS::datumEnsemble() PROJ_PURE_DEFN {
return d->datumEnsemble;
}
// ---------------------------------------------------------------------------
/** \brief Return the cs::CoordinateSystem associated with the CRS.
*
* This might be null, in which case datumEnsemble() return will not be null.
*
* @return a CoordinateSystem that might be null.
*/
const cs::CoordinateSystemNNPtr &SingleCRS::coordinateSystem() PROJ_PURE_DEFN {
return d->coordinateSystem;
}
// ---------------------------------------------------------------------------
bool SingleCRS::baseIsEquivalentTo(
const util::IComparable *other,
util::IComparable::Criterion criterion) const {
auto otherSingleCRS = dynamic_cast<const SingleCRS *>(other);
if (otherSingleCRS == nullptr ||
(criterion == util::IComparable::Criterion::STRICT &&
!ObjectUsage::_isEquivalentTo(other, criterion))) {
return false;
}
const auto &thisDatum = d->datum;
const auto &otherDatum = otherSingleCRS->d->datum;
if (thisDatum) {
if (!thisDatum->_isEquivalentTo(otherDatum.get(), criterion)) {
return false;
}
} else {
if (otherDatum) {
return false;
}
}
// TODO test DatumEnsemble
return d->coordinateSystem->_isEquivalentTo(
otherSingleCRS->d->coordinateSystem.get(), criterion);
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
void SingleCRS::exportDatumOrDatumEnsembleToWkt(
io::WKTFormatter *formatter) const // throw(io::FormattingException)
{
const auto &l_datum = d->datum;
if (l_datum) {
l_datum->_exportToWKT(formatter);
} else {
const auto &l_datumEnsemble = d->datumEnsemble;
assert(l_datumEnsemble);
l_datumEnsemble->_exportToWKT(formatter);
}
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct GeodeticCRS::Private {
std::vector<operation::PointMotionOperationNNPtr> velocityModel{};
datum::GeodeticReferenceFramePtr datum_;
explicit Private(const datum::GeodeticReferenceFramePtr &datumIn)
: datum_(datumIn) {}
};
// ---------------------------------------------------------------------------
static const datum::DatumEnsemblePtr &
checkEnsembleForGeodeticCRS(const datum::GeodeticReferenceFramePtr &datumIn,
const datum::DatumEnsemblePtr &ensemble) {
const char *msg = "One of Datum or DatumEnsemble should be defined";
if (datumIn) {
if (!ensemble) {
return ensemble;
}
msg = "Datum and DatumEnsemble should not be defined";
} else if (ensemble) {
const auto &datums = ensemble->datums();
assert(!datums.empty());
auto grfFirst =
dynamic_cast<datum::GeodeticReferenceFrame *>(datums[0].get());
if (grfFirst) {
return ensemble;
}
msg = "Ensemble should contain GeodeticReferenceFrame";
}
throw util::Exception(msg);
}
//! @endcond
// ---------------------------------------------------------------------------
GeodeticCRS::GeodeticCRS(const datum::GeodeticReferenceFramePtr &datumIn,
const datum::DatumEnsemblePtr &datumEnsembleIn,
const cs::EllipsoidalCSNNPtr &csIn)
: SingleCRS(datumIn, checkEnsembleForGeodeticCRS(datumIn, datumEnsembleIn),
csIn),
d(internal::make_unique<Private>(datumIn)) {}
// ---------------------------------------------------------------------------
GeodeticCRS::GeodeticCRS(const datum::GeodeticReferenceFramePtr &datumIn,
const datum::DatumEnsemblePtr &datumEnsembleIn,
const cs::SphericalCSNNPtr &csIn)
: SingleCRS(datumIn, checkEnsembleForGeodeticCRS(datumIn, datumEnsembleIn),
csIn),
d(internal::make_unique<Private>(datumIn)) {}
// ---------------------------------------------------------------------------
GeodeticCRS::GeodeticCRS(const datum::GeodeticReferenceFramePtr &datumIn,
const datum::DatumEnsemblePtr &datumEnsembleIn,
const cs::CartesianCSNNPtr &csIn)
: SingleCRS(datumIn, checkEnsembleForGeodeticCRS(datumIn, datumEnsembleIn),
csIn),
d(internal::make_unique<Private>(datumIn)) {}
// ---------------------------------------------------------------------------
GeodeticCRS::GeodeticCRS(const GeodeticCRS &other)
: SingleCRS(other), d(internal::make_unique<Private>(*other.d)) {}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
GeodeticCRS::~GeodeticCRS() = default;
//! @endcond
// ---------------------------------------------------------------------------
CRSNNPtr GeodeticCRS::_shallowClone() const {
auto crs(GeodeticCRS::nn_make_shared<GeodeticCRS>(*this));
crs->assignSelf(crs);
return crs;
}
// ---------------------------------------------------------------------------
/** \brief Return the datum::GeodeticReferenceFrame associated with the CRS.
*
* @return a GeodeticReferenceFrame or null (in which case datumEnsemble()
* should return a non-null pointer.)
*/
const datum::GeodeticReferenceFramePtr &GeodeticCRS::datum() PROJ_PURE_DEFN {
return d->datum_;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static datum::GeodeticReferenceFrame *oneDatum(const GeodeticCRS *crs) {
const auto &l_datumEnsemble = crs->datumEnsemble();
assert(l_datumEnsemble);
const auto &l_datums = l_datumEnsemble->datums();
return static_cast<datum::GeodeticReferenceFrame *>(l_datums[0].get());
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the PrimeMeridian associated with the GeodeticReferenceFrame
* or with one of the GeodeticReferenceFrame of the datumEnsemble().
*
* @return the PrimeMeridian.
*/
const datum::PrimeMeridianNNPtr &GeodeticCRS::primeMeridian() PROJ_PURE_DEFN {
if (d->datum_) {
return d->datum_->primeMeridian();
}
return oneDatum(this)->primeMeridian();
}
// ---------------------------------------------------------------------------
/** \brief Return the ellipsoid associated with the GeodeticReferenceFrame
* or with one of the GeodeticReferenceFrame of the datumEnsemble().
*
* @return the PrimeMeridian.
*/
const datum::EllipsoidNNPtr &GeodeticCRS::ellipsoid() PROJ_PURE_DEFN {
if (d->datum_) {
return d->datum_->ellipsoid();
}
return oneDatum(this)->ellipsoid();
}
// ---------------------------------------------------------------------------
/** \brief Return the velocity model associated with the CRS.
*
* @return a velocity model. might be null.
*/
const std::vector<operation::PointMotionOperationNNPtr> &
GeodeticCRS::velocityModel() PROJ_PURE_DEFN {
return d->velocityModel;
}
// ---------------------------------------------------------------------------
/** \brief Return whether the CRS is a geocentric one.
*
* A geocentric CRS is a geodetic CRS that has a Cartesian coordinate system
* with three axis, whose direction is respectively
* cs::AxisDirection::GEOCENTRIC_X,
* cs::AxisDirection::GEOCENTRIC_Y and cs::AxisDirection::GEOCENTRIC_Z.
*
* @return true if the CRS is a geocentric CRS.
*/
bool GeodeticCRS::isGeocentric() PROJ_PURE_DEFN {
const auto &cs = coordinateSystem();
const auto &axisList = cs->axisList();
return axisList.size() == 3 &&
dynamic_cast<cs::CartesianCS *>(cs.get()) != nullptr &&
&axisList[0]->direction() == &cs::AxisDirection::GEOCENTRIC_X &&
&axisList[1]->direction() == &cs::AxisDirection::GEOCENTRIC_Y &&
&axisList[2]->direction() == &cs::AxisDirection::GEOCENTRIC_Z;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate a GeodeticCRS from a datum::GeodeticReferenceFrame and a
* cs::SphericalCS.
*
* @param properties See \ref general_properties.
* At minimum the name should be defined.
* @param datum The datum of the CRS.
* @param cs a SphericalCS.
* @return new GeodeticCRS.
*/
GeodeticCRSNNPtr
GeodeticCRS::create(const util::PropertyMap &properties,
const datum::GeodeticReferenceFrameNNPtr &datum,
const cs::SphericalCSNNPtr &cs) {
return create(properties, datum.as_nullable(), nullptr, cs);
}
// ---------------------------------------------------------------------------
/** \brief Instantiate a GeodeticCRS from a datum::GeodeticReferenceFrame or
* datum::DatumEnsemble and a cs::SphericalCS.
*
* One and only one of datum or datumEnsemble should be set to a non-null value.
*
* @param properties See \ref general_properties.
* At minimum the name should be defined.
* @param datum The datum of the CRS, or nullptr
* @param datumEnsemble The datum ensemble of the CRS, or nullptr.
* @param cs a SphericalCS.
* @return new GeodeticCRS.
*/
GeodeticCRSNNPtr
GeodeticCRS::create(const util::PropertyMap &properties,
const datum::GeodeticReferenceFramePtr &datum,
const datum::DatumEnsemblePtr &datumEnsemble,
const cs::SphericalCSNNPtr &cs) {
auto crs(
GeodeticCRS::nn_make_shared<GeodeticCRS>(datum, datumEnsemble, cs));
crs->assignSelf(crs);
crs->setProperties(properties);
properties.getStringValue("EXTENSION_PROJ4",
crs->CRS::getPrivate()->extensionProj4_);
return crs;
}
// ---------------------------------------------------------------------------