aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2019-01-09 11:56:39 +0100
committerEven Rouault <even.rouault@spatialys.com>2019-01-09 12:34:40 +0100
commit11bc4783ca2c5fed8603d0c49f5aea102eca2432 (patch)
tree95adef15f42efa35ff8862e9c3753c6206068f87 /src
parent96339b80be1ad3db18075e2d8d650ef121797964 (diff)
downloadPROJ-11bc4783ca2c5fed8603d0c49f5aea102eca2432.tar.gz
PROJ-11bc4783ca2c5fed8603d0c49f5aea102eca2432.zip
proj.db search: use pj_find_file() mechanism instead of hand coded simplified version of it
Diffstat (limited to 'src')
-rw-r--r--src/iso19111/c_api.cpp4
-rw-r--r--src/iso19111/factory.cpp77
2 files changed, 23 insertions, 58 deletions
diff --git a/src/iso19111/c_api.cpp b/src/iso19111/c_api.cpp
index 70488de4..5717a76a 100644
--- a/src/iso19111/c_api.cpp
+++ b/src/iso19111/c_api.cpp
@@ -99,9 +99,7 @@ struct projCppContext {
explicit projCppContext(PJ_CONTEXT *ctx, const char *dbPath = nullptr,
const char *const *auxDbPaths = nullptr)
: databaseContext(DatabaseContext::create(
- dbPath ? dbPath : std::string(), toVector(auxDbPaths))) {
- databaseContext->attachPJContext(ctx);
- }
+ dbPath ? dbPath : std::string(), toVector(auxDbPaths), ctx)) {}
static std::vector<std::string> toVector(const char *const *auxDbPaths) {
std::vector<std::string> res;
diff --git a/src/iso19111/factory.cpp b/src/iso19111/factory.cpp
index 8454966a..3b6563f3 100644
--- a/src/iso19111/factory.cpp
+++ b/src/iso19111/factory.cpp
@@ -118,7 +118,7 @@ struct DatabaseContext::Private {
Private();
~Private();
- void open(const std::string &databasePath = std::string());
+ void open(const std::string &databasePath, PJ_CONTEXT *ctx);
void setHandle(sqlite3 *sqlite_handle);
sqlite3 *handle() const { return sqlite_handle_; }
@@ -554,20 +554,18 @@ bool DatabaseContext::Private::createCustomVFS() {
// ---------------------------------------------------------------------------
-void DatabaseContext::Private::open(const std::string &databasePath) {
+void DatabaseContext::Private::open(const std::string &databasePath,
+ PJ_CONTEXT *ctx) {
+ setPjCtxt(ctx ? ctx : pj_get_default_ctx());
std::string path(databasePath);
if (path.empty()) {
- const char *proj_lib = std::getenv("PROJ_LIB");
-#ifdef PROJ_LIB
- if (!proj_lib) {
- proj_lib = PROJ_LIB;
+ path.resize(2048);
+ const bool found =
+ pj_find_file(pjCtxt(), "proj.db", &path[0], path.size() - 1) != 0;
+ path.resize(strlen(path.c_str()));
+ if (!found) {
+ throw FactoryException("Cannot find proj.db");
}
-#endif
- if (!proj_lib) {
- throw FactoryException(
- "Cannot find proj.db due to missing PROJ_LIB");
- }
- path = std::string(proj_lib) + DIR_CHAR + "proj.db";
}
if (
@@ -854,52 +852,27 @@ DatabaseContext::DatabaseContext() : d(internal::make_unique<Private>()) {}
// ---------------------------------------------------------------------------
-/** \brief Instantiate a database context, using the default proj.db file
- *
- * It will be searched in the directory pointed by the PROJ_LIB environment
- * variable. If not found, on Unix builds, it will be then searched first in
- * the pkgdatadir directory of the installation prefix.
- *
- * This database context should be used only by one thread at a time.
- * @throw FactoryException
- */
-DatabaseContextNNPtr DatabaseContext::create() {
- return create(std::string(), {});
-}
-
-// ---------------------------------------------------------------------------
-
-/** \brief Instantiate a database context from a full filename.
+/** \brief Instantiate a database context.
*
* This database context should be used only by one thread at a time.
- * @param databasePath Path and filename of the database. Might be empty
- * string for the default rules to locate the default proj.db
- * @throw FactoryException
- */
-DatabaseContextNNPtr DatabaseContext::create(const std::string &databasePath) {
- return create(databasePath, {});
-}
-
-// ---------------------------------------------------------------------------
-
-/** \brief Instantiate a database context from a full filename, and attach
- * auxiliary databases to it.
*
- * This database context should be used only by one thread at a time.
* @param databasePath Path and filename of the database. Might be empty
* string for the default rules to locate the default proj.db
- * @param auxiliaryDatabasePaths Path and filename of auxiliary databases;
+ * @param auxiliaryDatabasePaths Path and filename of auxiliary databases.
+ * Might be empty.
+ * @param ctx Context used for file search.
* @throw FactoryException
*/
-DatabaseContextNNPtr DatabaseContext::create(
- const std::string &databasePath,
- const std::vector<std::string> &auxiliaryDatabasePaths) {
- auto ctxt = DatabaseContext::nn_make_shared<DatabaseContext>();
- ctxt->getPrivate()->open(databasePath);
+DatabaseContextNNPtr
+DatabaseContext::create(const std::string &databasePath,
+ const std::vector<std::string> &auxiliaryDatabasePaths,
+ PJ_CONTEXT *ctx) {
+ auto dbCtx = DatabaseContext::nn_make_shared<DatabaseContext>();
+ dbCtx->getPrivate()->open(databasePath, ctx);
if (!auxiliaryDatabasePaths.empty()) {
- ctxt->getPrivate()->attachExtraDatabases(auxiliaryDatabasePaths);
+ dbCtx->getPrivate()->attachExtraDatabases(auxiliaryDatabasePaths);
}
- return ctxt;
+ return dbCtx;
}
// ---------------------------------------------------------------------------
@@ -964,12 +937,6 @@ void *DatabaseContext::getSqliteHandle() const {
// ---------------------------------------------------------------------------
-void DatabaseContext::attachPJContext(void *pjCtxt) {
- d->setPjCtxt(static_cast<PJ_CONTEXT *>(pjCtxt));
-}
-
-// ---------------------------------------------------------------------------
-
bool DatabaseContext::lookForGridAlternative(const std::string &officialName,
std::string &projFilename,
std::string &projFormat,