OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GDAL.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Geospatial/GDAL.h"
18 #include <array>
19 #include <string>
20 
21 #include <gdal.h>
22 #include <gdal_priv.h>
23 
24 #include "Logger/Logger.h"
26 
27 #ifdef _MSC_VER
28 #include <Windows.h>
29 #endif
30 
31 namespace Geospatial {
32 
33 namespace {
34 
35 void gdal_error_handler(CPLErr err_class, int err_no, const char* err_msg) {
36  CHECK(err_class >= CE_None && err_class <= CE_Fatal);
37  static constexpr std::array<const char*, 5> err_class_strings{
38  "Info",
39  "Debug",
40  "Warning",
41  "Failure",
42  "Fatal",
43  };
44  std::string log_msg = std::string("GDAL ") + err_class_strings[err_class] + ": " +
45  err_msg + " (" + std::to_string(err_no) + ")";
46  if (err_class >= CE_Failure) {
47  throw std::runtime_error(log_msg);
48  } else {
49  LOG(INFO) << log_msg;
50  }
51 }
52 
53 } // namespace
54 
55 bool GDAL::initialized_ = false;
56 
57 std::mutex GDAL::init_mutex_;
58 
59 void GDAL::init() {
60  // this should not be called from multiple threads, but...
61  std::lock_guard<std::mutex> guard(init_mutex_);
62 
63  // init under mutex
64  if (!initialized_) {
65  // FIXME(andrewseidl): investigate if CPLPushFinderLocation can be public
66 #ifdef _MSC_VER
67  SetEnvironmentVariable(
68  L"GDAL_DATA",
69  reinterpret_cast<LPCWSTR>(
70  std::string(omnisci::get_root_abs_path() + "/ThirdParty/gdal-data").c_str()));
71 #else
72  setenv("GDAL_DATA",
73  std::string(omnisci::get_root_abs_path() + "/ThirdParty/gdal-data").c_str(),
74  true);
75 #endif
76 
77 #ifndef _MSC_VER // TODO
78  // configure SSL certificate path (per S3Archive::init_for_read)
79  // in a production build, GDAL and Curl will have been built on
80  // CentOS, so the baked-in system path will be wrong for Ubuntu
81  // and other Linux distros. Unless the user is deliberately
82  // overriding it by setting SSL_CERT_FILE explicitly in the server
83  // environment, we set it to whichever CA bundle directory exists
84  // on the machine we're running on
85  static constexpr std::array<const char*, 6> known_ca_paths{
86  "/etc/ssl/certs/ca-certificates.crt",
87  "/etc/pki/tls/certs/ca-bundle.crt",
88  "/usr/share/ssl/certs/ca-bundle.crt",
89  "/usr/local/share/certs/ca-root.crt",
90  "/etc/ssl/cert.pem",
91  "/etc/ssl/ca-bundle.pem"};
92  for (const auto& known_ca_path : known_ca_paths) {
93  if (boost::filesystem::exists(known_ca_path)) {
94  LOG(INFO) << "GDAL SSL Certificate path: " << known_ca_path;
95  setenv("SSL_CERT_FILE", known_ca_path, false); // no overwrite
96  break;
97  }
98  }
99 #endif
100 
101  GDALAllRegister();
102  OGRRegisterAll();
103  CPLSetErrorHandler(*gdal_error_handler);
104  LOG(INFO) << "GDAL Initialized: " << GDALVersionInfo("--version");
105  initialized_ = true;
106  }
107 }
108 
110 #if (GDAL_VERSION_MAJOR > 2) || (GDAL_VERSION_MAJOR == 2 && GDAL_VERSION_MINOR >= 2)
111  return true;
112 #else
113  return false;
114 #endif
115 }
116 
117 bool GDAL::supportsDriver(const char* driver_name) {
118  init();
119  return GetGDALDriverManager()->GetDriverByName(driver_name) != nullptr;
120 }
121 
122 } // namespace Geospatial
void gdal_error_handler(CPLErr err_class, int err_no, const char *err_msg)
Definition: GDAL.cpp:35
#define LOG(tag)
Definition: Logger.h:188
static void init()
Definition: GDAL.cpp:59
std::string to_string(char const *&&v)
static bool initialized_
Definition: GDAL.h:30
static bool supportsDriver(const char *driver_name)
Definition: GDAL.cpp:117
static bool supportsNetworkFileAccess()
Definition: GDAL.cpp:109
static std::mutex init_mutex_
Definition: GDAL.h:31
#define CHECK(condition)
Definition: Logger.h:197
std::string get_root_abs_path()