Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 9.0] Fix comparison of GeodeticRefrenceFrame vs DynamicGeodeticReferenceFrame #3120

Merged
merged 1 commit into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/proj/datum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ class PROJ_GCC_DLL GeodeticReferenceFrame : public Datum {
util::IComparable::Criterion criterion =
util::IComparable::Criterion::STRICT,
const io::DatabaseContextPtr &dbContext = nullptr) const override;

PROJ_INTERNAL bool isEquivalentToNoExactTypeCheck(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const;
//! @endcond

protected:
Expand Down Expand Up @@ -587,6 +591,10 @@ class PROJ_GCC_DLL VerticalReferenceFrame : public Datum {
util::IComparable::Criterion::STRICT,
const io::DatabaseContextPtr &dbContext = nullptr) const override;

PROJ_INTERNAL bool isEquivalentToNoExactTypeCheck(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const;

PROJ_INTERNAL void _exportToWKT(io::WKTFormatter *formatter)
const override; // throw(io::FormattingException)

Expand Down
61 changes: 53 additions & 8 deletions src/iso19111/datum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,8 @@ void GeodeticReferenceFrame::_exportToJSON(
// ---------------------------------------------------------------------------

//! @cond Doxygen_Suppress
bool GeodeticReferenceFrame::_isEquivalentTo(

bool GeodeticReferenceFrame::isEquivalentToNoExactTypeCheck(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
auto otherGRF = dynamic_cast<const GeodeticReferenceFrame *>(other);
Expand All @@ -1427,6 +1428,19 @@ bool GeodeticReferenceFrame::_isEquivalentTo(
ellipsoid()->_isEquivalentTo(otherGRF->ellipsoid().get(), criterion,
dbContext);
}

// ---------------------------------------------------------------------------

bool GeodeticReferenceFrame::_isEquivalentTo(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
if (criterion == Criterion::STRICT &&
!util::isOfExactType<GeodeticReferenceFrame>(*other)) {
return false;
}
return isEquivalentToNoExactTypeCheck(other, criterion, dbContext);
}

//! @endcond

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1558,11 +1572,20 @@ DynamicGeodeticReferenceFrame::deformationModelName() const {
bool DynamicGeodeticReferenceFrame::_isEquivalentTo(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
auto otherDGRF = dynamic_cast<const DynamicGeodeticReferenceFrame *>(other);
if (otherDGRF == nullptr ||
!GeodeticReferenceFrame::_isEquivalentTo(other, criterion, dbContext)) {
if (criterion == Criterion::STRICT &&
!util::isOfExactType<DynamicGeodeticReferenceFrame>(*other)) {
return false;
}
if (!GeodeticReferenceFrame::isEquivalentToNoExactTypeCheck(
other, criterion, dbContext)) {
return false;
}
auto otherDGRF = dynamic_cast<const DynamicGeodeticReferenceFrame *>(other);
if (otherDGRF == nullptr) {
// we can go here only if criterion != Criterion::STRICT, and thus
// given the above check we can consider the objects equivalent.
return true;
}
return frameReferenceEpoch()._isEquivalentTo(
otherDGRF->frameReferenceEpoch(), criterion) &&
metadata::Identifier::isEquivalentName(
Expand Down Expand Up @@ -2101,7 +2124,7 @@ void VerticalReferenceFrame::_exportToJSON(
// ---------------------------------------------------------------------------

//! @cond Doxygen_Suppress
bool VerticalReferenceFrame::_isEquivalentTo(
bool VerticalReferenceFrame::isEquivalentToNoExactTypeCheck(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
auto otherVRF = dynamic_cast<const VerticalReferenceFrame *>(other);
Expand All @@ -2121,6 +2144,19 @@ bool VerticalReferenceFrame::_isEquivalentTo(
}
return true;
}

// ---------------------------------------------------------------------------

bool VerticalReferenceFrame::_isEquivalentTo(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
if (criterion == Criterion::STRICT &&
!util::isOfExactType<VerticalReferenceFrame>(*other)) {
return false;
}
return isEquivalentToNoExactTypeCheck(other, criterion, dbContext);
}

//! @endcond

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -2195,11 +2231,20 @@ DynamicVerticalReferenceFrame::deformationModelName() const {
bool DynamicVerticalReferenceFrame::_isEquivalentTo(
const util::IComparable *other, util::IComparable::Criterion criterion,
const io::DatabaseContextPtr &dbContext) const {
auto otherDGRF = dynamic_cast<const DynamicVerticalReferenceFrame *>(other);
if (otherDGRF == nullptr ||
!VerticalReferenceFrame::_isEquivalentTo(other, criterion, dbContext)) {
if (criterion == Criterion::STRICT &&
!util::isOfExactType<DynamicVerticalReferenceFrame>(*other)) {
return false;
}
if (!VerticalReferenceFrame::isEquivalentToNoExactTypeCheck(
other, criterion, dbContext)) {
return false;
}
auto otherDGRF = dynamic_cast<const DynamicVerticalReferenceFrame *>(other);
if (otherDGRF == nullptr) {
// we can go here only if criterion != Criterion::STRICT, and thus
// given the above check we can consider the objects equivalent.
return true;
}
return frameReferenceEpoch()._isEquivalentTo(
otherDGRF->frameReferenceEpoch(), criterion) &&
metadata::Identifier::isEquivalentName(
Expand Down
53 changes: 53 additions & 0 deletions test/unit/test_datum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,33 @@ TEST(datum, dynamic_geodetic_reference_frame) {
drf->exportToWKT(
WKTFormatter::create(WKTFormatter::Convention::WKT2_2019).get()),
expected_wtk2_2019);

EXPECT_TRUE(drf->isEquivalentTo(drf.get()));
EXPECT_TRUE(
drf->isEquivalentTo(drf.get(), IComparable::Criterion::EQUIVALENT));
EXPECT_FALSE(drf->isEquivalentTo(createUnrelatedObject().get()));

// "Same" datum, except that it is a non-dynamic one
auto datum = GeodeticReferenceFrame::create(
PropertyMap().set(IdentifiedObject::NAME_KEY, "test"), Ellipsoid::WGS84,
optional<std::string>("My anchor"), PrimeMeridian::GREENWICH);
EXPECT_FALSE(datum->isEquivalentTo(drf.get()));
EXPECT_FALSE(drf->isEquivalentTo(datum.get()));
EXPECT_TRUE(
datum->isEquivalentTo(drf.get(), IComparable::Criterion::EQUIVALENT));
EXPECT_TRUE(
drf->isEquivalentTo(datum.get(), IComparable::Criterion::EQUIVALENT));

auto unrelated_datum = GeodeticReferenceFrame::create(
PropertyMap().set(IdentifiedObject::NAME_KEY, "test2"),
Ellipsoid::WGS84, optional<std::string>("My anchor"),
PrimeMeridian::GREENWICH);
EXPECT_FALSE(unrelated_datum->isEquivalentTo(drf.get()));
EXPECT_FALSE(drf->isEquivalentTo(unrelated_datum.get()));
EXPECT_FALSE(unrelated_datum->isEquivalentTo(
drf.get(), IComparable::Criterion::EQUIVALENT));
EXPECT_FALSE(drf->isEquivalentTo(unrelated_datum.get(),
IComparable::Criterion::EQUIVALENT));
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -395,6 +422,32 @@ TEST(datum, dynamic_vertical_reference_frame) {
drf->exportToWKT(
WKTFormatter::create(WKTFormatter::Convention::WKT2_2019).get()),
expected_wtk2_2019);

EXPECT_TRUE(drf->isEquivalentTo(drf.get()));
EXPECT_TRUE(
drf->isEquivalentTo(drf.get(), IComparable::Criterion::EQUIVALENT));
EXPECT_FALSE(drf->isEquivalentTo(createUnrelatedObject().get()));

// "Same" datum, except that it is a non-dynamic one
auto datum = VerticalReferenceFrame::create(
PropertyMap().set(IdentifiedObject::NAME_KEY, "test"),
optional<std::string>("My anchor"), optional<RealizationMethod>());
EXPECT_FALSE(datum->isEquivalentTo(drf.get()));
EXPECT_FALSE(drf->isEquivalentTo(datum.get()));
EXPECT_TRUE(
datum->isEquivalentTo(drf.get(), IComparable::Criterion::EQUIVALENT));
EXPECT_TRUE(
drf->isEquivalentTo(datum.get(), IComparable::Criterion::EQUIVALENT));

auto unrelated_datum = VerticalReferenceFrame::create(
PropertyMap().set(IdentifiedObject::NAME_KEY, "test2"),
optional<std::string>("My anchor"), optional<RealizationMethod>());
EXPECT_FALSE(unrelated_datum->isEquivalentTo(drf.get()));
EXPECT_FALSE(drf->isEquivalentTo(unrelated_datum.get()));
EXPECT_FALSE(unrelated_datum->isEquivalentTo(
drf.get(), IComparable::Criterion::EQUIVALENT));
EXPECT_FALSE(drf->isEquivalentTo(unrelated_datum.get(),
IComparable::Criterion::EQUIVALENT));
}

// ---------------------------------------------------------------------------
Expand Down