From 225df1ee596db14220796e89f8a5f6fc3ae1808f Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 5 Dec 2022 23:47:43 +0100 Subject: [PATCH] DatabaseContext::lookForGridInfo(): add PROJ networking state in cache information (fixes pyproj4/pyproj#1192) --- src/iso19111/factory.cpp | 17 ++++++++------ test/unit/test_network.cpp | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/iso19111/factory.cpp b/src/iso19111/factory.cpp index 96327a63f8..5bd686a9fd 100644 --- a/src/iso19111/factory.cpp +++ b/src/iso19111/factory.cpp @@ -3243,8 +3243,16 @@ bool DatabaseContext::lookForGridInfo( std::string &fullFilename, std::string &packageName, std::string &url, bool &directDownload, bool &openLicense, bool &gridAvailable) const { Private::GridInfoCache info; - const std::string key(projFilename + - (considerKnownGridsAsAvailable ? "true" : "false")); + + auto ctxt = d->pjCtxt(); + if (ctxt == nullptr) { + ctxt = pj_get_default_ctx(); + d->setPjCtxt(ctxt); + } + + std::string key(projFilename); + key += proj_context_is_network_enabled(ctxt) ? "true" : "false"; + key += considerKnownGridsAsAvailable ? "true" : "false"; if (d->getGridInfoFromCache(key, info)) { fullFilename = info.fullFilename; packageName = info.packageName; @@ -3262,11 +3270,6 @@ bool DatabaseContext::lookForGridInfo( directDownload = false; fullFilename.resize(2048); - auto ctxt = d->pjCtxt(); - if (ctxt == nullptr) { - ctxt = pj_get_default_ctx(); - d->setPjCtxt(ctxt); - } int errno_before = proj_context_errno(ctxt); gridAvailable = NS_PROJ::FileManager::open_resource_file( ctxt, projFilename.c_str(), &fullFilename[0], diff --git a/test/unit/test_network.cpp b/test/unit/test_network.cpp index f16d04324b..0b258ed384 100644 --- a/test/unit/test_network.cpp +++ b/test/unit/test_network.cpp @@ -1974,4 +1974,51 @@ TEST(networking, proj_coordoperation_get_grid_used) { #endif +// --------------------------------------------------------------------------- + +#ifdef CURL_ENABLED + +TEST(networking, pyproj_issue_1192) { + if (!networkAccessOK) { + return; + } + + const auto doTest = [](PJ_CONTEXT *ctxt) { + auto factory_context = + proj_create_operation_factory_context(ctxt, nullptr); + proj_operation_factory_context_set_grid_availability_use( + ctxt, factory_context, PROJ_GRID_AVAILABILITY_IGNORED); + proj_operation_factory_context_set_spatial_criterion( + ctxt, factory_context, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION); + auto from = proj_create(ctxt, "EPSG:4326"); + auto to = proj_create(ctxt, "EPSG:2964"); + auto pj_operations = + proj_create_operations(ctxt, from, to, factory_context); + proj_destroy(from); + proj_destroy(to); + auto num_operations = proj_list_get_count(pj_operations); + for (int i = 0; i < num_operations; ++i) { + PJ *P = proj_list_get(ctxt, pj_operations, i); + int is_instantiable = proj_coordoperation_is_instantiable(ctxt, P); + if (is_instantiable) { + EXPECT_TRUE(proj_pj_info(P).id != nullptr); + } + proj_destroy(P); + } + proj_operation_factory_context_destroy(factory_context); + proj_list_destroy(pj_operations); + }; + + auto ctx = proj_context_create(); + proj_grid_cache_set_enable(ctx, false); + proj_context_set_enable_network(ctx, true); + doTest(ctx); + proj_context_set_enable_network(ctx, false); + doTest(ctx); + + proj_context_destroy(ctx); +} + +#endif + } // namespace