aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2021-10-07 09:14:20 +0200
committerGitHub <noreply@github.com>2021-10-07 09:14:20 +0200
commit39ecfbec938a5135026a3f5116928b6c5bb47221 (patch)
tree9adde1d0f8936b3439bb2b42e5c075f9d8df6362 /src
parentf057ac086e36e33fd83656c283d18496355cea78 (diff)
parentf28d36cee9ec099ae5fea3873988204a7ebda520 (diff)
downloadPROJ-39ecfbec938a5135026a3f5116928b6c5bb47221.tar.gz
PROJ-39ecfbec938a5135026a3f5116928b6c5bb47221.zip
Merge pull request #2887 from rouault/fix_2886
Fix CRS Equality with PROJ parameter order
Diffstat (limited to 'src')
-rw-r--r--src/iso19111/crs.cpp46
-rw-r--r--src/iso19111/io.cpp25
2 files changed, 67 insertions, 4 deletions
diff --git a/src/iso19111/crs.cpp b/src/iso19111/crs.cpp
index b48f0d70..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();
}
// ---------------------------------------------------------------------------
@@ -4171,6 +4198,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<const ProjectedCRS *>(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<ProjectedCRS>(*other) &&
DerivedCRS::_isEquivalentTo(other, criterion, dbContext);
}
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_;
}