Skip to content

Commit 25efd86

Browse files
authored
Merge pull request #4703 from OSGeo/backport-4701-to-9.8
[Backport 9.8] Fix pj_obj_create() grid alternative resolution when old_proj_grid_name is NULL
2 parents dd57ef4 + 6521c3b commit 25efd86

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

include/proj/internal/io_internal.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ struct PROJ_GCC_DLL projCppContext {
210210

211211
NS_PROJ::io::DatabaseContextNNPtr PROJ_FOR_TEST getDatabaseContext();
212212

213+
/** Return the database context only if already opened.
214+
* Does not attempt to open proj.db. */
215+
inline NS_PROJ::io::DatabaseContextPtr getDatabaseContextIfOpen() const {
216+
return databaseContext_;
217+
}
218+
213219
void closeDb() { databaseContext_ = nullptr; }
214220
};
215221

src/iso19111/c_api.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,19 @@ PJ *pj_obj_create(PJ_CONTEXT *ctx, const BaseObjectNNPtr &objIn) {
205205
}
206206
if (bTryToExportToProj) {
207207
try {
208+
// Use the database context if already open (e.g. when
209+
// coming from proj_create_from_database), so that
210+
// substitutePROJAlternativeGridNames() can resolve
211+
// grid names via the grid_alternatives table.
212+
// Do NOT open the database here — callers such as
213+
// proj_create() with a plain pipeline string may run
214+
// without proj.db (see commit 63c491eda3).
215+
auto dbContext =
216+
ctx->cpp_context
217+
? ctx->get_cpp_context()->getDatabaseContextIfOpen()
218+
: nullptr;
208219
auto formatter = PROJStringFormatter::create(
209-
PROJStringFormatter::Convention::PROJ_5, nullptr);
220+
PROJStringFormatter::Convention::PROJ_5, dbContext);
210221
auto projString = coordop->exportToPROJString(formatter.get());
211222
const bool defer_grid_opening_backup = ctx->defer_grid_opening;
212223
if (!defer_grid_opening_backup &&

test/unit/test_c_api.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,37 @@ TEST_F(CApi, transformation_from_boundCRS) {
16941694

16951695
// ---------------------------------------------------------------------------
16961696

1697+
TEST_F(CApi,
1698+
proj_create_from_database_grid_alternative_null_old_proj_grid_name) {
1699+
// EPSG:9484 uses grid "href2008a.bin" whose grid_alternatives entry has
1700+
// proj_grid_name = "no_kv_href2008a.tif" but old_proj_grid_name IS NULL.
1701+
// Without the database context in pj_obj_create(),
1702+
// substitutePROJAlternativeGridNames() cannot resolve the grid name,
1703+
// leaving the original "href2008a.bin" in the PROJ string. With the fix,
1704+
// the CDN name "no_kv_href2008a.tif" is used instead.
1705+
//
1706+
// Enable network so that pj_obj_create() sets defer_grid_opening=true,
1707+
// allowing the pipeline to be created even without the grid file on disk.
1708+
proj_context_set_enable_network(m_ctxt, 1);
1709+
1710+
auto op = proj_create_from_database(m_ctxt, "EPSG", "9484",
1711+
PJ_CATEGORY_COORDINATE_OPERATION, false,
1712+
nullptr);
1713+
ASSERT_NE(op, nullptr);
1714+
ObjectKeeper keeper(op);
1715+
1716+
auto info = proj_pj_info(op);
1717+
ASSERT_NE(info.definition, nullptr);
1718+
EXPECT_TRUE(std::string(info.definition).find("no_kv_href2008a.tif") !=
1719+
std::string::npos)
1720+
<< "Expected CDN grid name 'no_kv_href2008a.tif' in definition, got: "
1721+
<< info.definition;
1722+
1723+
proj_context_set_enable_network(m_ctxt, 0);
1724+
}
1725+
1726+
// ---------------------------------------------------------------------------
1727+
16971728
TEST_F(CApi, proj_coordoperation_get_grid_used) {
16981729
auto op = proj_create_from_database(m_ctxt, "EPSG", "1312",
16991730
PJ_CATEGORY_COORDINATE_OPERATION, true,

0 commit comments

Comments
 (0)