Skip to content

Commit

Permalink
createObjectsFromName(): in exact match, make looking for 'ETRS89 / U…
Browse files Browse the repository at this point in the history
…TM zone 32N' return only the exact match
  • Loading branch information
rouault committed Nov 28, 2020
1 parent a8ad205 commit f0ef1b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/iso19111/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6019,6 +6019,8 @@ AuthorityFactory::createObjectsFromNameEx(
auto sqlRes = d->run(sql, params);
bool isFirst = true;
bool firstIsDeprecated = false;
bool foundExactMatch = false;
std::size_t hashCodeFirstMatch = 0;
for (const auto &row : sqlRes) {
const auto &name = row[3];
if (approximateMatch) {
Expand Down Expand Up @@ -6082,11 +6084,38 @@ AuthorityFactory::createObjectsFromNameEx(
}
throw std::runtime_error("Unsupported table_name");
};
res.emplace_back(PairObjectName(getObject(table_name, code), name));
const auto obj = getObject(table_name, code);
if (metadata::Identifier::canonicalizeName(obj->nameStr()) ==
canonicalizedSearchedName) {
foundExactMatch = true;
}

const auto objPtr = obj.get();
if (res.empty()) {
hashCodeFirstMatch = typeid(*objPtr).hash_code();
} else if (hashCodeFirstMatch != typeid(*objPtr).hash_code()) {
hashCodeFirstMatch = 0;
}

res.emplace_back(PairObjectName(obj, name));
if (limitResultCount > 0 && res.size() == limitResultCount) {
break;
}
}

// If we found a name that is an exact match, and all objects have the
// same type, and we are not in approximate mode, only keep the objet(s)
// with the exact name match.
if (foundExactMatch && hashCodeFirstMatch != 0 && !approximateMatch) {
std::list<PairObjectName> resTmp;
for (const auto &pair : res) {
if (metadata::Identifier::canonicalizeName(
pair.first->nameStr()) == canonicalizedSearchedName) {
resTmp.emplace_back(pair);
}
}
res = std::move(resTmp);
}
}

auto sortLambda = [](const PairObjectName &a, const PairObjectName &b) {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3062,6 +3062,12 @@ TEST(factory, createObjectsFromName) {
.size(),
1U);

// Exact name, but with other CRS that have an aliases to it ==> should
// match only the CRS with the given name, not those other CRS.
EXPECT_EQ(factory->createObjectsFromName("ETRS89 / UTM zone 32N", {}, false)
.size(),
1U);

// Prime meridian
EXPECT_EQ(factoryEPSG->createObjectsFromName("Paris", {}, false, 2).size(),
1U);
Expand Down

0 comments on commit f0ef1b9

Please sign in to comment.