src/terralib/ogr/Plugin.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/ogr/Plugin.cpp
24 
25  \brief Plugin interface for dynamic loading of OGR Driver for TerraLib.
26 */
27 
28 // TerraLib
29 #include "../Defines.h"
30 #include "../core/filesystem/FileSystem.h"
31 #include "../core/logger/Logger.h"
32 #include "../core/plugin/CppPlugin.h"
33 #include "../core/translator/Translator.h"
34 #include "../core/utils/Platform.h"
35 #include "../dataaccess/datasource/DataSourceFactory.h"
36 #include "../dataaccess/datasource/DataSourceManager.h"
37 #include "../dataaccess/query/BinaryOpEncoder.h"
38 #include "../dataaccess/query/FunctionEncoder.h"
39 #include "../dataaccess/query/SQLDialect.h"
40 #include "../dataaccess/query/SQLFunctionEncoder.h"
41 #include "../dataaccess/query/TemplateEncoder.h"
42 #include "../dataaccess/query/UnaryOpEncoder.h"
43 #include "DataSource.h"
44 #include "Globals.h"
45 #include "Utils.h"
46 
47 // Boost
48 #include <boost/filesystem.hpp>
49 
50 // OGR
51 #include <cpl_conv.h>
52 #include <ogr_api.h>
53 namespace te
54 {
55  namespace ogr
56  {
58 
60  {
61  if(m_initialized)
62  return;
63 
64  // for all platforms, first look at an environment variable
65  // defined by macro TERRALIB_GDAL_DATA.
66  // note: TERRALIB_GDAL_DATA is detected by CMAKE.
67  // note: installed versions on developers machine may look for this
68  // version of GDAL
69  std::string gdal_data_dir(TERRALIB_GDAL_DATA);
70 
71  // if the above variable is not set or it points to an invalid directory
72  if(gdal_data_dir.empty() ||
73  !te::core::FileSystem::isDirectory(gdal_data_dir))
74  {
75  // search for GDAL in a PATH relative to TerraLib.
76  // note: each SO will look in a different folder
77  #if defined(TE_PLATFORM) && defined(TE_PLATFORMCODE_MSWINDOWS)
78  #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
79  gdal_data_dir = te::core::FindInTerraLibPath("/share/data");
80  #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
81  gdal_data_dir = te::core::FindInTerraLibPath("/share/gdal");
82  #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
83  gdal_data_dir = te::core::FindInTerraLibPath("/share/gdal");
84  #else
85  #error "unsupported plataform: please, contact terralib-team@terralib.org"
86  #endif
87  #else
88  #error \
89  "the macro TE_PLATFORM is not set, please, contact terralib-team@terralib.org"
90  #endif
91  }
92 
93  if(!gdal_data_dir.empty())
94  {
95  CPLSetConfigOption("GDAL_DATA", gdal_data_dir.c_str());
96  }
97 
98  CPLSetConfigOption("GDAL_DATA", gdal_data_dir.c_str());
99 
100  CPLSetConfigOption("GDAL_PAM_ENABLED", "NO");
101 
102  CPLPushErrorHandler((CPLErrorHandler)te::ogr::OGRErrorHandler);
103 
104  // registers all format drivers built into OGR.
105  OGRRegisterAll();
106 
107  // it initializes the OGR Factory support
109 
110  #include "OGRDialect.h"
111 
112  #if GDAL_VERSION_NUM >= 1900
113  const char* currentValue = CPLGetConfigOption("GDAL_FIX_ESRI_WKT", "");
114  if(strcmp(currentValue, "") == 0) // to avoid override
115  CPLSetConfigOption("GDAL_FIX_ESRI_WKT", "GEOGCS");
116  #endif
117 
118  // initializing the static mutex
119  getStaticMutex();
120 
121  TE_LOG_TRACE(TE_TR("TerraLib OGR driver startup!"));
122 
123  m_initialized = true;
124  }
125 
127  {
128  if(!m_initialized)
129  return;
130 
131  // it finalizes the OGR factory support.
133 
134  // free OGR registered drivers
136 
137  OGRCleanupAll();
138 
139  DataSource::setDialect(nullptr);
140 
141  TE_LOG_TRACE(TE_TR("TerraLib OGR driver shutdown!"));
142 
143  m_initialized = false;
144  }
145  TERRALIB_CPP_PLUGIN_END(te::ogr::Plugin)
146  }
147 }
TERRALIB_CPP_PLUGIN_BEGIN(Plugin) TERRALIB_CPP_PLUGIN_STARTUP
TE_LOG_TRACE(TE_TR("TerraLib OGR driver shutdown!"))
#define OGR_DRIVER_IDENTIFIER
The OGR driver identifier string.
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
#define TERRALIB_CPP_PLUGIN_END(plugin_class_name)
OGRCleanupAll()
Utility functions for OGR support.
TEOGREXPORT boost::mutex & getStaticMutex()
Returns a reference to a static mutex initialized when this module is initialized.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static void remove(const std::string &dsType)
it finalizes the GDAL factory support.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
URI C++ Library.
Definition: Attributes.h:37
#define TERRALIB_CPP_PLUGIN_STARTUP
te::da::DataSource * Build(const te::core::URI &uri)
A class for data providers of OGR.
void OGRErrorHandler(CPLErr eErrClass, int errNo, const char *msg)
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
static void add(const std::string &dsType, FactoryFnctType f)
static void setDialect(te::da::SQLDialect *dialect)
An static class with global definitions.