diff --git a/include/proj/io.hpp b/include/proj/io.hpp index ba7e9f5339..41038e2df6 100644 --- a/include/proj/io.hpp +++ b/include/proj/io.hpp @@ -907,7 +907,10 @@ class PROJ_GCC_DLL AuthorityFactory { const std::vector &allowedObjectTypes = std::vector(), bool approximateMatch = true, - size_t limitResultCount = 0); + size_t limitResultCount = 0) const; + + PROJ_DLL std::list> + listAreaOfUseFromName(const std::string &name, bool approximateMatch) const; PROJ_PRIVATE : //! @cond Doxygen_Suppress diff --git a/src/factory.cpp b/src/factory.cpp index 91d0c47860..7bc9c4d2ed 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -3642,7 +3642,7 @@ std::list AuthorityFactory::createObjectsFromName( const std::string &searchedName, const std::vector &allowedObjectTypes, bool approximateMatch, - size_t limitResultCount) { + size_t limitResultCount) const { std::string searchedNameWithoutDeprecated(searchedName); bool deprecated = false; @@ -3885,6 +3885,39 @@ AuthorityFactory::createObjectsFromName( // --------------------------------------------------------------------------- +/** \brief Return a list of area of use from their name + * + * @param name Searched name. + * @param approximateMatch Whether approximate name identification is allowed. + * @return list of (auth_name, code) of matched objects. + * @throw FactoryException + */ +std::list> +AuthorityFactory::listAreaOfUseFromName(const std::string &name, + bool approximateMatch) const { + std::string sql( + "SELECT auth_name, code FROM area WHERE deprecated = 0 AND "); + std::vector params; + if (!getAuthority().empty()) { + sql += " auth_name = ? AND "; + params.emplace_back(getAuthority()); + } + sql += "name LIKE ?"; + if (!approximateMatch) { + params.push_back(name); + } else { + params.push_back('%' + name + '%'); + } + auto sqlRes = d->run(sql, params); + std::list> res; + for (const auto &row : sqlRes) { + res.emplace_back(row[0], row[1]); + } + return res; +} + +// --------------------------------------------------------------------------- + //! @cond Doxygen_Suppress std::list AuthorityFactory::createEllipsoidFromExisting( const datum::EllipsoidNNPtr &ellipsoid) const { diff --git a/src/projinfo.cpp b/src/projinfo.cpp index 5f580a6f3a..742889b771 100644 --- a/src/projinfo.cpp +++ b/src/projinfo.cpp @@ -74,7 +74,8 @@ static void usage() { std::cerr << "usage: projinfo [-o formats] [-k crs|operation] [--summary] [-q]" << std::endl - << " [--bbox min_long,min_lat,max_long,max_lat] " + << " ([--area name_or_code] | " + "[--bbox min_long,min_lat,max_long,max_lat]) " << std::endl << " [--spatial-test contains|intersects]" << std::endl << " [--crs-extent-use none|both|intersection|smallest]" @@ -565,6 +566,7 @@ int main(int argc, char **argv) { bool kindIsCRS = true; bool summary = false; ExtentPtr bboxFilter = nullptr; + std::string area; CoordinateOperationContext::SpatialCriterion spatialCriterion = CoordinateOperationContext::SpatialCriterion::STRICT_CONTAINMENT; CoordinateOperationContext::SourceTargetCRSExtentUse crsExtentUse = @@ -686,6 +688,9 @@ int main(int argc, char **argv) { << ", " << e.what() << std::endl; usage(); } + } else if (arg == "--area" && i + 1 < argc) { + i++; + area = argv[i]; } else if (arg == "-k" && i + 1 < argc) { i++; std::string kind(argv[i]); @@ -818,12 +823,17 @@ int main(int argc, char **argv) { } } + if (bboxFilter && !area.empty()) { + std::cerr << "ERROR: --bbox and --area are exclusive" << std::endl; + std::exit(1); + } + DatabaseContextPtr dbContext; try { dbContext = DatabaseContext::create(mainDBPath, auxDBPath).as_nullable(); } catch (const std::exception &e) { - if (!mainDBPath.empty() || !auxDBPath.empty()) { + if (!mainDBPath.empty() || !auxDBPath.empty() || !area.empty()) { std::cerr << "ERROR: Cannot create database connection: " << e.what() << std::endl; std::exit(1); @@ -928,6 +938,66 @@ int main(int argc, char **argv) { } } } else { + + if (!area.empty()) { + assert(dbContext); + try { + if (area.find(' ') == std::string::npos && + area.find(':') != std::string::npos) { + auto tokens = split(area, ':'); + if (tokens.size() == 2) { + const std::string &areaAuth = tokens[0]; + const std::string &areaCode = tokens[1]; + bboxFilter = AuthorityFactory::create( + NN_NO_CHECK(dbContext), areaAuth) + ->createExtent(areaCode); + } + } + if (!bboxFilter) { + auto authFactory = AuthorityFactory::create( + NN_NO_CHECK(dbContext), std::string()); + auto res = authFactory->listAreaOfUseFromName(area, false); + if (res.size() == 1) { + bboxFilter = + AuthorityFactory::create(NN_NO_CHECK(dbContext), + res.front().first) + ->createExtent(res.front().second); + } else { + res = authFactory->listAreaOfUseFromName(area, true); + if (res.size() == 1) { + bboxFilter = + AuthorityFactory::create(NN_NO_CHECK(dbContext), + res.front().first) + ->createExtent(res.front().second); + } else if (res.empty()) { + std::cerr + << "No area of use matching provided name" + << std::endl; + std::exit(1); + } else { + std::cerr << "Several candidate area of use " + "matching providing name :" + << std::endl; + for (const auto &candidate : res) { + auto obj = + AuthorityFactory::create( + NN_NO_CHECK(dbContext), candidate.first) + ->createExtent(candidate.second); + std::cerr << " " << candidate.first << ":" + << candidate.second << " : " + << *obj->description() << std::endl; + } + std::exit(1); + } + } + } + } catch (const std::exception &e) { + std::cerr << "Area of use retrieval failed: " << e.what() + << std::endl; + std::exit(1); + } + } + outputOperations(dbContext, sourceCRSStr, targetCRSStr, bboxFilter, spatialCriterion, crsExtentUse, gridAvailabilityUse, allowPivots, pivots, authority, diff --git a/test/cli/testprojinfo b/test/cli/testprojinfo index ad62d1daa2..90829fddf6 100755 --- a/test/cli/testprojinfo +++ b/test/cli/testprojinfo @@ -49,6 +49,31 @@ echo "Testing projinfo -s NAD27 -t NAD83 --grid-check none --spatial-test inters $EXE -s NAD27 -t NAD83 --grid-check none --spatial-test intersects >>${OUT} echo "" >>${OUT} +echo "Testing projinfo -s EPSG:4230 -t EPSG:4258 --bbox 8,54.51,15.24,57.8 --summary" >> ${OUT} +$EXE -s EPSG:4230 -t EPSG:4258 --bbox 8,54.51,15.24,57.8 --summary >>${OUT} +echo "" >>${OUT} + +echo "Testing projinfo -s EPSG:4230 -t EPSG:4258 --area EPSG:3237 --summary" >> ${OUT} +$EXE -s EPSG:4230 -t EPSG:4258 --area EPSG:3237 --summary >>${OUT} +echo "" >>${OUT} + +echo "Testing projinfo -s EPSG:4230 -t EPSG:4258 --area 'Denmark - onshore' --summary" >> ${OUT} +$EXE -s EPSG:4230 -t EPSG:4258 --area 'Denmark - onshore' --summary >>${OUT} +echo "" >>${OUT} + +# several match +echo "Testing projinfo -s EPSG:4230 -t EPSG:4258 --area 'Denmark -' --summary" >> ${OUT} +$EXE -s EPSG:4230 -t EPSG:4258 --area 'Denmark -' --summary >>${OUT} 2>&1 +echo "" >>${OUT} + +echo "Testing projinfo -s EPSG:4230 -t EPSG:4258 --area no_match --summary" >> ${OUT} +$EXE -s EPSG:4230 -t EPSG:4258 --area no_match --summary >>${OUT} 2>&1 +echo "" >>${OUT} + +echo "Testing projinfo -s EPSG:4230 -t EPSG:4258 --area WRONG:CODE --summary" >> ${OUT} +$EXE -s EPSG:4230 -t EPSG:4258 --area WRONG:CODE --summary >>${OUT} 2>&1 +echo "" >>${OUT} + # do 'diff' with distribution results echo "diff ${OUT} with testprojinfo_out.dist" diff -u ${OUT} ${TEST_CLI_DIR}/testprojinfo_out.dist diff --git a/test/cli/testprojinfo_out.dist b/test/cli/testprojinfo_out.dist index 7cb4891b08..ab7f6e238e 100644 --- a/test/cli/testprojinfo_out.dist +++ b/test/cli/testprojinfo_out.dist @@ -496,3 +496,32 @@ COORDINATEOPERATION["NAD27 to NAD83 (6)", BBOX[44.99,-79.85,62.62,-57.1], ID["EPSG",1573]] +Testing projinfo -s EPSG:4230 -t EPSG:4258 --bbox 8,54.51,15.24,57.8 --summary +Candidate operations found: 1 +EPSG:1626, ED50 to ETRS89 (4), 1.0 m, Denmark - onshore + +Testing projinfo -s EPSG:4230 -t EPSG:4258 --area EPSG:3237 --summary +Candidate operations found: 1 +EPSG:1626, ED50 to ETRS89 (4), 1.0 m, Denmark - onshore + +Testing projinfo -s EPSG:4230 -t EPSG:4258 --area 'Denmark - onshore' --summary +Candidate operations found: 1 +EPSG:1626, ED50 to ETRS89 (4), 1.0 m, Denmark - onshore + +Testing projinfo -s EPSG:4230 -t EPSG:4258 --area 'Denmark -' --summary +Several candidate area of use matching providing name : + EPSG:2531 : Denmark - onshore Jutland and Funen + EPSG:2532 : Denmark - onshore Zealand and Lolland + EPSG:2533 : Denmark - onshore Bornholm + EPSG:3237 : Denmark - onshore + EPSG:3471 : Denmark - onshore west of 12°E + EPSG:3472 : Denmark - onshore east of 12°E + EPSG:3631 : Denmark - onshore Jutland west of 10°E + EPSG:3632 : Denmark - onshore Jutland east of 9°E and Funen + +Testing projinfo -s EPSG:4230 -t EPSG:4258 --area no_match --summary +No area of use matching provided name + +Testing projinfo -s EPSG:4230 -t EPSG:4258 --area WRONG:CODE --summary +Area of use retrieval failed: area not found + diff --git a/test/unit/test_factory.cpp b/test/unit/test_factory.cpp index 7cdb0b40eb..c30111cebc 100644 --- a/test/unit/test_factory.cpp +++ b/test/unit/test_factory.cpp @@ -2746,4 +2746,26 @@ TEST(factory, getMetadata) { EXPECT_EQ(std::string(IGNF_VERSION), "3.0.2"); } +// --------------------------------------------------------------------------- + +TEST(factory, listAreaOfUseFromName) { + auto ctxt = DatabaseContext::create(); + auto factory = AuthorityFactory::create(ctxt, std::string()); + auto factoryEPSG = AuthorityFactory::create(ctxt, "EPSG"); + { + auto res = factory->listAreaOfUseFromName("Denmark - onshore", false); + ASSERT_EQ(res.size(), 1); + EXPECT_EQ(res.front().first, "EPSG"); + EXPECT_EQ(res.front().second, "3237"); + } + { + auto res = factory->listAreaOfUseFromName("Denmark", true); + EXPECT_GT(res.size(), 1U); + } + { + auto res = factory->listAreaOfUseFromName("no where land", false); + ASSERT_EQ(res.size(), 0); + } +} + } // namespace