diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2020-01-28 12:03:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-28 12:03:20 +0100 |
| commit | f2efd1d73c6c56531280604fd27c73fb13b41206 (patch) | |
| tree | 93b2016db24e875ccebb43abcd2de43a41d1f698 /src | |
| parent | a4c5cc1e42559f1d92c6b7655680d11f1eead703 (diff) | |
| parent | 15814256e2fbbc2702422fd3d09d3a0f6be60006 (diff) | |
| download | PROJ-f2efd1d73c6c56531280604fd27c73fb13b41206.tar.gz PROJ-f2efd1d73c6c56531280604fd27c73fb13b41206.zip | |
Merge pull request #1892 from rouault/add_projinfo_searchpaths
projinfo: add --searchpaths switch
Diffstat (limited to 'src')
| -rw-r--r-- | src/4D_api.cpp | 19 | ||||
| -rw-r--r-- | src/apps/projinfo.cpp | 33 | ||||
| -rw-r--r-- | src/filemanager.cpp | 66 | ||||
| -rw-r--r-- | src/filemanager.hpp | 5 | ||||
| -rw-r--r-- | src/proj_internal.h | 3 |
5 files changed, 99 insertions, 27 deletions
diff --git a/src/4D_api.cpp b/src/4D_api.cpp index dabd44a0..91510c35 100644 --- a/src/4D_api.cpp +++ b/src/4D_api.cpp @@ -1484,23 +1484,10 @@ PJ_INFO proj_info (void) { /* build search path string */ auto ctx = pj_get_default_ctx(); if (!ctx || ctx->search_paths.empty()) { - // Env var mostly for testing purposes and being independent from - // an existing installation - const char* ignoreUserWritableDirectory = - getenv("PROJ_SKIP_READ_USER_WRITABLE_DIRECTORY"); - if( ignoreUserWritableDirectory == nullptr || - ignoreUserWritableDirectory[0] == '\0' ) { - buf = path_append(buf, - pj_context_get_user_writable_directory(ctx, false).c_str(), - &buf_size); - } - const std::string envPROJ_LIB = NS_PROJ::FileManager::getProjLibEnvVar(ctx); - buf = path_append(buf, envPROJ_LIB.empty() ? nullptr : envPROJ_LIB.c_str(), &buf_size); -#ifdef PROJ_LIB - if (envPROJ_LIB.empty()) { - buf = path_append(buf, PROJ_LIB, &buf_size); + const auto searchpaths = pj_get_default_searchpaths(ctx); + for( const auto& path: searchpaths ) { + buf = path_append(buf, path.c_str(), &buf_size); } -#endif } else { for (const auto &path : ctx->search_paths) { buf = path_append(buf, path.c_str(), &buf_size); diff --git a/src/apps/projinfo.cpp b/src/apps/projinfo.cpp index 27ea278a..9cd9b2ee 100644 --- a/src/apps/projinfo.cpp +++ b/src/apps/projinfo.cpp @@ -98,8 +98,9 @@ static void usage() { << std::endl << " [--identify] [--3d]" << std::endl << " [--c-ify] [--single-line]" << std::endl - << " {object_definition} | (-s {srs_def} -t {srs_def})" - << std::endl; + << " --searchpaths | --remote-data | " + "{object_definition} |" + << " (-s {srs_def} -t {srs_def})" << std::endl; std::cerr << std::endl; std::cerr << "-o: formats is a comma separated combination of: " "all,default,PROJ,WKT_ALL,WKT2:2015,WKT2:2019,WKT1:GDAL," @@ -1057,6 +1058,34 @@ int main(int argc, char **argv) { outputOpt.strict = false; } else if (ci_equal(arg, "--3d")) { promoteTo3D = true; + } else if (ci_equal(arg, "--searchpaths")) { +#ifdef _WIN32 + constexpr char delim = ';'; +#else + constexpr char delim = ':'; +#endif + const auto paths = split(proj_info().searchpath, delim); + for (const auto &path : paths) { + std::cout << path << std::endl; + } + std::exit(0); + } else if (ci_equal(arg, "--remote-data")) { +#ifdef CURL_ENABLED + if (pj_context_is_network_enabled(nullptr)) { + std::cout << "Status: enabled" << std::endl; + std::cout << "URL: " << pj_context_get_url_endpoint(nullptr) + << std::endl; + } else { + std::cout << "Status: disabled" << std::endl; + std::cout << "Reason: not enabled in proj.ini or " + "PROJ_NETWORK=ON not specified" + << std::endl; + } +#else + std::cout << "Status: disabled" << std::endl; + std::cout << "Reason: build without Curl support" << std::endl; +#endif + std::exit(0); } else if (arg == "-?" || arg == "--help") { usage(); } else if (arg[0] == '-') { diff --git a/src/filemanager.cpp b/src/filemanager.cpp index 005e734b..9263e9fe 100644 --- a/src/filemanager.cpp +++ b/src/filemanager.cpp @@ -1196,9 +1196,8 @@ static bool is_rel_or_absolute_filename(const char *name) { // --------------------------------------------------------------------------- #ifdef _WIN32 -static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, - const char *name, - std::string &out) { + +static std::string pj_get_win32_projlib() { /* Check if proj.db lieves in a share/proj dir parallel to bin/proj.dll */ /* Based in * https://stackoverflow.com/questions/9112893/how-to-get-path-to-executable-in-c-running-on-windows @@ -1214,10 +1213,10 @@ static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, DWORD last_error = GetLastError(); if (result == 0) { - return nullptr; + return std::string(); } else if (result == path_size - 1) { if (ERROR_INSUFFICIENT_BUFFER != last_error) { - return nullptr; + return std::string(); } path_size = path_size * 2; } else { @@ -1227,18 +1226,33 @@ static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, // Now remove the program's name. It was (example) // "C:\programs\gmt6\bin\gdal_translate.exe" wout.resize(wcslen(wout.c_str())); - out = NS_PROJ::WStringToUTF8(wout); + std::string out = NS_PROJ::WStringToUTF8(wout); size_t k = out.size(); while (k > 0 && out[--k] != '\\') { } out.resize(k); - out += "/../share/proj/"; + out += "/../share/proj"; + return out; +} + +// --------------------------------------------------------------------------- + +static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, + const char *name, + std::string &out) { + + out = pj_get_win32_projlib(); + if (out.empty()) { + return nullptr; + } + out += '/'; out += name; return NS_PROJ::FileManager::exists(ctx, out.c_str()) ? out.c_str() : nullptr; } + #endif /************************************************************************/ @@ -1402,6 +1416,41 @@ pj_open_lib_internal(projCtx ctx, const char *name, const char *mode, } /************************************************************************/ +/* pj_get_default_searchpaths() */ +/************************************************************************/ + +std::vector<std::string> pj_get_default_searchpaths(PJ_CONTEXT *ctx) { + std::vector<std::string> ret; + + // Env var mostly for testing purposes and being independent from + // an existing installation + const char *ignoreUserWritableDirectory = + getenv("PROJ_SKIP_READ_USER_WRITABLE_DIRECTORY"); + if (ignoreUserWritableDirectory == nullptr || + ignoreUserWritableDirectory[0] == '\0') { + ret.push_back(pj_context_get_user_writable_directory(ctx, false)); + } + const std::string envPROJ_LIB = NS_PROJ::FileManager::getProjLibEnvVar(ctx); + if (!envPROJ_LIB.empty()) { + ret.push_back(envPROJ_LIB); + } +#ifdef _WIN32 + if (envPROJ_LIB.empty()) { + const std::string win32Dir = pj_get_win32_projlib(); + if (!win32Dir.empty()) { + ret.push_back(win32Dir); + } + } +#endif +#ifdef PROJ_LIB + if (envPROJ_LIB.empty()) { + ret.push_back(PROJ_LIB); + } +#endif + return ret; +} + +/************************************************************************/ /* pj_open_file_with_manager() */ /************************************************************************/ @@ -1546,6 +1595,9 @@ int pj_find_file(projCtx ctx, const char *short_filename, /************************************************************************/ std::string pj_context_get_url_endpoint(PJ_CONTEXT *ctx) { + if (ctx == nullptr) { + ctx = pj_get_default_ctx(); + } if (!ctx->endpoint.empty()) { return ctx->endpoint; } diff --git a/src/filemanager.hpp b/src/filemanager.hpp index 554bd325..787af342 100644 --- a/src/filemanager.hpp +++ b/src/filemanager.hpp @@ -29,6 +29,8 @@ #define FILEMANAGER_HPP_INCLUDED #include <memory> +#include <string> +#include <vector> #include "proj.h" #include "proj/util.hpp" @@ -92,9 +94,10 @@ class File { std::unique_ptr<File> pj_network_file_open(PJ_CONTEXT *ctx, const char *filename); - NS_PROJ_END +std::vector<std::string> pj_get_default_searchpaths(PJ_CONTEXT *ctx); + //! @endcond Doxygen_Suppress #endif // FILEMANAGER_HPP_INCLUDED diff --git a/src/proj_internal.h b/src/proj_internal.h index a587c037..69d583fc 100644 --- a/src/proj_internal.h +++ b/src/proj_internal.h @@ -863,7 +863,8 @@ PJ *pj_create_argv_internal (PJ_CONTEXT *ctx, int argc, char **argv); // For use by projinfo bool PROJ_DLL pj_context_is_network_enabled(PJ_CONTEXT* ctx); -std::string pj_context_get_url_endpoint(PJ_CONTEXT* ctx); +// For use by projinfo +std::string PROJ_DLL pj_context_get_url_endpoint(PJ_CONTEXT* ctx); void pj_load_ini(PJ_CONTEXT* ctx); |
