Skip to content

Commit

Permalink
proj_crs_promote_to_3D(): make it work on CoordinateMetadata as well
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Aug 21, 2023
1 parent f5b49b8 commit 88df86b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/iso19111/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4155,18 +4155,44 @@ PJ *proj_crs_promote_to_3D(PJ_CONTEXT *ctx, const char *crs_3D_name,
}
auto cpp_2D_crs = dynamic_cast<const CRS *>(crs_2D->iso_obj.get());
if (!cpp_2D_crs) {
proj_log_error(ctx, __FUNCTION__, "crs_2D is not a CRS");
return nullptr;
}
try {
auto dbContext = getDBcontextNoException(ctx, __FUNCTION__);
return pj_obj_create(
ctx, cpp_2D_crs->promoteTo3D(crs_3D_name ? std::string(crs_3D_name)
: cpp_2D_crs->nameStr(),
dbContext));
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
return nullptr;
auto coordinateMetadata =
dynamic_cast<const CoordinateMetadata *>(crs_2D->iso_obj.get());
if (!coordinateMetadata) {
proj_log_error(ctx, __FUNCTION__,
"crs_2D is not a CRS or a CoordinateMetadata");
return nullptr;
}

try {
auto dbContext = getDBcontextNoException(ctx, __FUNCTION__);
auto crs = coordinateMetadata->crs();
auto crs_3D = crs->promoteTo3D(
crs_3D_name ? std::string(crs_3D_name) : crs->nameStr(),
dbContext);
if (coordinateMetadata->coordinateEpoch().has_value()) {
return pj_obj_create(
ctx, CoordinateMetadata::create(
crs_3D,
coordinateMetadata->coordinateEpochAsDecimalYear(),
dbContext));
} else {
return pj_obj_create(ctx, CoordinateMetadata::create(crs_3D));
}
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
return nullptr;
}
} else {
try {
auto dbContext = getDBcontextNoException(ctx, __FUNCTION__);
return pj_obj_create(ctx, cpp_2D_crs->promoteTo3D(
crs_3D_name ? std::string(crs_3D_name)
: cpp_2D_crs->nameStr(),
dbContext));
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
return nullptr;
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/unit/test_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4985,6 +4985,35 @@ TEST_F(CApi, proj_crs_promote_to_3D) {

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

TEST_F(CApi, proj_crs_promote_to_3D_on_coordinate_metadata) {

auto cm_crs2D = proj_create(m_ctxt, "[email protected]");
ObjectKeeper keeper_cm_crs2D(cm_crs2D);
EXPECT_NE(cm_crs2D, nullptr);

auto cm_crs3D = proj_crs_promote_to_3D(m_ctxt, nullptr, cm_crs2D);
ObjectKeeper keeper_cm_crs3D(cm_crs3D);
EXPECT_NE(cm_crs3D, nullptr);

EXPECT_NEAR(proj_coordinate_metadata_get_epoch(m_ctxt, cm_crs3D), 2010.0,
1e-10);

auto crs3D = proj_get_source_crs(m_ctxt, cm_crs3D);
ObjectKeeper keeper_crs3D(crs3D);
EXPECT_NE(crs3D, nullptr);

auto cs = proj_crs_get_coordinate_system(m_ctxt, crs3D);
ASSERT_NE(cs, nullptr);
ObjectKeeper keeperCs(cs);
EXPECT_EQ(proj_cs_get_axis_count(m_ctxt, cs), 3);

auto code = proj_get_id_code(crs3D, 0);
ASSERT_TRUE(code != nullptr);
EXPECT_EQ(code, std::string("7912"));
}

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

TEST_F(CApi, proj_crs_demote_to_2D) {

auto crs3D =
Expand Down

0 comments on commit 88df86b

Please sign in to comment.