All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ArithmeticOpWizard.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/ArithmeticOpWizard.cpp
22 
23  \brief A Qt dialog that allows users to run a arithmeticOp operation defined by RP module.
24 */
25 
26 // TerraLib
27 #include "../../../common/progress/ProgressManager.h"
28 #include "../../../common/STLUtils.h"
29 #include "../../../dataaccess/dataset/DataSet.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../raster/RasterFactory.h"
32 #include "../../../rp/Functions.h"
33 #include "../../widgets/help/HelpPushButton.h"
34 #include "../../widgets/progress/ProgressViewerDialog.h"
35 #include "../layer/search/LayerSearchWidget.h"
36 #include "../layer/search/LayerSearchWizardPage.h"
37 #include "ArithmeticOpWizard.h"
38 #include "ArithmeticOpWizardPage.h"
39 #include "RasterInfoWidget.h"
40 #include "RasterInfoWizardPage.h"
41 #include "Utils.h"
42 
43 // STL
44 #include <cassert>
45 
46 // Qt
47 #include <QMessageBox>
48 #include <QApplication>
49 
50 
52  : QWizard(parent)
53 {
54  //configure the wizard
55  this->setWizardStyle(QWizard::ModernStyle);
56  this->setWindowTitle(tr("Arithmetic Operation"));
57  //this->setFixedSize(640, 480);
58 
59  this->setOption(QWizard::HaveHelpButton, true);
60  this->setOption(QWizard::HelpButtonOnRight, false);
61 
63 
64  this->setButton(QWizard::HelpButton, helpButton);
65 
66  helpButton->setPageReference("plugins/rp/rp_arithOp.html");
67 
68  addPages();
69 }
70 
72 {
73 
74 }
75 
77 {
78  if(currentPage() == m_layerSearchPage.get())
79  {
80  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
81 
82  m_arithmeticOpPage->setList(list);
83 
84  return m_layerSearchPage->isComplete();
85  }
86  else if(currentPage() == m_arithmeticOpPage.get())
87  {
88  return m_arithmeticOpPage->isComplete();
89  }
90  else if(currentPage() == m_rasterInfoPage.get())
91  {
92  return execute();
93  }
94 
95  return true;
96 }
97 
98 void te::qt::widgets::ArithmeticOpWizard::setList(std::list<te::map::AbstractLayerPtr>& layerList)
99 {
100  m_layerSearchPage->getSearchWidget()->setList(layerList);
101  m_layerSearchPage->getSearchWidget()->filterOnlyByRaster();
102  m_layerSearchPage->getSearchWidget()->enableMultiSelection(true);
103 }
104 
106 {
107  return m_outputLayer;
108 }
109 
111 {
112  m_layerSearchPage.reset(new te::qt::widgets::LayerSearchWizardPage(this));
113  m_arithmeticOpPage.reset(new te::qt::widgets::ArithmeticOpWizardPage(this));
114  m_rasterInfoPage.reset(new te::qt::widgets::RasterInfoWizardPage(this));
115 
116  addPage(m_layerSearchPage.get());
117  addPage(m_arithmeticOpPage.get());
118  addPage(m_rasterInfoPage.get());
119 }
120 
122 {
123  //progress
126 
127  QApplication::setOverrideCursor(Qt::WaitCursor);
128 
129  int type = m_arithmeticOpPage->getOperationType();
130 
132 
133  if(type == ARITH_OP_TYPE_1)
134  algoInputParams = paramsFromOp1();
135  else if(type == ARITH_OP_TYPE_2)
136  algoInputParams = paramsFromOp2();
137  else if(type == ARITH_OP_TYPE_3)
138  algoInputParams = paramsFromOp3();
139  else if(type == ARITH_OP_TYPE_4)
140  algoInputParams = paramsFromOp4();
141  else if(type == ARITH_OP_TYPE_5)
142  algoInputParams = paramsFromOp5();
143  else if(type == ARITH_OP_TYPE_6)
144  algoInputParams = paramsFromOp6();
145  else if(type == ARITH_OP_TYPE_USER_DEFINED)
146  algoInputParams = paramsFromOpUserdDef();
147 
149 
150  algoOutputParams.m_rType = m_rasterInfoPage->getWidget()->getType();
151  algoOutputParams.m_rInfo = m_rasterInfoPage->getWidget()->getInfo();
152 
153  te::rp::ArithmeticOperations algorithmInstance;
154 
155  try
156  {
157  if(algorithmInstance.initialize(algoInputParams))
158  {
159  if(algorithmInstance.execute(algoOutputParams))
160  {
161  algoOutputParams.reset();
162 
163  //set output layer
164  m_outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
165  m_rasterInfoPage->getWidget()->getInfo());
166 
167  QMessageBox::information(this, tr("Arithmetic Operation"), tr("Arithmetic Operation ended sucessfully"));
168  }
169  else
170  {
171  QMessageBox::critical(this, tr("Arithmetic Operation"), tr("Arithmetic Operation execution error.") + ( " " + te::rp::Module::getLastLogStr() ).c_str());
172 
174 
175  QApplication::restoreOverrideCursor();
176 
177  return false;
178  }
179  }
180  else
181  {
182  QMessageBox::critical(this, tr("Arithmetic Operation"), tr("Arithmetic Operation initialization error") +
183  ( " " + te::rp::Module::getLastLogStr() ).c_str());
184 
186 
187  QApplication::restoreOverrideCursor();
188 
189  return false;
190  }
191  }
192  catch(const std::exception& e)
193  {
194  QMessageBox::warning(this, tr("Arithmetic Operation"), e.what());
195 
197 
198  QApplication::restoreOverrideCursor();
199 
200  return false;
201  }
202  catch(...)
203  {
204  QMessageBox::warning(this, tr("Arithmetic Operation"), tr("An exception has occurred!"));
205 
207 
208  QApplication::restoreOverrideCursor();
209 
210  return false;
211  }
212 
214 
215  QApplication::restoreOverrideCursor();
216 
217  return true;
218 }
219 
221 {
223 
224  double gain = m_arithmeticOpPage->getGainValue();
225  double offSet = m_arithmeticOpPage->getOffSetValue();
226  bool normalize = m_arithmeticOpPage->normalize();
227 
228  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
229  int bA = m_arithmeticOpPage->getLayerBandA();
230 
231  //get raster
232  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
233  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
234  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
235 
236 //Gain * ( A ) + Offset
237  std::string arithmeticString = QString::number(gain).toLatin1().data();
238  arithmeticString += " * R0:";
239  arithmeticString += QString::number(bA).toLatin1().data();
240  arithmeticString += " + ";
241  arithmeticString += QString::number(offSet).toLatin1().data();
242 
243  params.m_arithmeticString = arithmeticString;
244  params.m_normalize = normalize;
245 
246  params.m_inputRasters.push_back(rstA.release());
247 
248  return params;
249 }
250 
252 {
254 
255  double gain = m_arithmeticOpPage->getGainValue();
256  double offSet = m_arithmeticOpPage->getOffSetValue();
257  bool normalize = m_arithmeticOpPage->normalize();
258 
259  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
260  int bA = m_arithmeticOpPage->getLayerBandA();
261 
262  //get raster
263  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
264  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
265  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
266 
267  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
268  int bB = m_arithmeticOpPage->getLayerBandB();
269 
270  //get raster
271  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
272  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
273  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
274 
275 //Gain * ( A + B ) + Offset
276  std::string arithmeticString = QString::number(gain).toLatin1().data();
277  arithmeticString += " * ( R0:";
278  arithmeticString += QString::number(bA).toLatin1().data();
279  arithmeticString += " + R1:";
280  arithmeticString += QString::number(bB).toLatin1().data();
281  arithmeticString += " ) + ";
282  arithmeticString += QString::number(offSet).toLatin1().data();
283 
284  params.m_arithmeticString = arithmeticString;
285  params.m_normalize = normalize;
286 
287  params.m_inputRasters.push_back(rstA.release());
288 
289  params.m_inputRasters.push_back(rstB.release());
290 
291  return params;
292 }
293 
295 {
297 
298  double gain = m_arithmeticOpPage->getGainValue();
299  double offSet = m_arithmeticOpPage->getOffSetValue();
300  bool normalize = m_arithmeticOpPage->normalize();
301 
302  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
303  int bA = m_arithmeticOpPage->getLayerBandA();
304 
305  //get raster
306  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
307  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
308  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
309 
310  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
311  int bB = m_arithmeticOpPage->getLayerBandB();
312 
313  //get raster
314  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
315  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
316  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
317 
318 //Gain * ( A - B ) + Offset
319  std::string arithmeticString = QString::number(gain).toLatin1().data();
320  arithmeticString += " * ( R0:";
321  arithmeticString += QString::number(bA).toLatin1().data();
322  arithmeticString += " - R1:";
323  arithmeticString += QString::number(bB).toLatin1().data();
324  arithmeticString += " ) + ";
325  arithmeticString += QString::number(offSet).toLatin1().data();
326 
327  params.m_arithmeticString = arithmeticString;
328  params.m_normalize = normalize;
329 
330  params.m_inputRasters.push_back(rstA.release());
331 
332  params.m_inputRasters.push_back(rstB.release());
333 
334  return params;
335 }
336 
338 {
340 
341  double gain = m_arithmeticOpPage->getGainValue();
342  double offSet = m_arithmeticOpPage->getOffSetValue();
343  bool normalize = m_arithmeticOpPage->normalize();
344 
345  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
346  int bA = m_arithmeticOpPage->getLayerBandA();
347 
348  //get raster
349  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
350  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
351  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
352 
353  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
354  int bB = m_arithmeticOpPage->getLayerBandB();
355 
356  //get raster
357  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
358  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
359  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
360 
361 //Gain * ( A * B ) + Offset
362  std::string arithmeticString = QString::number(gain).toLatin1().data();
363  arithmeticString += " * ( R0:";
364  arithmeticString += QString::number(bA).toLatin1().data();
365  arithmeticString += " * R1:";
366  arithmeticString += QString::number(bB).toLatin1().data();
367  arithmeticString += " ) + ";
368  arithmeticString += QString::number(offSet).toLatin1().data();
369 
370  params.m_arithmeticString = arithmeticString;
371  params.m_normalize = normalize;
372 
373  params.m_inputRasters.push_back(rstA.release());
374 
375  params.m_inputRasters.push_back(rstB.release());
376 
377  return params;
378 }
379 
381 {
383 
384  double gain = m_arithmeticOpPage->getGainValue();
385  double offSet = m_arithmeticOpPage->getOffSetValue();
386  bool normalize = m_arithmeticOpPage->normalize();
387 
388  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
389  int bA = m_arithmeticOpPage->getLayerBandA();
390 
391  //get raster
392  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
393  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
394  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
395 
396  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
397  int bB = m_arithmeticOpPage->getLayerBandB();
398 
399  //get raster
400  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
401  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
402  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
403 
404 //Gain * ( A / B ) + Offset
405  std::string arithmeticString = QString::number(gain).toLatin1().data();
406  arithmeticString += " * ( R0:";
407  arithmeticString += QString::number(bA).toLatin1().data();
408  arithmeticString += " / R1:";
409  arithmeticString += QString::number(bB).toLatin1().data();
410  arithmeticString += " ) + ";
411  arithmeticString += QString::number(offSet).toLatin1().data();
412 
413  params.m_arithmeticString = arithmeticString;
414  params.m_normalize = normalize;
415 
416  params.m_inputRasters.push_back(rstA.release());
417 
418  params.m_inputRasters.push_back(rstB.release());
419 
420  return params;
421 }
422 
424 {
426 
427  double gain = m_arithmeticOpPage->getGainValue();
428  double offSet = m_arithmeticOpPage->getOffSetValue();
429  bool normalize = m_arithmeticOpPage->normalize();
430 
431  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
432  int bA = m_arithmeticOpPage->getLayerBandA();
433 
434  //get raster
435  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
436  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
437  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
438 
439  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
440  int bB = m_arithmeticOpPage->getLayerBandB();
441 
442  //get raster
443  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
444  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
445  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
446 
447 //( Gain * ( A - B ) / ( A + B ) ) + Offset
448  std::string arithmeticString = "( ";
449  arithmeticString += QString::number(gain).toLatin1().data();
450  arithmeticString += " * ( R0:";
451  arithmeticString += QString::number(bA).toLatin1().data();
452  arithmeticString += " - R1:";
453  arithmeticString += QString::number(bB).toLatin1().data();
454  arithmeticString += " ) / ";
455  arithmeticString += "( R0:";
456  arithmeticString += QString::number(bA).toLatin1().data();
457  arithmeticString += " + R1:";
458  arithmeticString += QString::number(bB).toLatin1().data();
459  arithmeticString += " ) ) + ";
460  arithmeticString += QString::number(offSet).toLatin1().data();
461 
462  params.m_arithmeticString = arithmeticString;
463  params.m_normalize = normalize;
464 
465  params.m_inputRasters.push_back(rstA.release());
466 
467  params.m_inputRasters.push_back(rstB.release());
468 
469  return params;
470 }
471 
473 {
475 
476  bool normalize = m_arithmeticOpPage->normalize();
477  std::string arithExpStr = m_arithmeticOpPage->getUserDefinedExpression();
478 
479  params.m_arithmeticString = arithExpStr;
480  params.m_normalize = normalize;
481 
482  //params.m_rasterVec.push_back();
483 
484  return params;
485 }
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Utility functions for the data access module.
This class is GUI used to define the arithmeticOp parameters for the RP arithmeticOp operation...
This file defines a class for a ArithmeticOp Wizard page.
te::rp::ArithmeticOperations::InputParameters paramsFromOp5()
void setPageReference(const QString &ref)
Sets the documentation page reference.
te::map::AbstractLayerPtr getOutputLayer()
te::rp::ArithmeticOperations::InputParameters paramsFromOp6()
bool m_normalize
Output values normalization will be performed to fit the allowed values range (default:false).
te::rp::ArithmeticOperations::InputParameters paramsFromOp3()
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
This file defines a class for a Raster Info Wizard page.
te::rp::ArithmeticOperations::InputParameters paramsFromOp4()
A Qt dialog that allows users to run a arithmeticOp operation defined by RP module.
static const std::string & getLastLogStr()
Returns the last log string generated by this module.
Definition: Module.h:53
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
This class is GUI used to define the raster info parameters for raster factory.
ArithmeticOperations output parameters.
te::rp::ArithmeticOperations::InputParameters paramsFromOp1()
This file has the RasterInfoWidget class.
te::rp::ArithmeticOperations::InputParameters paramsFromOp2()
std::vector< te::rst::Raster * > m_inputRasters
Input rasters vector.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
te::rp::ArithmeticOperations::InputParameters paramsFromOpUserdDef()
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
Performs arithmetic operation over raster data.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const std::map< std::string, std::string > &connInfo)
Definition: Utils.cpp:40
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
ArithmeticOperations input parameters.
std::string m_arithmeticString
Arithmetic string.