From 3a67ea87fac5b835df7966fa801881eaf7503e78 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 6 Oct 2021 19:21:03 +0200 Subject: ProjectedCRS::_isEquivalentTo(): ignore base CRS axis order even in EQUIVALENT mode if one of them is lacking an explicit CS order (refs #2886) --- src/iso19111/crs.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src') diff --git a/src/iso19111/crs.cpp b/src/iso19111/crs.cpp index b48f0d70..731bf7f9 100644 --- a/src/iso19111/crs.cpp +++ b/src/iso19111/crs.cpp @@ -4171,6 +4171,17 @@ ProjectedCRS::create(const util::PropertyMap &properties, bool ProjectedCRS::_isEquivalentTo( const util::IComparable *other, util::IComparable::Criterion criterion, const io::DatabaseContextPtr &dbContext) const { + auto otherProjCRS = dynamic_cast(other); + if (otherProjCRS != nullptr && + criterion == util::IComparable::Criterion::EQUIVALENT && + (d->baseCRS_->hasImplicitCS() || + otherProjCRS->d->baseCRS_->hasImplicitCS())) { + // If one of the 2 base CRS has implicit coordinate system, then + // relax the check. The axis order of the base CRS doesn't matter + // for most purposes. + criterion = + util::IComparable::Criterion::EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS; + } return other != nullptr && util::isOfExactType(*other) && DerivedCRS::_isEquivalentTo(other, criterion, dbContext); } -- cgit v1.2.3 From f28d36cee9ec099ae5fea3873988204a7ebda520 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 6 Oct 2021 19:26:09 +0200 Subject: CRS::_isEquivalentTo(): be tolerant to different order of PROJ step options (fixes #2886) --- src/iso19111/crs.cpp | 35 +++++++++++++++++++++++++++++++---- src/iso19111/io.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/iso19111/crs.cpp b/src/iso19111/crs.cpp index 731bf7f9..b7d57767 100644 --- a/src/iso19111/crs.cpp +++ b/src/iso19111/crs.cpp @@ -1394,6 +1394,7 @@ bool SingleCRS::baseIsEquivalentTo( return false; } + // Check datum if (criterion == util::IComparable::Criterion::STRICT) { const auto &thisDatum = d->datum; const auto &otherDatum = otherSingleCRS->d->datum; @@ -1428,10 +1429,36 @@ bool SingleCRS::baseIsEquivalentTo( } } - return d->coordinateSystem->_isEquivalentTo( - otherSingleCRS->d->coordinateSystem.get(), criterion, - dbContext) && - getExtensionProj4() == otherSingleCRS->getExtensionProj4(); + // Check coordinate system + if (!(d->coordinateSystem->_isEquivalentTo( + otherSingleCRS->d->coordinateSystem.get(), criterion, dbContext))) { + return false; + } + + // Now compare PROJ4 extensions + + const auto &thisProj4 = getExtensionProj4(); + const auto &otherProj4 = otherSingleCRS->getExtensionProj4(); + + if (thisProj4.empty() && otherProj4.empty()) { + return true; + } + + if (!(thisProj4.empty() ^ otherProj4.empty())) { + return true; + } + + // Asks for a "normalized" output during toString(), aimed at comparing two + // strings for equivalence. + auto formatter1 = io::PROJStringFormatter::create(); + formatter1->setNormalizeOutput(); + formatter1->ingestPROJString(thisProj4); + + auto formatter2 = io::PROJStringFormatter::create(); + formatter2->setNormalizeOutput(); + formatter2->ingestPROJString(otherProj4); + + return formatter1->toString() == formatter2->toString(); } // --------------------------------------------------------------------------- diff --git a/src/iso19111/io.cpp b/src/iso19111/io.cpp index 1f193559..24201ee1 100644 --- a/src/iso19111/io.cpp +++ b/src/iso19111/io.cpp @@ -7436,6 +7436,7 @@ struct PROJStringFormatter::Private { bool crsExport_ = false; bool legacyCRSToCRSContext_ = false; bool multiLine_ = false; + bool normalizeOutput_ = false; int indentWidth_ = 2; int indentLevel_ = 0; int maxLineLength_ = 80; @@ -7535,6 +7536,17 @@ const std::string &PROJStringFormatter::toString() const { d->result_.clear(); auto &steps = d->steps_; + + if (d->normalizeOutput_) { + // Sort +key=value options of each step in lexicographic order. + for (auto &step : steps) { + std::sort(step.paramValues.begin(), step.paramValues.end(), + [](const Step::KeyValue &a, const Step::KeyValue &b) { + return a.key < b.key; + }); + } + } + for (auto iter = steps.begin(); iter != steps.end();) { // Remove no-op helmert auto &step = *iter; @@ -8689,6 +8701,19 @@ bool PROJStringFormatter::getLegacyCRSToCRSContext() const { // --------------------------------------------------------------------------- +/** Asks for a "normalized" output during toString(), aimed at comparing two + * strings for equivalence. + * + * This consists for now in sorting the +key=value option in lexicographic + * order. + */ +PROJStringFormatter &PROJStringFormatter::setNormalizeOutput() { + d->normalizeOutput_ = true; + return *this; +} + +// --------------------------------------------------------------------------- + const DatabaseContextPtr &PROJStringFormatter::databaseContext() const { return d->dbContext_; } -- cgit v1.2.3