From d1fa64ff3eeb515431bc7fe4d2d3beae4071051a Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 28 Nov 2024 21:58:34 +0100 Subject: [PATCH] proj_trans_bounds(): make it work when target CRS is a CompoundCRS --- src/4D_api.cpp | 14 ++++++++++++-- test/unit/test_c_api.cpp | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/4D_api.cpp b/src/4D_api.cpp index 478dab8fa8..99e56ffda2 100644 --- a/src/4D_api.cpp +++ b/src/4D_api.cpp @@ -1416,8 +1416,18 @@ static int target_crs_lon_lat_order(PJ_CONTEXT *transformer_ctx, "Unable to retrieve target CRS"); return -1; } - PJ *coord_system_pj = - proj_crs_get_coordinate_system(transformer_ctx, target_crs); + PJ *coord_system_pj; + if (proj_get_type(target_crs) == PJ_TYPE_COMPOUND_CRS) { + PJ *horiz_crs = proj_crs_get_sub_crs(transformer_ctx, target_crs, 0); + if (!horiz_crs) + return -1; + coord_system_pj = + proj_crs_get_coordinate_system(transformer_ctx, horiz_crs); + proj_destroy(horiz_crs); + } else { + coord_system_pj = + proj_crs_get_coordinate_system(transformer_ctx, target_crs); + } proj_destroy(target_crs); if (coord_system_pj == nullptr) { proj_context_log_debug(transformer_ctx, diff --git a/test/unit/test_c_api.cpp b/test/unit/test_c_api.cpp index 4a6450979a..ff6d13dab1 100644 --- a/test/unit/test_c_api.cpp +++ b/test/unit/test_c_api.cpp @@ -6515,6 +6515,27 @@ TEST_F(CApi, proj_trans_bounds__south_pole) { // --------------------------------------------------------------------------- +TEST_F(CApi, proj_trans_bounds_to_compound_crs) { + // EPSG:9707 = "WGS 84 + EGM96 height" + auto P = proj_create_crs_to_crs(m_ctxt, "EPSG:4326", "EPSG:9707", nullptr); + ObjectKeeper keeper_P(P); + ASSERT_NE(P, nullptr); + double out_left; + double out_bottom; + double out_right; + double out_top; + int success = + proj_trans_bounds(m_ctxt, P, PJ_FWD, 40, -120, 64, -80, &out_left, + &out_bottom, &out_right, &out_top, 0); + EXPECT_TRUE(success == 1); + EXPECT_NEAR(out_left, 40, 1e-8); + EXPECT_NEAR(out_bottom, -120, 1e-8); + EXPECT_NEAR(out_right, 64, 1e-8); + EXPECT_NEAR(out_top, -80, 1e-8); +} + +// --------------------------------------------------------------------------- + TEST_F(CApi, proj_crs_has_point_motion_operation) { auto ctxt = proj_create_operation_factory_context(m_ctxt, nullptr); ASSERT_NE(ctxt, nullptr);