Skip to content

Commit

Permalink
projinfo: add a --area option (refs #1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Dec 3, 2018
1 parent ac24807 commit 1dab665
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/proj/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,10 @@ class PROJ_GCC_DLL AuthorityFactory {
const std::vector<ObjectType> &allowedObjectTypes =
std::vector<ObjectType>(),
bool approximateMatch = true,
size_t limitResultCount = 0);
size_t limitResultCount = 0) const;

PROJ_DLL std::list<std::pair<std::string, std::string>>
listAreaOfUseFromName(const std::string &name, bool approximateMatch) const;

PROJ_PRIVATE :
//! @cond Doxygen_Suppress
Expand Down
35 changes: 34 additions & 1 deletion src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3642,7 +3642,7 @@ std::list<common::IdentifiedObjectNNPtr>
AuthorityFactory::createObjectsFromName(
const std::string &searchedName,
const std::vector<ObjectType> &allowedObjectTypes, bool approximateMatch,
size_t limitResultCount) {
size_t limitResultCount) const {

std::string searchedNameWithoutDeprecated(searchedName);
bool deprecated = false;
Expand Down Expand Up @@ -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<std::pair<std::string, std::string>>
AuthorityFactory::listAreaOfUseFromName(const std::string &name,
bool approximateMatch) const {
std::string sql(
"SELECT auth_name, code FROM area WHERE deprecated = 0 AND ");
std::vector<SQLValues> 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<std::pair<std::string, std::string>> res;
for (const auto &row : sqlRes) {
res.emplace_back(row[0], row[1]);
}
return res;
}

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

//! @cond Doxygen_Suppress
std::list<datum::EllipsoidNNPtr> AuthorityFactory::createEllipsoidFromExisting(
const datum::EllipsoidNNPtr &ellipsoid) const {
Expand Down
74 changes: 72 additions & 2 deletions src/projinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions test/cli/testprojinfo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/cli/testprojinfo_out.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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

22 changes: 22 additions & 0 deletions test/unit/test_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1dab665

Please sign in to comment.