src/terralib/qt/plugins/datasource/gdal/Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9 
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19 
20 /*!
21 \file terralib/qt/plugins/datasource/gdal/Utils.cpp
22 
23 \brief Utility functions for the GDAL data source widget plugin.
24 */
25 
26 // TerraLib
27 #include "../../../../core/logger/Logger.h"
28 #include "../../../../core/translator/Translator.h"
29 #include "../../../../dataaccess/dataset/DataSetType.h"
30 #include "../../../../dataaccess/datasource/DataSourceManager.h"
31 #include "../../../../dataaccess/utils/Utils.h"
32 #include "../../../../maptools/AbstractLayer.h"
33 #include "../../../../srs/SpatialReferenceSystemManager.h"
34 #include "../../../widgets/layer/utils/DataSet2Layer.h"
35 #include "../../../af/ApplicationController.h"
36 #include "../../../af/events/LayerEvents.h"
37 #include "Utils.h"
38 
39 // Qt
40 #include <QApplication>
41 #include <QDir>
42 #include <QFileInfo>
43 #include <QMessageBox>
44 
45 // STL
46 #include <cassert>
47 
48 // Boost
49 #include <boost/uuid/random_generator.hpp>
50 #include <boost/uuid/uuid_io.hpp>
51 #include <boost/filesystem.hpp>
52 
53 std::list<te::da::DataSetTypePtr> te::qt::plugins::gdal::GetDataSetsInfo(const te::da::DataSourceInfoPtr& info)
54 {
55  std::list<te::da::DataSetTypePtr> res;
56 
57  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(info->getId(), info->getType(), info->getConnInfo());
58 
59  std::vector<std::string> dsets = ds->getDataSetNames();
60 
61  std::vector<std::string>::iterator it;
62 
63  for (it = dsets.begin(); it != dsets.end(); ++it)
64  res.push_back(te::da::DataSetTypePtr(ds->getDataSetType(*it).release()));
65 
66  return res;
67 }
68 
70 {
72  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(info->getId(), info->getType(), info->getConnInfo());
73 
74  te::da::DataSetTypePtr dss = ds->getDataSetType(fileName);
75 
76  try
77  {
78  te::qt::widgets::DataSet2Layer layer(info->getId());
79  res = layer(dss);
80  }
81  catch (std::exception& e)
82  {
83  #ifdef TERRALIB_LOGGER_ENABLED
84  std::string str = "GDAL Plug-in - GetLayer";
85  str += e.what();
86  TE_LOG_DEBUG(str);
87  #endif // TERRALIB_LOGGER_ENABLED
88 
89  QMessageBox::warning(nullptr, TE_TR("GDAL functions"), TE_TR("There were issues while reading the layer from the requested GDAL data source."));
90  }
91 
92  return res;
93 }
94 
95 void te::qt::plugins::gdal::GetLayers(const te::da::DataSourceInfoPtr& info, std::list<te::map::AbstractLayerPtr>& layers)
96 {
97  std::list<te::map::AbstractLayerPtr> res;
98  std::list<te::da::DataSetTypePtr> dss = GetDataSetsInfo(info);
99 
100  try
101  {
102  std::transform(dss.begin(), dss.end(), std::back_inserter(layers), te::qt::widgets::DataSet2Layer(info->getId()));
103  }
104  catch (std::exception& e)
105  {
106  #ifdef TERRALIB_LOGGER_ENABLED
107  std::string str = "GDAL Plug-in - GetLayers";
108  str += e.what();
109  TE_LOG_DEBUG(str);
110  #endif // TERRALIB_LOGGER_ENABLED
111 
112  QMessageBox::warning(nullptr, TE_TR("GDAL functions"), TE_TR("There were issues while reading some layer(s) from the requested GDAL data source."));
113  }
114 }
115 
116 void te::qt::plugins::gdal::CreateLayers(QStringList fileNames, std::list<te::map::AbstractLayerPtr>& layers)
117 {
118  for (QStringList::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
119  {
121 
122  ds->setAccessDriver("GDAL");
123 
124  std::string fpath = it->toUtf8().data();
125  ds->setConnInfo("file://" + fpath);
126 
127  ds->setDescription("A single raster file");
128 
129  boost::uuids::basic_random_generator<boost::mt19937> gen;
130  boost::uuids::uuid u = gen();
131  std::string id = boost::uuids::to_string(u);
132 
133  ds->setId(id);
134 
135  boost::filesystem::path mpath(it->toUtf8().data());
136 
137  std::string fileBaseName = mpath.stem().string();
138 
139  ds->setTitle(fileBaseName);
140 
141  ds->setType("GDAL");
142 
144  ds = te::da::DataSourceInfoManager::getInstance().getByConnInfo(ds->getConnInfoAsString());
145 
146  GetLayers(ds, layers);
147  }
148 
149  te::map::AbstractLayerPtr parentLayer(nullptr);
150  te::qt::af::evt::LayersAdded added(layers, parentLayer);
152 }
153 
std::list< te::da::DataSetTypePtr > GetDataSetsInfo(const te::da::DataSourceInfoPtr &info)
void CreateLayers(QStringList fileNames, std::list< te::map::AbstractLayerPtr > &layers)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
This event signals that a list of layers have been created.
Definition: LayerEvents.h:95
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void GetLayers(const te::da::DataSourceInfoPtr &info, std::list< te::map::AbstractLayerPtr > &layers)
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
te::map::AbstractLayerPtr GetLayer(const te::da::DataSourceInfoPtr &info, std::string fileName)
std::list< te::map::AbstractLayerPtr > GetLayers(const QModelIndexList &lst)
Utility functions for the data access module.
std::list< te::da::DataSetTypePtr > GetDataSetsInfo(const te::da::DataSourceInfoPtr &info)
#define TE_LOG_DEBUG(message)
Use this tag in order to log a message to the TerraLib default logger with the DEBUG level...
Definition: Logger.h:304
A class that represents a data source component.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr