23 #include <gdal_priv.h>
24 #include <ogrsf_frmts.h>
26 #include <boost/filesystem.hpp>
35 namespace Geospatial {
40 CHECK(err_class >= CE_None && err_class <= CE_Fatal);
41 if (err_no == CPLE_NotSupported) {
45 static constexpr std::array<const char*, 5> err_class_strings{
52 std::string log_msg = std::string(
"GDAL ") + err_class_strings[err_class] +
": " +
54 if (err_class >= CE_Failure) {
91 #ifndef _MSC_VER // TODO
99 static constexpr std::array<const char*, 6> known_ca_paths{
100 "/etc/ssl/certs/ca-certificates.crt",
101 "/etc/pki/tls/certs/ca-bundle.crt",
102 "/usr/share/ssl/certs/ca-bundle.crt",
103 "/usr/local/share/certs/ca-root.crt",
105 "/etc/ssl/ca-bundle.pem"};
106 for (
const auto& known_ca_path : known_ca_paths) {
107 if (boost::filesystem::exists(known_ca_path)) {
108 LOG(
INFO) <<
"GDAL SSL Certificate path: " << known_ca_path;
109 setenv(
"SSL_CERT_FILE", known_ca_path,
false);
118 LOG(
INFO) <<
"GDAL Initialized: " << GDALVersionInfo(
"--version");
124 #if (GDAL_VERSION_MAJOR > 2) || (GDAL_VERSION_MAJOR == 2 && GDAL_VERSION_MINOR >= 2)
135 return GetGDALDriverManager()->GetDriverByName(driver_name.c_str()) !=
nullptr;
139 const std::string& s3_endpoint,
140 const std::string& s3_access_key,
141 const std::string& s3_secret_key,
142 const std::string& s3_session_token) {
147 if (s3_region.size()) {
148 CPLSetConfigOption(
"AWS_REGION", s3_region.c_str());
150 CPLSetConfigOption(
"AWS_REGION",
nullptr);
152 if (s3_endpoint.size()) {
153 CPLSetConfigOption(
"AWS_S3_ENDPOINT", s3_endpoint.c_str());
155 CPLSetConfigOption(
"AWS_S3_ENDPOINT",
nullptr);
157 if (s3_access_key.size()) {
158 CPLSetConfigOption(
"AWS_ACCESS_KEY_ID", s3_access_key.c_str());
160 CPLSetConfigOption(
"AWS_ACCESS_KEY_ID",
nullptr);
162 if (s3_secret_key.size()) {
163 CPLSetConfigOption(
"AWS_SECRET_ACCESS_KEY", s3_secret_key.c_str());
165 CPLSetConfigOption(
"AWS_SECRET_ACCESS_KEY",
nullptr);
167 if (s3_session_token.size()) {
168 CPLSetConfigOption(
"AWS_SESSION_TOKEN", s3_session_token.c_str());
170 CPLSetConfigOption(
"AWS_SESSION_TOKEN",
nullptr);
174 if (s3_access_key.size() || s3_secret_key.size()) {
175 CPLSetConfigOption(
"AWS_NO_SIGN_REQUEST",
nullptr);
177 CPLSetConfigOption(
"AWS_NO_SIGN_REQUEST",
"YES");
187 unsigned int open_flags{0u};
188 switch (source_type) {
190 open_flags = GDAL_OF_VECTOR | GDAL_OF_RASTER;
193 open_flags = GDAL_OF_VECTOR;
196 open_flags = GDAL_OF_RASTER;
199 CHECK(
false) <<
"Invalid datasource source type";
204 auto* datasource =
static_cast<OGRDataSource*
>(
205 GDALOpenEx(name.c_str(), open_flags | GDAL_OF_READONLY,
nullptr,
nullptr,
nullptr));
226 auto* driver = datasource->GetDriver();
232 auto const is_vector =
getMetadataString(driver->GetMetadata(),
"DCAP_VECTOR") ==
"YES";
233 auto const is_raster =
getMetadataString(driver->GetMetadata(),
"DCAP_RASTER") ==
"YES";
236 if (is_vector && !is_raster) {
238 }
else if (is_raster && !is_vector) {
247 std::vector<std::string> strings;
250 strings.emplace_back(*metadata);
260 LOG(
INFO) <<
"DEBUG: Metadata domains for object '" <<
object->GetDescription() <<
"'";
261 LOG(
INFO) <<
"DEBUG: (default)";
266 auto const metadata_domains =
unpackMetadata(object->GetMetadataDomainList());
267 for (
auto const& domain : metadata_domains) {
268 LOG(
INFO) <<
"DEBUG: " << domain;
269 auto const metadata =
unpackMetadata(object->GetMetadata(domain.c_str()));
270 for (
auto const& str : metadata) {
278 auto const key_len = key.length();
280 for (
auto const& str : strings) {
281 if (str.substr(0, key_len) == key) {
282 return str.substr(key_len + 1, std::string::npos);
285 return std::string();
290 GDALClose(datasource);
296 OGRFeature::DestroyFeature(feature);
302 OGRSpatialReference::DestroySpatialReference(reference);
307 OGRCoordinateTransformation* transformation) {
308 if (transformation) {
309 delete transformation;
void gdal_error_handler(CPLErr err_class, int err_no, const char *err_msg)
std::string get_root_abs_path()
static std::vector< std::string > unpackMetadata(char **metadata)
static void logMetadata(GDALMajorObject *object)
std::string default_metadata(const std::string &metadata)
void operator()(OGRDataSource *datasource)
void operator()(OGRFeature *feature)
void operator()(OGRSpatialReference *ref)
static void setAuthorizationTokens(const std::string &s3_region, const std::string &s3_endpoint, const std::string &s3_access_key, const std::string &s3_secret_key, const std::string &s3_session_token)
static bool supportsNetworkFileAccess()
static std::mutex init_mutex_
std::unique_ptr< OGRDataSource, DataSourceDeleter > DataSourceUqPtr
static import_export::SourceType getDataSourceType(const std::string &name)
static bool supportsDriver(const std::string &driver_name)
static DataSourceUqPtr openDataSource(const std::string &name, const import_export::SourceType source_type)
static std::string getMetadataString(char **metadata, const std::string &key)