Skip to content

Commit 4680522

Browse files
committed
Add getDatabaseContextIfOpen method and update pj_obj_create to utilize it
1 parent fc4c131 commit 4680522

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

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 &&

0 commit comments

Comments
 (0)