-
Notifications
You must be signed in to change notification settings - Fork 743
/
datum.cpp
2762 lines (2373 loc) · 98.1 KB
/
datum.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/datum.hpp"
#include "proj/common.hpp"
#include "proj/io.hpp"
#include "proj/metadata.hpp"
#include "proj/util.hpp"
#include "proj/internal/datum_internal.hpp"
#include "proj/internal/internal.hpp"
#include "proj/internal/io_internal.hpp"
// PROJ include order is sensitive
// clang-format off
#include "proj.h"
#include "proj_internal.h"
// clang-format on
#include "proj_json_streaming_writer.hpp"
#include <cmath>
#include <cstdlib>
#include <memory>
#include <string>
using namespace NS_PROJ::internal;
#if 0
namespace dropbox{ namespace oxygen {
template<> nn<NS_PROJ::datum::DatumPtr>::~nn() = default;
template<> nn<NS_PROJ::datum::DatumEnsemblePtr>::~nn() = default;
template<> nn<NS_PROJ::datum::PrimeMeridianPtr>::~nn() = default;
template<> nn<NS_PROJ::datum::EllipsoidPtr>::~nn() = default;
template<> nn<NS_PROJ::datum::GeodeticReferenceFramePtr>::~nn() = default;
template<> nn<NS_PROJ::datum::DynamicGeodeticReferenceFramePtr>::~nn() = default;
template<> nn<NS_PROJ::datum::VerticalReferenceFramePtr>::~nn() = default;
template<> nn<NS_PROJ::datum::DynamicVerticalReferenceFramePtr>::~nn() = default;
template<> nn<NS_PROJ::datum::EngineeringDatumPtr>::~nn() = default;
template<> nn<NS_PROJ::datum::TemporalDatumPtr>::~nn() = default;
template<> nn<NS_PROJ::datum::ParametricDatumPtr>::~nn() = default;
}}
#endif
NS_PROJ_START
namespace datum {
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static util::PropertyMap createMapNameEPSGCode(const char *name, int code) {
return util::PropertyMap()
.set(common::IdentifiedObject::NAME_KEY, name)
.set(metadata::Identifier::CODESPACE_KEY, metadata::Identifier::EPSG)
.set(metadata::Identifier::CODE_KEY, code);
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct Datum::Private {
util::optional<std::string> anchorDefinition{};
std::shared_ptr<util::optional<common::Measure>> anchorEpoch =
std::make_shared<util::optional<common::Measure>>();
util::optional<common::DateTime> publicationDate{};
common::IdentifiedObjectPtr conventionalRS{};
// cppcheck-suppress functionStatic
void exportAnchorDefinition(io::WKTFormatter *formatter) const;
// cppcheck-suppress functionStatic
void exportAnchorEpoch(io::WKTFormatter *formatter) const;
// cppcheck-suppress functionStatic
void exportAnchorDefinition(io::JSONFormatter *formatter) const;
// cppcheck-suppress functionStatic
void exportAnchorEpoch(io::JSONFormatter *formatter) const;
};
// ---------------------------------------------------------------------------
void Datum::Private::exportAnchorDefinition(io::WKTFormatter *formatter) const {
if (anchorDefinition) {
formatter->startNode(io::WKTConstants::ANCHOR, false);
formatter->addQuotedString(*anchorDefinition);
formatter->endNode();
}
}
// ---------------------------------------------------------------------------
// Avoid rounding issues due to year -> second (SI unit) -> year roundtrips
static double getRoundedEpochInDecimalYear(double year) {
// Try to see if the value is close to xxxx.yyy decimal year.
if (std::fabs(1000 * year - std::round(1000 * year)) <= 1e-3) {
year = std::round(1000 * year) / 1000.0;
}
return year;
}
// ---------------------------------------------------------------------------
void Datum::Private::exportAnchorEpoch(io::WKTFormatter *formatter) const {
if (anchorEpoch->has_value()) {
formatter->startNode(io::WKTConstants::ANCHOREPOCH, false);
const double year =
(*anchorEpoch)->convertToUnit(common::UnitOfMeasure::YEAR);
formatter->add(getRoundedEpochInDecimalYear(year));
formatter->endNode();
}
}
// ---------------------------------------------------------------------------
void Datum::Private::exportAnchorDefinition(
io::JSONFormatter *formatter) const {
if (anchorDefinition) {
auto writer = formatter->writer();
writer->AddObjKey("anchor");
writer->Add(*anchorDefinition);
}
}
// ---------------------------------------------------------------------------
void Datum::Private::exportAnchorEpoch(io::JSONFormatter *formatter) const {
if (anchorEpoch->has_value()) {
auto writer = formatter->writer();
writer->AddObjKey("anchor_epoch");
const double year =
(*anchorEpoch)->convertToUnit(common::UnitOfMeasure::YEAR);
writer->Add(getRoundedEpochInDecimalYear(year));
}
}
//! @endcond
// ---------------------------------------------------------------------------
Datum::Datum() : d(internal::make_unique<Private>()) {}
// ---------------------------------------------------------------------------
#ifdef notdef
Datum::Datum(const Datum &other)
: ObjectUsage(other), d(internal::make_unique<Private>(*other.d)) {}
#endif
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
Datum::~Datum() = default;
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the anchor definition.
*
* A description - possibly including coordinates of an identified point or
* points - of the relationship used to anchor a coordinate system to the
* Earth or alternate object.
* <ul>
* <li>For modern geodetic reference frames the anchor may be a set of station
* coordinates; if the reference frame is dynamic it will also include
* coordinate velocities. For a traditional geodetic datum, this anchor may be
* a point known as the fundamental point, which is traditionally the point
* where the relationship between geoid and ellipsoid is defined, together
* with a direction from that point.</li>
* <li>For a vertical reference frame the anchor may be the zero level at one
* or more defined locations or a conventionally defined surface.</li>
* <li>For an engineering datum, the anchor may be an identified physical point
* with the orientation defined relative to the object.</li>
* </ul>
*
* @return the anchor definition, or empty.
*/
const util::optional<std::string> &Datum::anchorDefinition() const {
return d->anchorDefinition;
}
// ---------------------------------------------------------------------------
/** \brief Return the anchor epoch.
*
* Epoch at which a static reference frame matches a dynamic reference frame
* from which it has been derived.
*
* Note: Not to be confused with the frame reference epoch of dynamic geodetic
* and dynamic vertical reference frames. Nor with the epoch at which a
* reference frame is defined to be aligned with another reference frame;
* this information should be included in the datum anchor definition.
*
* @return the anchor epoch, or empty.
* @since 9.2
*/
const util::optional<common::Measure> &Datum::anchorEpoch() const {
return *(d->anchorEpoch);
}
// ---------------------------------------------------------------------------
/** \brief Return the date on which the datum definition was published.
*
* \note Departure from \ref ISO_19111_2019 : we return a DateTime instead of
* a Citation::Date.
*
* @return the publication date, or empty.
*/
const util::optional<common::DateTime> &Datum::publicationDate() const {
return d->publicationDate;
}
// ---------------------------------------------------------------------------
/** \brief Return the conventional reference system.
*
* This is the name, identifier, alias and remarks for the terrestrial
* reference system or vertical reference system realized by this reference
* frame, for example "ITRS" for ITRF88 through ITRF2008 and ITRF2014, or
* "EVRS" for EVRF2000 and EVRF2007.
*
* @return the conventional reference system, or nullptr.
*/
const common::IdentifiedObjectPtr &Datum::conventionalRS() const {
return d->conventionalRS;
}
// ---------------------------------------------------------------------------
void Datum::setAnchor(const util::optional<std::string> &anchor) {
d->anchorDefinition = anchor;
}
// ---------------------------------------------------------------------------
void Datum::setAnchorEpoch(const util::optional<common::Measure> &anchorEpoch) {
d->anchorEpoch =
std::make_shared<util::optional<common::Measure>>(anchorEpoch);
}
// ---------------------------------------------------------------------------
void Datum::setProperties(
const util::PropertyMap &properties) // throw(InvalidValueTypeException)
{
std::string publicationDateResult;
properties.getStringValue("PUBLICATION_DATE", publicationDateResult);
if (!publicationDateResult.empty()) {
d->publicationDate = common::DateTime::create(publicationDateResult);
}
ObjectUsage::setProperties(properties);
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
bool Datum::_isEquivalentTo(const util::IComparable *other,
util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
auto otherDatum = dynamic_cast<const Datum *>(other);
if (otherDatum == nullptr ||
!ObjectUsage::_isEquivalentTo(other, criterion, dbContext)) {
return false;
}
if (criterion == util::IComparable::Criterion::STRICT) {
if ((anchorDefinition().has_value() ^
otherDatum->anchorDefinition().has_value())) {
return false;
}
if (anchorDefinition().has_value() &&
otherDatum->anchorDefinition().has_value() &&
*anchorDefinition() != *otherDatum->anchorDefinition()) {
return false;
}
if ((publicationDate().has_value() ^
otherDatum->publicationDate().has_value())) {
return false;
}
if (publicationDate().has_value() &&
otherDatum->publicationDate().has_value() &&
publicationDate()->toString() !=
otherDatum->publicationDate()->toString()) {
return false;
}
if (((conventionalRS() != nullptr) ^
(otherDatum->conventionalRS() != nullptr))) {
return false;
}
if (conventionalRS() && otherDatum->conventionalRS() &&
conventionalRS()->_isEquivalentTo(
otherDatum->conventionalRS().get(), criterion, dbContext)) {
return false;
}
}
return true;
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct PrimeMeridian::Private {
common::Angle longitude_{};
explicit Private(const common::Angle &longitude) : longitude_(longitude) {}
};
//! @endcond
// ---------------------------------------------------------------------------
PrimeMeridian::PrimeMeridian(const common::Angle &longitudeIn)
: d(internal::make_unique<Private>(longitudeIn)) {}
// ---------------------------------------------------------------------------
#ifdef notdef
PrimeMeridian::PrimeMeridian(const PrimeMeridian &other)
: common::IdentifiedObject(other),
d(internal::make_unique<Private>(*other.d)) {}
#endif
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
PrimeMeridian::~PrimeMeridian() = default;
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the longitude of the prime meridian.
*
* It is measured from the internationally-recognised reference meridian
* ('Greenwich meridian'), positive eastward.
* The default value is 0 degrees.
*
* @return the longitude of the prime meridian.
*/
const common::Angle &PrimeMeridian::longitude() PROJ_PURE_DEFN {
return d->longitude_;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate a PrimeMeridian.
*
* @param properties See \ref general_properties.
* At minimum the name should be defined.
* @param longitudeIn the longitude of the prime meridian.
* @return new PrimeMeridian.
*/
PrimeMeridianNNPtr PrimeMeridian::create(const util::PropertyMap &properties,
const common::Angle &longitudeIn) {
auto pm(PrimeMeridian::nn_make_shared<PrimeMeridian>(longitudeIn));
pm->setProperties(properties);
return pm;
}
// ---------------------------------------------------------------------------
const PrimeMeridianNNPtr PrimeMeridian::createGREENWICH() {
return create(createMapNameEPSGCode("Greenwich", 8901), common::Angle(0));
}
// ---------------------------------------------------------------------------
const PrimeMeridianNNPtr PrimeMeridian::createREFERENCE_MERIDIAN() {
return create(util::PropertyMap().set(IdentifiedObject::NAME_KEY,
"Reference meridian"),
common::Angle(0));
}
// ---------------------------------------------------------------------------
const PrimeMeridianNNPtr PrimeMeridian::createPARIS() {
return create(createMapNameEPSGCode("Paris", 8903),
common::Angle(2.5969213, common::UnitOfMeasure::GRAD));
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
void PrimeMeridian::_exportToWKT(
io::WKTFormatter *formatter) const // throw(FormattingException)
{
const bool isWKT2 = formatter->version() == io::WKTFormatter::Version::WKT2;
std::string l_name =
name()->description().has_value() ? nameStr() : "Greenwich";
if (!(isWKT2 && formatter->primeMeridianOmittedIfGreenwich() &&
l_name == "Greenwich")) {
formatter->startNode(io::WKTConstants::PRIMEM, !identifiers().empty());
if (formatter->useESRIDialect()) {
bool aliasFound = false;
const auto &dbContext = formatter->databaseContext();
if (dbContext) {
auto l_alias = dbContext->getAliasFromOfficialName(
l_name, "prime_meridian", "ESRI");
if (!l_alias.empty()) {
l_name = l_alias;
aliasFound = true;
}
}
if (!aliasFound && dbContext) {
auto authFactory = io::AuthorityFactory::create(
NN_NO_CHECK(dbContext), "ESRI");
aliasFound =
authFactory
->createObjectsFromName(
l_name,
{io::AuthorityFactory::ObjectType::PRIME_MERIDIAN},
false // approximateMatch
)
.size() == 1;
}
if (!aliasFound) {
l_name = io::WKTFormatter::morphNameToESRI(l_name);
}
}
formatter->addQuotedString(l_name);
const auto &l_long = longitude();
if (formatter->primeMeridianInDegree()) {
formatter->add(l_long.convertToUnit(common::UnitOfMeasure::DEGREE));
} else {
formatter->add(l_long.value());
}
const auto &unit = l_long.unit();
if (isWKT2) {
if (!(formatter
->primeMeridianOrParameterUnitOmittedIfSameAsAxis() &&
unit == *(formatter->axisAngularUnit()))) {
unit._exportToWKT(formatter, io::WKTConstants::ANGLEUNIT);
}
} else if (!formatter->primeMeridianInDegree()) {
unit._exportToWKT(formatter);
}
if (formatter->outputId()) {
formatID(formatter);
}
formatter->endNode();
}
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
void PrimeMeridian::_exportToJSON(
io::JSONFormatter *formatter) const // throw(FormattingException)
{
auto writer = formatter->writer();
auto objectContext(
formatter->MakeObjectContext("PrimeMeridian", !identifiers().empty()));
writer->AddObjKey("name");
std::string l_name =
name()->description().has_value() ? nameStr() : "Greenwich";
writer->Add(l_name);
const auto &l_long = longitude();
writer->AddObjKey("longitude");
const auto &unit = l_long.unit();
if (unit == common::UnitOfMeasure::DEGREE) {
writer->Add(l_long.value(), 15);
} else {
auto longitudeContext(formatter->MakeObjectContext(nullptr, false));
writer->AddObjKey("value");
writer->Add(l_long.value(), 15);
writer->AddObjKey("unit");
unit._exportToJSON(formatter);
}
if (formatter->outputId()) {
formatID(formatter);
}
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
std::string
PrimeMeridian::getPROJStringWellKnownName(const common::Angle &angle) {
const double valRad = angle.getSIValue();
std::string projPMName;
PJ_CONTEXT *ctxt = proj_context_create();
auto proj_pm = proj_list_prime_meridians();
for (int i = 0; proj_pm[i].id != nullptr; ++i) {
double valRefRad = dmstor_ctx(ctxt, proj_pm[i].defn, nullptr);
if (::fabs(valRad - valRefRad) < 1e-10) {
projPMName = proj_pm[i].id;
break;
}
}
proj_context_destroy(ctxt);
return projPMName;
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
void PrimeMeridian::_exportToPROJString(
io::PROJStringFormatter *formatter) const // throw(FormattingException)
{
if (longitude().getSIValue() != 0) {
std::string projPMName(getPROJStringWellKnownName(longitude()));
if (!projPMName.empty()) {
formatter->addParam("pm", projPMName);
} else {
const double valDeg =
longitude().convertToUnit(common::UnitOfMeasure::DEGREE);
formatter->addParam("pm", valDeg);
}
}
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
bool PrimeMeridian::_isEquivalentTo(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
auto otherPM = dynamic_cast<const PrimeMeridian *>(other);
if (otherPM == nullptr ||
!IdentifiedObject::_isEquivalentTo(other, criterion, dbContext)) {
return false;
}
// In MapInfo, the Paris prime meridian is returned as 2.3372291666667
// instead of the official value of 2.33722917, which is a relative
// error in the 1e-9 range.
return longitude()._isEquivalentTo(otherPM->longitude(), criterion, 1e-8);
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
struct Ellipsoid::Private {
common::Length semiMajorAxis_{};
util::optional<common::Scale> inverseFlattening_{};
util::optional<common::Length> semiMinorAxis_{};
util::optional<common::Length> semiMedianAxis_{};
std::string celestialBody_{};
explicit Private(const common::Length &radius,
const std::string &celestialBody)
: semiMajorAxis_(radius), celestialBody_(celestialBody) {}
Private(const common::Length &semiMajorAxisIn,
const common::Scale &invFlattening,
const std::string &celestialBody)
: semiMajorAxis_(semiMajorAxisIn), inverseFlattening_(invFlattening),
celestialBody_(celestialBody) {}
Private(const common::Length &semiMajorAxisIn,
const common::Length &semiMinorAxisIn,
const std::string &celestialBody)
: semiMajorAxis_(semiMajorAxisIn), semiMinorAxis_(semiMinorAxisIn),
celestialBody_(celestialBody) {}
};
//! @endcond
// ---------------------------------------------------------------------------
Ellipsoid::Ellipsoid(const common::Length &radius,
const std::string &celestialBodyIn)
: d(internal::make_unique<Private>(radius, celestialBodyIn)) {}
// ---------------------------------------------------------------------------
Ellipsoid::Ellipsoid(const common::Length &semiMajorAxisIn,
const common::Scale &invFlattening,
const std::string &celestialBodyIn)
: d(internal::make_unique<Private>(semiMajorAxisIn, invFlattening,
celestialBodyIn)) {}
// ---------------------------------------------------------------------------
Ellipsoid::Ellipsoid(const common::Length &semiMajorAxisIn,
const common::Length &semiMinorAxisIn,
const std::string &celestialBodyIn)
: d(internal::make_unique<Private>(semiMajorAxisIn, semiMinorAxisIn,
celestialBodyIn)) {}
// ---------------------------------------------------------------------------
#ifdef notdef
Ellipsoid::Ellipsoid(const Ellipsoid &other)
: common::IdentifiedObject(other),
d(internal::make_unique<Private>(*other.d)) {}
#endif
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
Ellipsoid::~Ellipsoid() = default;
Ellipsoid::Ellipsoid(const Ellipsoid &other)
: IdentifiedObject(other), d(internal::make_unique<Private>(*(other.d))) {}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return the length of the semi-major axis of the ellipsoid.
*
* @return the semi-major axis.
*/
const common::Length &Ellipsoid::semiMajorAxis() PROJ_PURE_DEFN {
return d->semiMajorAxis_;
}
// ---------------------------------------------------------------------------
/** \brief Return the inverse flattening value of the ellipsoid, if the
* ellipsoid
* has been defined with this value.
*
* @see computeInverseFlattening() that will always return a valid value of the
* inverse flattening, whether the ellipsoid has been defined through inverse
* flattening or semi-minor axis.
*
* @return the inverse flattening value of the ellipsoid, or empty.
*/
const util::optional<common::Scale> &
Ellipsoid::inverseFlattening() PROJ_PURE_DEFN {
return d->inverseFlattening_;
}
// ---------------------------------------------------------------------------
/** \brief Return the length of the semi-minor axis of the ellipsoid, if the
* ellipsoid
* has been defined with this value.
*
* @see computeSemiMinorAxis() that will always return a valid value of the
* semi-minor axis, whether the ellipsoid has been defined through inverse
* flattening or semi-minor axis.
*
* @return the semi-minor axis of the ellipsoid, or empty.
*/
const util::optional<common::Length> &
Ellipsoid::semiMinorAxis() PROJ_PURE_DEFN {
return d->semiMinorAxis_;
}
// ---------------------------------------------------------------------------
/** \brief Return whether the ellipsoid is spherical.
*
* That is to say is semiMajorAxis() == computeSemiMinorAxis().
*
* A sphere is completely defined by the semi-major axis, which is the radius
* of the sphere.
*
* @return true if the ellipsoid is spherical.
*/
bool Ellipsoid::isSphere() PROJ_PURE_DEFN {
if (d->inverseFlattening_.has_value()) {
return d->inverseFlattening_->value() == 0;
}
if (semiMinorAxis().has_value()) {
return semiMajorAxis() == *semiMinorAxis();
}
return true;
}
// ---------------------------------------------------------------------------
/** \brief Return the length of the semi-median axis of a triaxial ellipsoid
*
* This parameter is not required for a biaxial ellipsoid.
*
* @return the semi-median axis of the ellipsoid, or empty.
*/
const util::optional<common::Length> &
Ellipsoid::semiMedianAxis() PROJ_PURE_DEFN {
return d->semiMedianAxis_;
}
// ---------------------------------------------------------------------------
/** \brief Return or compute the inverse flattening value of the ellipsoid.
*
* If computed, the inverse flattening is the result of a / (a - b),
* where a is the semi-major axis and b the semi-minor axis.
*
* @return the inverse flattening value of the ellipsoid, or 0 for a sphere.
*/
double Ellipsoid::computedInverseFlattening() PROJ_PURE_DEFN {
if (d->inverseFlattening_.has_value()) {
return d->inverseFlattening_->getSIValue();
}
if (d->semiMinorAxis_.has_value()) {
const double a = d->semiMajorAxis_.getSIValue();
const double b = d->semiMinorAxis_->getSIValue();
return (a == b) ? 0.0 : a / (a - b);
}
return 0.0;
}
// ---------------------------------------------------------------------------
/** \brief Return the squared eccentricity of the ellipsoid.
*
* @return the squared eccentricity, or a negative value if invalid.
*/
double Ellipsoid::squaredEccentricity() PROJ_PURE_DEFN {
const double rf = computedInverseFlattening();
const double f = rf != 0.0 ? 1. / rf : 0.0;
const double e2 = f * (2 - f);
return e2;
}
// ---------------------------------------------------------------------------
/** \brief Return or compute the length of the semi-minor axis of the ellipsoid.
*
* If computed, the semi-minor axis is the result of a * (1 - 1 / rf)
* where a is the semi-major axis and rf the reverse/inverse flattening.
* @return the semi-minor axis of the ellipsoid.
*/
common::Length Ellipsoid::computeSemiMinorAxis() const {
if (d->semiMinorAxis_.has_value()) {
return *d->semiMinorAxis_;
}
if (inverseFlattening().has_value()) {
return common::Length(
(1.0 - 1.0 / d->inverseFlattening_->getSIValue()) *
d->semiMajorAxis_.value(),
d->semiMajorAxis_.unit());
}
return d->semiMajorAxis_;
}
// ---------------------------------------------------------------------------
/** \brief Return the name of the celestial body on which the ellipsoid refers
* to.
*/
const std::string &Ellipsoid::celestialBody() PROJ_PURE_DEFN {
return d->celestialBody_;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate a Ellipsoid as a sphere.
*
* @param properties See \ref general_properties.
* At minimum the name should be defined.
* @param radius the sphere radius (semi-major axis).
* @param celestialBody Name of the celestial body on which the ellipsoid refers
* to.
* @return new Ellipsoid.
*/
EllipsoidNNPtr Ellipsoid::createSphere(const util::PropertyMap &properties,
const common::Length &radius,
const std::string &celestialBody) {
auto ellipsoid(Ellipsoid::nn_make_shared<Ellipsoid>(radius, celestialBody));
ellipsoid->setProperties(properties);
return ellipsoid;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate a Ellipsoid from its inverse/reverse flattening.
*
* @param properties See \ref general_properties.
* At minimum the name should be defined.
* @param semiMajorAxisIn the semi-major axis.
* @param invFlattening the inverse/reverse flattening. If set to 0, this will
* be considered as a sphere.
* @param celestialBody Name of the celestial body on which the ellipsoid refers
* to.
* @return new Ellipsoid.
*/
EllipsoidNNPtr Ellipsoid::createFlattenedSphere(
const util::PropertyMap &properties, const common::Length &semiMajorAxisIn,
const common::Scale &invFlattening, const std::string &celestialBody) {
auto ellipsoid(invFlattening.value() == 0
? Ellipsoid::nn_make_shared<Ellipsoid>(semiMajorAxisIn,
celestialBody)
: Ellipsoid::nn_make_shared<Ellipsoid>(
semiMajorAxisIn, invFlattening, celestialBody));
ellipsoid->setProperties(properties);
return ellipsoid;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate a Ellipsoid from the value of its two semi axis.
*
* @param properties See \ref general_properties.
* At minimum the name should be defined.
* @param semiMajorAxisIn the semi-major axis.
* @param semiMinorAxisIn the semi-minor axis.
* @param celestialBody Name of the celestial body on which the ellipsoid refers
* to.
* @return new Ellipsoid.
*/
EllipsoidNNPtr Ellipsoid::createTwoAxis(const util::PropertyMap &properties,
const common::Length &semiMajorAxisIn,
const common::Length &semiMinorAxisIn,
const std::string &celestialBody) {
auto ellipsoid(Ellipsoid::nn_make_shared<Ellipsoid>(
semiMajorAxisIn, semiMinorAxisIn, celestialBody));
ellipsoid->setProperties(properties);
return ellipsoid;
}
// ---------------------------------------------------------------------------
const EllipsoidNNPtr Ellipsoid::createCLARKE_1866() {
return createTwoAxis(createMapNameEPSGCode("Clarke 1866", 7008),
common::Length(6378206.4), common::Length(6356583.8));
}
// ---------------------------------------------------------------------------
const EllipsoidNNPtr Ellipsoid::createWGS84() {
return createFlattenedSphere(createMapNameEPSGCode("WGS 84", 7030),
common::Length(6378137),
common::Scale(298.257223563));
}
// ---------------------------------------------------------------------------
const EllipsoidNNPtr Ellipsoid::createGRS1980() {
return createFlattenedSphere(createMapNameEPSGCode("GRS 1980", 7019),
common::Length(6378137),
common::Scale(298.257222101));
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
void Ellipsoid::_exportToWKT(
io::WKTFormatter *formatter) const // throw(FormattingException)
{
const bool isWKT2 = formatter->version() == io::WKTFormatter::Version::WKT2;
formatter->startNode(isWKT2 ? io::WKTConstants::ELLIPSOID
: io::WKTConstants::SPHEROID,
!identifiers().empty());
{
auto l_name = nameStr();
if (l_name.empty()) {
formatter->addQuotedString("unnamed");
} else {
if (formatter->useESRIDialect()) {
if (l_name == "WGS 84") {
l_name = "WGS_1984";
} else {
bool aliasFound = false;
const auto &dbContext = formatter->databaseContext();
if (dbContext) {
auto l_alias = dbContext->getAliasFromOfficialName(
l_name, "ellipsoid", "ESRI");
if (!l_alias.empty()) {
l_name = l_alias;
aliasFound = true;
}
}
if (!aliasFound && dbContext) {
auto authFactory = io::AuthorityFactory::create(
NN_NO_CHECK(dbContext), "ESRI");
aliasFound = authFactory
->createObjectsFromName(
l_name,
{io::AuthorityFactory::ObjectType::
ELLIPSOID},
false // approximateMatch
)
.size() == 1;
}
if (!aliasFound) {
l_name = io::WKTFormatter::morphNameToESRI(l_name);
}
}
}
formatter->addQuotedString(l_name);
}
const auto &semiMajor = semiMajorAxis();
if (isWKT2) {
formatter->add(semiMajor.value());
} else {
formatter->add(semiMajor.getSIValue());
}
formatter->add(computedInverseFlattening());
const auto &unit = semiMajor.unit();
if (isWKT2 && !(formatter->ellipsoidUnitOmittedIfMetre() &&
unit == common::UnitOfMeasure::METRE)) {
unit._exportToWKT(formatter, io::WKTConstants::LENGTHUNIT);
}
if (formatter->outputId()) {
formatID(formatter);
}
}
formatter->endNode();
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
void Ellipsoid::_exportToJSON(
io::JSONFormatter *formatter) const // throw(FormattingException)
{
auto writer = formatter->writer();
auto objectContext(
formatter->MakeObjectContext("Ellipsoid", !identifiers().empty()));
writer->AddObjKey("name");
auto l_name = nameStr();
if (l_name.empty()) {
writer->Add("unnamed");
} else {
writer->Add(l_name);
}
const auto &semiMajor = semiMajorAxis();
const auto &semiMajorUnit = semiMajor.unit();
writer->AddObjKey(isSphere() ? "radius" : "semi_major_axis");
if (semiMajorUnit == common::UnitOfMeasure::METRE) {
writer->Add(semiMajor.value(), 15);
} else {
auto objContext(formatter->MakeObjectContext(nullptr, false));
writer->AddObjKey("value");
writer->Add(semiMajor.value(), 15);
writer->AddObjKey("unit");
semiMajorUnit._exportToJSON(formatter);
}
if (!isSphere()) {
const auto &l_inverseFlattening = inverseFlattening();
if (l_inverseFlattening.has_value()) {
writer->AddObjKey("inverse_flattening");
writer->Add(l_inverseFlattening->getSIValue(), 15);
} else {
writer->AddObjKey("semi_minor_axis");
const auto &l_semiMinorAxis(semiMinorAxis());
const auto &semiMinorAxisUnit(l_semiMinorAxis->unit());
if (semiMinorAxisUnit == common::UnitOfMeasure::METRE) {
writer->Add(l_semiMinorAxis->value(), 15);
} else {
auto objContext(formatter->MakeObjectContext(nullptr, false));
writer->AddObjKey("value");
writer->Add(l_semiMinorAxis->value(), 15);
writer->AddObjKey("unit");
semiMinorAxisUnit._exportToJSON(formatter);
}
}
}
if (formatter->outputId()) {
formatID(formatter);
}