Skip to content

Commit 330b206

Browse files
authored
Merge pull request OSGeo#2224 from rouault/fix_2202
Make projinfo --3d --boundcrs-to-wgs84 better work (fixes OSGeo#2202)
2 parents 6c5164e + a2f8fd1 commit 330b206

4 files changed

Lines changed: 85 additions & 6 deletions

File tree

include/proj/coordinateoperation.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,14 @@ class PROJ_GCC_DLL Transformation : public SingleOperation {
15931593

15941594
PROJ_INTERNAL TransformationNNPtr shallowClone() const;
15951595

1596+
PROJ_INTERNAL TransformationNNPtr
1597+
promoteTo3D(const std::string &newName,
1598+
const io::DatabaseContextPtr &dbContext) const;
1599+
1600+
PROJ_INTERNAL TransformationNNPtr
1601+
demoteTo2D(const std::string &newName,
1602+
const io::DatabaseContextPtr &dbContext) const;
1603+
15961604
//! @endcond
15971605

15981606
protected:

src/iso19111/coordinateoperation.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6594,6 +6594,31 @@ TransformationNNPtr Transformation::shallowClone() const {
65946594
CoordinateOperationNNPtr Transformation::_shallowClone() const {
65956595
return util::nn_static_pointer_cast<CoordinateOperation>(shallowClone());
65966596
}
6597+
6598+
// ---------------------------------------------------------------------------
6599+
6600+
TransformationNNPtr
6601+
Transformation::promoteTo3D(const std::string &,
6602+
const io::DatabaseContextPtr &dbContext) const {
6603+
auto transf = shallowClone();
6604+
transf->setCRSs(sourceCRS()->promoteTo3D(std::string(), dbContext),
6605+
targetCRS()->promoteTo3D(std::string(), dbContext),
6606+
interpolationCRS());
6607+
return transf;
6608+
}
6609+
6610+
// ---------------------------------------------------------------------------
6611+
6612+
TransformationNNPtr
6613+
Transformation::demoteTo2D(const std::string &,
6614+
const io::DatabaseContextPtr &dbContext) const {
6615+
auto transf = shallowClone();
6616+
transf->setCRSs(sourceCRS()->demoteTo2D(std::string(), dbContext),
6617+
targetCRS()->demoteTo2D(std::string(), dbContext),
6618+
interpolationCRS());
6619+
return transf;
6620+
}
6621+
65976622
//! @endcond
65986623

65996624
// ---------------------------------------------------------------------------

src/iso19111/crs.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,17 @@ CRSNNPtr CRS::promoteTo3D(const std::string &newName,
902902

903903
const auto boundCRS = dynamic_cast<const BoundCRS *>(this);
904904
if (boundCRS) {
905-
return BoundCRS::create(
906-
boundCRS->baseCRS()->promoteTo3D(newName, dbContext),
907-
boundCRS->hubCRS(), boundCRS->transformation());
905+
auto base3DCRS = boundCRS->baseCRS()->promoteTo3D(newName, dbContext);
906+
auto transf = boundCRS->transformation();
907+
try {
908+
transf->getTOWGS84Parameters();
909+
return BoundCRS::create(
910+
base3DCRS,
911+
boundCRS->hubCRS()->promoteTo3D(std::string(), dbContext),
912+
transf->promoteTo3D(std::string(), dbContext));
913+
} catch (const io::FormattingException &) {
914+
return BoundCRS::create(base3DCRS, boundCRS->hubCRS(), transf);
915+
}
908916
}
909917

910918
return NN_NO_CHECK(
@@ -937,9 +945,17 @@ CRSNNPtr CRS::demoteTo2D(const std::string &newName,
937945

938946
const auto boundCRS = dynamic_cast<const BoundCRS *>(this);
939947
if (boundCRS) {
940-
return BoundCRS::create(
941-
boundCRS->baseCRS()->demoteTo2D(newName, dbContext),
942-
boundCRS->hubCRS(), boundCRS->transformation());
948+
auto base2DCRS = boundCRS->baseCRS()->demoteTo2D(newName, dbContext);
949+
auto transf = boundCRS->transformation();
950+
try {
951+
transf->getTOWGS84Parameters();
952+
return BoundCRS::create(
953+
base2DCRS,
954+
boundCRS->hubCRS()->demoteTo2D(std::string(), dbContext),
955+
transf->demoteTo2D(std::string(), dbContext));
956+
} catch (const io::FormattingException &) {
957+
return BoundCRS::create(base2DCRS, boundCRS->hubCRS(), transf);
958+
}
943959
}
944960

945961
const auto compoundCRS = dynamic_cast<const CompoundCRS *>(this);

test/unit/test_crs.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5794,6 +5794,21 @@ TEST(crs, promoteTo3D_and_demoteTo2D) {
57945794
nn_dynamic_pointer_cast<ProjectedCRS>(crs3DAsBound->baseCRS());
57955795
ASSERT_TRUE(baseCRS != nullptr);
57965796
EXPECT_EQ(baseCRS->coordinateSystem()->axisList().size(), 3U);
5797+
5798+
auto hubCRS =
5799+
nn_dynamic_pointer_cast<GeographicCRS>(crs3DAsBound->hubCRS());
5800+
ASSERT_TRUE(hubCRS != nullptr);
5801+
EXPECT_EQ(hubCRS->coordinateSystem()->axisList().size(), 3U);
5802+
5803+
auto transfSrcCRS = nn_dynamic_pointer_cast<GeographicCRS>(
5804+
crs3DAsBound->transformation()->sourceCRS());
5805+
ASSERT_TRUE(transfSrcCRS != nullptr);
5806+
EXPECT_EQ(transfSrcCRS->coordinateSystem()->axisList().size(), 3U);
5807+
5808+
auto transfDstCRS = nn_dynamic_pointer_cast<GeographicCRS>(
5809+
crs3DAsBound->transformation()->targetCRS());
5810+
ASSERT_TRUE(transfDstCRS != nullptr);
5811+
EXPECT_EQ(transfDstCRS->coordinateSystem()->axisList().size(), 3U);
57975812
}
57985813

57995814
auto demoted = crs3DAsBound->demoteTo2D(std::string(), nullptr);
@@ -5804,6 +5819,21 @@ TEST(crs, promoteTo3D_and_demoteTo2D) {
58045819
nn_dynamic_pointer_cast<ProjectedCRS>(crs2DAsBound->baseCRS());
58055820
ASSERT_TRUE(baseCRS != nullptr);
58065821
EXPECT_EQ(baseCRS->coordinateSystem()->axisList().size(), 2U);
5822+
5823+
auto hubCRS =
5824+
nn_dynamic_pointer_cast<GeographicCRS>(crs2DAsBound->hubCRS());
5825+
ASSERT_TRUE(hubCRS != nullptr);
5826+
EXPECT_EQ(hubCRS->coordinateSystem()->axisList().size(), 2U);
5827+
5828+
auto transfSrcCRS = nn_dynamic_pointer_cast<GeographicCRS>(
5829+
crs2DAsBound->transformation()->sourceCRS());
5830+
ASSERT_TRUE(transfSrcCRS != nullptr);
5831+
EXPECT_EQ(transfSrcCRS->coordinateSystem()->axisList().size(), 2U);
5832+
5833+
auto transfDstCRS = nn_dynamic_pointer_cast<GeographicCRS>(
5834+
crs2DAsBound->transformation()->targetCRS());
5835+
ASSERT_TRUE(transfDstCRS != nullptr);
5836+
EXPECT_EQ(transfDstCRS->coordinateSystem()->axisList().size(), 2U);
58075837
}
58085838
}
58095839

0 commit comments

Comments
 (0)