1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
/******************************************************************************
*
* Project: GDAL
* Purpose: PROJ-related functionality
* Author: Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "ogr_proj_p.h"
#include "ogr_srs_api.h"
#include "proj.h"
#ifndef _WIN32
#include <sys/types.h>
#include <unistd.h>
#if defined(HAVE_PTHREAD_ATFORK)
#include <pthread.h>
#endif
#endif
#include <mutex>
#include <vector>
// #define SIMUL_OLD_PROJ6
/*! @cond Doxygen_Suppress */
static void osr_proj_logger(void * /* user_data */, int level,
const char *message)
{
if (level == PJ_LOG_ERROR)
{
CPLError(CE_Failure, CPLE_AppDefined, "PROJ: %s", message);
}
else if (level == PJ_LOG_DEBUG)
{
CPLDebug("PROJ", "%s", message);
}
else if (level == PJ_LOG_TRACE)
{
CPLDebug("PROJ_TRACE", "%s", message);
}
}
static unsigned g_searchPathGenerationCounter = 0;
static unsigned g_auxDbPathsGenerationCounter = 0;
static std::mutex g_oSearchPathMutex;
static CPLStringList g_aosSearchpaths;
static CPLStringList g_aosAuxDbPaths;
#if PROJ_VERSION_MAJOR >= 7
static int g_projNetworkEnabled = -1;
static unsigned g_projNetworkEnabledGenerationCounter = 0;
#endif
#if !defined(_WIN32) && defined(HAVE_PTHREAD_ATFORK)
static bool g_bForkOccurred = false;
static void ForkOccurred(void)
{
g_bForkOccurred = true;
}
#endif
struct OSRPJContextHolder
{
unsigned searchPathGenerationCounter = 0;
unsigned auxDbPathsGenerationCounter = 0;
#if PROJ_VERSION_MAJOR >= 7
unsigned projNetworkEnabledGenerationCounter = 0;
#endif
PJ_CONTEXT *context = nullptr;
OSRProjTLSCache oCache;
#if !defined(_WIN32)
#if !defined(HAVE_PTHREAD_ATFORK)
pid_t curpid = 0;
#endif
#if defined(SIMUL_OLD_PROJ6) || \
!(PROJ_VERSION_MAJOR > 6 || PROJ_VERSION_MINOR >= 2)
std::vector<PJ_CONTEXT *> oldcontexts{};
#endif
#endif
#if !defined(_WIN32)
OSRPJContextHolder()
: oCache(init())
#if !defined(HAVE_PTHREAD_ATFORK)
,
curpid(getpid())
#endif
{
#if HAVE_PTHREAD_ATFORK
pthread_atfork(nullptr, nullptr, ForkOccurred);
#endif
init();
}
#else
OSRPJContextHolder() : oCache(init())
{
}
#endif
~OSRPJContextHolder();
PJ_CONTEXT *init();
void deinit();
private:
OSRPJContextHolder(const OSRPJContextHolder &) = delete;
OSRPJContextHolder &operator=(const OSRPJContextHolder &) = delete;
};
PJ_CONTEXT *OSRPJContextHolder::init()
{
if (!context)
{
context = proj_context_create();
proj_log_func(context, nullptr, osr_proj_logger);
}
return context;
}
OSRPJContextHolder::~OSRPJContextHolder()
{
deinit();
}
void OSRPJContextHolder::deinit()
{
searchPathGenerationCounter = 0;
oCache.clear();
// Destroy context in last
proj_context_destroy(context);
context = nullptr;
#if defined(SIMUL_OLD_PROJ6) || \
(!defined(_WIN32) && !(PROJ_VERSION_MAJOR > 6 || PROJ_VERSION_MINOR >= 2))
for (size_t i = 0; i < oldcontexts.size(); i++)
{
proj_context_destroy(oldcontexts[i]);
}
oldcontexts.clear();
#endif
}
#ifdef WIN32
// Currently thread_local and C++ objects don't work well with DLL on Windows
static void FreeProjTLSContextHolder(void *pData)
{
delete static_cast<OSRPJContextHolder *>(pData);
}
static OSRPJContextHolder &GetProjTLSContextHolder()
{
static OSRPJContextHolder dummy;
int bMemoryErrorOccurred = false;
void *pData = CPLGetTLSEx(CTLS_PROJCONTEXTHOLDER, &bMemoryErrorOccurred);
if (bMemoryErrorOccurred)
{
return dummy;
}
if (pData == nullptr)
{
auto pHolder = new OSRPJContextHolder();
CPLSetTLSWithFreeFuncEx(CTLS_PROJCONTEXTHOLDER, pHolder,
FreeProjTLSContextHolder,
&bMemoryErrorOccurred);
if (bMemoryErrorOccurred)
{
delete pHolder;
return dummy;
}
return *pHolder;
}
return *static_cast<OSRPJContextHolder *>(pData);
}
#else
static thread_local OSRPJContextHolder g_tls_projContext;
static OSRPJContextHolder &GetProjTLSContextHolder()
{
OSRPJContextHolder &l_projContext = g_tls_projContext;
// Detect if we are now running in a child process created by fork()
// In that situation we must make sure *not* to use the same underlying
// file open descriptor to the sqlite3 database, since seeks&reads in one
// of the parent or child will affect the other end.
#if defined(HAVE_PTHREAD_ATFORK)
if (g_bForkOccurred)
#else
const pid_t curpid = getpid();
if (curpid != l_projContext.curpid)
#endif
{
#if defined(HAVE_PTHREAD_ATFORK)
g_bForkOccurred = false;
#else
l_projContext.curpid = curpid;
#endif
#if defined(SIMUL_OLD_PROJ6) || \
!(PROJ_VERSION_MAJOR > 6 || PROJ_VERSION_MINOR >= 2)
// PROJ < 6.2 ? Recreate new context
l_projContext.oldcontexts.push_back(l_projContext.context);
l_projContext.context = nullptr;
l_projContext.init();
#else
const auto osr_proj_logger_none = [](void *, int, const char *) {};
proj_log_func(l_projContext.context, nullptr, osr_proj_logger_none);
proj_context_set_autoclose_database(l_projContext.context, true);
// dummy call to cause the database to be closed
proj_context_get_database_path(l_projContext.context);
proj_context_set_autoclose_database(l_projContext.context, false);
proj_log_func(l_projContext.context, nullptr, osr_proj_logger);
#endif
}
return l_projContext;
}
#endif
PJ_CONTEXT *OSRGetProjTLSContext()
{
auto &l_projContext = GetProjTLSContextHolder();
// This .init() must be kept, even if OSRPJContextHolder constructor
// calls it. The reason is that OSRCleanupTLSContext() calls deinit(),
// so if reusing the object, we must re-init again.
l_projContext.init();
{
// If OSRSetPROJSearchPaths() has been called since we created the
// context, set the new search paths on the context.
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
if (l_projContext.searchPathGenerationCounter !=
g_searchPathGenerationCounter)
{
l_projContext.searchPathGenerationCounter =
g_searchPathGenerationCounter;
proj_context_set_search_paths(l_projContext.context,
g_aosSearchpaths.Count(),
g_aosSearchpaths.List());
}
if (l_projContext.auxDbPathsGenerationCounter !=
g_auxDbPathsGenerationCounter)
{
l_projContext.auxDbPathsGenerationCounter =
g_auxDbPathsGenerationCounter;
std::string oMainPath(
proj_context_get_database_path(l_projContext.context));
proj_context_set_database_path(l_projContext.context,
oMainPath.c_str(),
g_aosAuxDbPaths.List(), nullptr);
}
#if PROJ_VERSION_MAJOR >= 7
if (l_projContext.projNetworkEnabledGenerationCounter !=
g_projNetworkEnabledGenerationCounter)
{
l_projContext.projNetworkEnabledGenerationCounter =
g_projNetworkEnabledGenerationCounter;
proj_context_set_enable_network(l_projContext.context,
g_projNetworkEnabled);
}
#endif
}
return l_projContext.context;
}
/************************************************************************/
/* OSRGetProjTLSCache() */
/************************************************************************/
OSRProjTLSCache *OSRGetProjTLSCache()
{
auto &l_projContext = GetProjTLSContextHolder();
return &l_projContext.oCache;
}
void OSRProjTLSCache::clear()
{
m_oCacheEPSG.clear();
m_oCacheWKT.clear();
m_tlsContext = nullptr;
}
PJ_CONTEXT *OSRProjTLSCache::GetPJContext()
{
if (m_tlsContext == nullptr)
m_tlsContext = OSRGetProjTLSContext();
return m_tlsContext;
}
PJ *OSRProjTLSCache::GetPJForEPSGCode(int nCode, bool bUseNonDeprecated,
bool bAddTOWGS84)
{
const EPSGCacheKey key(nCode, bUseNonDeprecated, bAddTOWGS84);
auto cached = m_oCacheEPSG.getPtr(key);
if (cached)
{
return proj_clone(GetPJContext(), cached->get());
}
return nullptr;
}
void OSRProjTLSCache::CachePJForEPSGCode(int nCode, bool bUseNonDeprecated,
bool bAddTOWGS84, PJ *pj)
{
const EPSGCacheKey key(nCode, bUseNonDeprecated, bAddTOWGS84);
m_oCacheEPSG.insert(key, UniquePtrPJ(proj_clone(GetPJContext(), pj)));
}
PJ *OSRProjTLSCache::GetPJForWKT(const std::string &wkt)
{
auto cached = m_oCacheWKT.getPtr(wkt);
if (cached)
{
return proj_clone(GetPJContext(), cached->get());
}
return nullptr;
}
void OSRProjTLSCache::CachePJForWKT(const std::string &wkt, PJ *pj)
{
m_oCacheWKT.insert(wkt, UniquePtrPJ(proj_clone(GetPJContext(), pj)));
}
/************************************************************************/
/* OSRCleanupTLSContext() */
/************************************************************************/
void OSRCleanupTLSContext()
{
GetProjTLSContextHolder().deinit();
}
/*! @endcond */
/************************************************************************/
/* OSRSetPROJSearchPaths() */
/************************************************************************/
/** \brief Set the search path(s) for PROJ resource files.
*
* @param papszPaths NULL terminated list of directory paths.
* @since GDAL 3.0
*/
void OSRSetPROJSearchPaths(const char *const *papszPaths)
{
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
g_searchPathGenerationCounter++;
g_aosSearchpaths.Assign(CSLDuplicate(papszPaths), true);
}
/************************************************************************/
/* OSRGetPROJSearchPaths() */
/************************************************************************/
/** \brief Get the search path(s) for PROJ resource files.
*
* @return NULL terminated list of directory paths. To be freed with
* CSLDestroy()
* @since GDAL 3.0.3
*/
char **OSRGetPROJSearchPaths()
{
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
if (g_searchPathGenerationCounter > 0 && !g_aosSearchpaths.empty())
{
return CSLDuplicate(g_aosSearchpaths.List());
}
const char *pszSep =
#ifdef _WIN32
";"
#else
":"
#endif
;
return CSLTokenizeString2(proj_info().searchpath, pszSep, 0);
}
/************************************************************************/
/* OSRSetPROJAuxDbPaths() */
/************************************************************************/
/** \brief Set list of PROJ auxiliary database filenames.
*
* @param papszAux NULL-terminated list of auxiliary database filenames, or NULL
* @since GDAL 3.3
*
* @see OSRGetPROJAuxDbPaths, proj_context_set_database_path
*/
void OSRSetPROJAuxDbPaths(const char *const *papszAux)
{
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
g_auxDbPathsGenerationCounter++;
g_aosAuxDbPaths.Assign(CSLDuplicate(papszAux), true);
}
/************************************************************************/
/* OSRGetPROJAuxDbPaths() */
/************************************************************************/
/** \brief Get PROJ auxiliary database filenames.
*
* @return NULL terminated list of PROJ auxiliary database filenames. To be
* freed with CSLDestroy()
* @since GDAL 3.3.0
*
* @see OSRSetPROJAuxDbPaths, proj_context_set_database_path
*/
char **OSRGetPROJAuxDbPaths(void)
{
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
// Unfortunately, there is no getter for auxiliary database list at PROJ.
// So, return our copy for now.
return CSLDuplicate(g_aosAuxDbPaths.List());
}
/************************************************************************/
/* OSRSetPROJEnableNetwork() */
/************************************************************************/
/** \brief Enable or disable PROJ networking capabilities.
*
* @param enabled Set to TRUE to enable networking capabilities.
* @since GDAL 3.4 and PROJ 7
*
* @see OSRGetPROJEnableNetwork, proj_context_set_enable_network
*/
void OSRSetPROJEnableNetwork(int enabled)
{
#if PROJ_VERSION_MAJOR >= 7
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
if (g_projNetworkEnabled != enabled)
{
g_projNetworkEnabled = enabled;
g_projNetworkEnabledGenerationCounter++;
}
#else
if (enabled)
{
CPLError(CE_Failure, CPLE_NotSupported,
"OSRSetPROJEnableNetwork() requires PROJ >= 7");
}
#endif
}
/************************************************************************/
/* OSRGetPROJEnableNetwork() */
/************************************************************************/
/** \brief Get whether PROJ networking capabilities are enabled.
*
* @return TRUE if PROJ networking capabilities are enabled.
* @since GDAL 3.4 and PROJ 7
*
* @see OSRSetPROJEnableNetwork, proj_context_is_network_enabled
*/
int OSRGetPROJEnableNetwork(void)
{
#if PROJ_VERSION_MAJOR >= 7
std::lock_guard<std::mutex> oLock(g_oSearchPathMutex);
if (g_projNetworkEnabled < 0)
{
g_oSearchPathMutex.unlock();
const int ret = proj_context_is_network_enabled(OSRGetProjTLSContext());
g_oSearchPathMutex.lock();
g_projNetworkEnabled = ret;
}
return g_projNetworkEnabled;
#else
return FALSE;
#endif
}
/************************************************************************/
/* OSRGetPROJVersion() */
/************************************************************************/
/** \brief Get the PROJ version
*
* @param pnMajor Pointer to major version number, or NULL
* @param pnMinor Pointer to minor version number, or NULL
* @param pnPatch Pointer to patch version number, or NULL
* @since GDAL 3.0.1
*/
void OSRGetPROJVersion(int *pnMajor, int *pnMinor, int *pnPatch)
{
auto info = proj_info();
if (pnMajor)
*pnMajor = info.major;
if (pnMinor)
*pnMinor = info.minor;
if (pnPatch)
*pnPatch = info.patch;
}
|