ComposeBandsWizard.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/widgets/rp/ComposeBandsWizard.cpp
22 
23  \brief A Qt dialog that allows users to compose or decompose bands.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../common/STLUtils.h"
29 #include "../../../common/progress/ProgressManager.h"
30 #include "../../../dataaccess/dataset/DataSet.h"
31 #include "../../../dataaccess/utils/Utils.h"
32 #include "../../../raster/Raster.h"
33 #include "../../../rp/FeedersRaster.h"
34 #include "../../../rp/Functions.h"
35 #include "../../../maptools/Utils.h"
36 #include "../help/HelpPushButton.h"
37 #include "../layer/search/LayerSearchWidget.h"
38 #include "../layer/search/LayerSearchWizardPage.h"
39 #include "../progress/ProgressViewerDialog.h"
40 #include "ComposeBandsWizard.h"
41 #include "ComposeBandsWizardPage.h"
42 #include "../raster/RasterInfoWidget.h"
43 #include "RasterInfoWizardPage.h"
44 #include "Utils.h"
45 
46 // STL
47 #include <cassert>
48 
49 // Qt
50 #include <QMessageBox>
51 #include <QApplication>
52 
53 
55  : QWizard(parent)
56 {
57  //configure the wizard
58  this->setWizardStyle(QWizard::ModernStyle);
59  this->setWindowTitle(tr("Compose / Decompose Bands"));
60 
61  this->setOption(QWizard::HaveHelpButton, true);
62  this->setOption(QWizard::HelpButtonOnRight, false);
63 
65 
66  this->setButton(QWizard::HelpButton, helpButton);
67 
68  helpButton->setPageReference("plugins/rp/rp_compose_bands.html");
69 
70  connect(this, SIGNAL(currentIdChanged(int)), SLOT(onPageChanged(int)));
71 
72  addPages();
73 }
74 
76 
78 {
79  if(currentPage() == m_layerSearchPage.get())
80  {
81  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
82 
83  m_composeBandsPage->setList(list);
84 
85  return m_layerSearchPage->isComplete();
86  }
87  else if(currentPage() == m_composeBandsPage.get())
88  {
89  return m_composeBandsPage->isComplete();
90  }
91  else if(currentPage() == m_rasterInfoPage.get())
92  {
93  return execute();
94  }
95 
96  return true;
97 }
98 
99 void te::qt::widgets::ComposeBandsWizard::setList(std::list<te::map::AbstractLayerPtr>& layerList)
100 {
101  m_layerSearchPage->getSearchWidget()->setList(layerList);
102  m_layerSearchPage->getSearchWidget()->filterOnlyByRaster();
103 }
104 
105 std::list<te::map::AbstractLayerPtr> te::qt::widgets::ComposeBandsWizard::getOutputLayers()
106 {
107  return m_outputLayerList;
108 }
109 
111 {
115 
116  addPage(m_layerSearchPage.get());
117  addPage(m_composeBandsPage.get());
118  addPage(m_rasterInfoPage.get());
119 
120  //for contrast only one layer can be selected
121  m_layerSearchPage->setSubTitle(tr("Allows selection of layers using filters for selection. Select one layer for DECOMPOSITION or multi layers for COMPOSITION."));
122  m_layerSearchPage->getSearchWidget()->enableMultiSelection(true);
123 }
124 
126 {
127  if(m_rasterInfoPage->getWidget()->fileExists())
128  {
129  QMessageBox::warning(this, tr("Compose / Decompose Bands"), tr("File already exists."));
130  return false;
131  }
132 
133  if(m_composeBandsPage->isCompose())
134  return executeCompose();
135 
136  if(m_composeBandsPage->isDecompose())
137  return executeDecompose();
138 
139  return false;
140 }
141 
143 {
144  //get output parameters
145  std::map<std::string, std::string> rinfo = m_rasterInfoPage->getWidget()->getInfo();
146  std::string type = m_rasterInfoPage->getWidget()->getType();
147 
148  //get input parameters
149  std::vector<const te::rst::Raster*> inputRasters;
150  std::vector<unsigned int> inputRasterBands;
151  te::rst::Interpolator::Method interpMethod;
152 
153  m_composeBandsPage->getComposeParameters(inputRasters, inputRasterBands, interpMethod);
154 
155  std::vector<const te::rst::Raster*> inputRastersComposed;
156 
157  if(m_composeBandsPage->isNormalize())
158  for(std::size_t i = 0; i < inputRasters.size(); ++i)
159  inputRastersComposed.push_back(te::rp::NormalizeRaster(inputRasters.at(i)));
160  else
161  inputRastersComposed = inputRasters;
162 
163  //set feeder
164  te::rp::FeederConstRasterVector feeder(inputRastersComposed);
165 
166  //execute
167  std::unique_ptr<te::rst::Raster> outputRaster;
168 
169  QApplication::setOverrideCursor(Qt::WaitCursor);
170 
171  bool res = false;
172 
173  try
174  {
175  res = te::rp::ComposeBands(feeder, inputRasterBands, interpMethod, rinfo, type, outputRaster);
176  }
177  catch(const std::exception& e)
178  {
179  QMessageBox::warning(this, tr("Compose"), e.what());
180 
181  QApplication::restoreOverrideCursor();
182 
183  te::common::FreeContents(inputRasters);
184 
185  if(m_composeBandsPage->isNormalize())
186  te::common::FreeContents(inputRastersComposed);
187 
188  return false;
189  }
190  catch(...)
191  {
192  QMessageBox::warning(this, tr("Compose"), tr("An exception has occurred!"));
193 
194  QApplication::restoreOverrideCursor();
195 
196  te::common::FreeContents(inputRasters);
197 
198  if(m_composeBandsPage->isNormalize())
199  te::common::FreeContents(inputRastersComposed);
200 
201  return false;
202  }
203 
204  QApplication::restoreOverrideCursor();
205 
206  //create layer
207  if(res)
208  {
209  outputRaster.reset();
210 
212 
213  m_outputLayerList.push_back(layer);
214 
216  }
217 
218  //clear vector
219 
220  te::common::FreeContents(inputRasters);
221 
222  if(m_composeBandsPage->isNormalize())
223  te::common::FreeContents(inputRastersComposed);
224 
225  return res;
226 }
227 
229 {
230  //get output parameters
231  std::string outputDataSetNamePrefix = m_rasterInfoPage->getWidget()->getShortName();
232  std::string outputDataSetNameExt = m_rasterInfoPage->getWidget()->getExtension();
233  std::string type = m_rasterInfoPage->getWidget()->getType();
234  std::vector< std::map<std::string, std::string> > outputRastersInfos;
235 
236  //get input parameters
237  te::rst::Raster* inputRaster = nullptr;
238  std::vector<unsigned int> inputRasterBands;
239 
240  m_composeBandsPage->getDecomposeParameters(inputRaster, inputRasterBands);
241 
242  te::rst::Raster* inputRasterDecomposed = nullptr;
243 
244  if(m_composeBandsPage->isNormalize())
245  inputRasterDecomposed = te::rp::NormalizeRaster(inputRaster);
246  else
247  inputRasterDecomposed = inputRaster;
248 
249  //set output names
250  std::vector<std::string> outputDataSetNames;
251 
252  for(std::size_t t = 0; t < inputRasterBands.size(); ++t)
253  {
254  std::string name = outputDataSetNamePrefix;
255  name += "_";
256  name += te::common::Convert2String(inputRasterBands[t]);
257  name += outputDataSetNameExt;
258 
259  outputDataSetNames.push_back(name);
260 
261  outputRastersInfos.push_back(m_rasterInfoPage->getWidget()->getInfo((int)t));
262  }
263 
264  std::vector< boost::shared_ptr< te::rst::Raster > > outputRastersPtrs;
265 
266  //execute
267  bool res = false;
268 
269  try
270  {
271  res = te::rp::DecomposeBands(*inputRasterDecomposed, inputRasterBands, outputRastersInfos, type, outputRastersPtrs);
272  }
273  catch(const std::exception& e)
274  {
275  QMessageBox::warning(this, tr("Decompose"), e.what());
276 
277  QApplication::restoreOverrideCursor();
278 
279  outputRastersPtrs.clear();
280 
281  delete inputRaster;
282 
283  if(m_composeBandsPage->isNormalize())
284  delete inputRasterDecomposed;
285 
286  return false;
287  }
288  catch(...)
289  {
290  QMessageBox::warning(this, tr("Decompose"), tr("An exception has occurred!"));
291 
292  QApplication::restoreOverrideCursor();
293 
294  outputRastersPtrs.clear();
295 
296  delete inputRaster;
297 
298  if(m_composeBandsPage->isNormalize())
299  delete inputRasterDecomposed;
300 
301  return false;
302  }
303 
304  QApplication::restoreOverrideCursor();
305 
306  //create layer
307  if(res)
308  {
309  outputRastersPtrs.clear();
310 
311  std::string type = m_rasterInfoPage->getWidget()->getType();
312 
313  for (std::size_t t = 0; t < outputRastersInfos.size(); ++t)
314  {
315  std::map<std::string, std::string> rinfo = outputRastersInfos[t];
317  m_outputLayerList.push_back(layer);
318 
320  }
321  }
322 
323  delete inputRaster;
324 
325  if(m_composeBandsPage->isNormalize())
326  delete inputRasterDecomposed;
327 
328  return res;
329 }
330 
332 {
333  this->adjustSize();
334 }
std::unique_ptr< te::qt::widgets::LayerSearchWizardPage > m_layerSearchPage
A feeder from a input rasters vector;.
Definition: FeedersRaster.h:69
void setPageReference(const QString &ref)
Sets the documentation page reference.
std::list< te::map::AbstractLayerPtr > m_outputLayerList
bool ComposeBands(te::rp::FeederConstRaster &feeder, const std::vector< unsigned int > &inputRasterBands, const te::rst::Interpolator::Method &interpMethod, const std::map< std::string, std::string > &outputRasterInfo, const std::string &outputDataSourceType, std::unique_ptr< te::rst::Raster > &outputRasterPtr)
Compose a set of bands into one multi-band raster.
bool DecomposeBands(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< std::map< std::string, std::string > > &outputRastersInfos, const std::string &outputDataSourceType, std::vector< boost::shared_ptr< te::rst::Raster > > &outputRastersPtrs)
Decompose a multi-band raster into a set of one-band rasters.
InterpolationMethod
Allowed interpolation methods.
std::unique_ptr< te::qt::widgets::ComposeBandsWizardPage > m_composeBandsPage
This file defines a class for a Raster Info Wizard page.
A Qt dialog that allows users to compose or decompose bands.
This class is GUI used to defines a class for a Compose / Decompose Bands Wizard page.
bool NormalizeRaster(te::rst::Raster &inraster, double nmin, double nmax)
Normalizes one raster in a given interval.
An abstract class for raster data strucutures.
This class is GUI used to define the raster info parameters for raster factory.
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
Utility functions for the data access module.
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
std::unique_ptr< te::qt::widgets::RasterInfoWizardPage > m_rasterInfoPage
This file defines a class for a Compose / Decompose Bands Wizard page.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
std::list< te::map::AbstractLayerPtr > getOutputLayers()
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)