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) 2001-2009 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  boost::shared_ptr<te::rst::Raster> rstAPtr(rstA.release());
247  params.m_rasterVec.push_back(rstAPtr);
248 
249  return params;
250 }
251 
253 {
255 
256  double gain = m_arithmeticOpPage->getGainValue();
257  double offSet = m_arithmeticOpPage->getOffSetValue();
258  bool normalize = m_arithmeticOpPage->normalize();
259 
260  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
261  int bA = m_arithmeticOpPage->getLayerBandA();
262 
263  //get raster
264  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
265  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
266  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
267 
268  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
269  int bB = m_arithmeticOpPage->getLayerBandB();
270 
271  //get raster
272  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
273  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
274  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
275 
276 //Gain * ( A + B ) + Offset
277  std::string arithmeticString = QString::number(gain).toLatin1().data();
278  arithmeticString += " * ( R0:";
279  arithmeticString += QString::number(bA).toLatin1().data();
280  arithmeticString += " + R1:";
281  arithmeticString += QString::number(bB).toLatin1().data();
282  arithmeticString += " ) + ";
283  arithmeticString += QString::number(offSet).toLatin1().data();
284 
285  params.m_arithmeticString = arithmeticString;
286  params.m_normalize = normalize;
287 
288  boost::shared_ptr<te::rst::Raster> rstAPtr(rstA.release());
289  params.m_rasterVec.push_back(rstAPtr);
290 
291  boost::shared_ptr<te::rst::Raster> rstBPtr(rstB.release());
292  params.m_rasterVec.push_back(rstBPtr);
293 
294  return params;
295 }
296 
298 {
300 
301  double gain = m_arithmeticOpPage->getGainValue();
302  double offSet = m_arithmeticOpPage->getOffSetValue();
303  bool normalize = m_arithmeticOpPage->normalize();
304 
305  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
306  int bA = m_arithmeticOpPage->getLayerBandA();
307 
308  //get raster
309  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
310  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
311  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
312 
313  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
314  int bB = m_arithmeticOpPage->getLayerBandB();
315 
316  //get raster
317  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
318  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
319  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
320 
321 //Gain * ( A - B ) + Offset
322  std::string arithmeticString = QString::number(gain).toLatin1().data();
323  arithmeticString += " * ( R0:";
324  arithmeticString += QString::number(bA).toLatin1().data();
325  arithmeticString += " - R1:";
326  arithmeticString += QString::number(bB).toLatin1().data();
327  arithmeticString += " ) + ";
328  arithmeticString += QString::number(offSet).toLatin1().data();
329 
330  params.m_arithmeticString = arithmeticString;
331  params.m_normalize = normalize;
332 
333  boost::shared_ptr<te::rst::Raster> rstAPtr(rstA.release());
334  params.m_rasterVec.push_back(rstAPtr);
335 
336  boost::shared_ptr<te::rst::Raster> rstBPtr(rstB.release());
337  params.m_rasterVec.push_back(rstBPtr);
338 
339  return params;
340 }
341 
343 {
345 
346  double gain = m_arithmeticOpPage->getGainValue();
347  double offSet = m_arithmeticOpPage->getOffSetValue();
348  bool normalize = m_arithmeticOpPage->normalize();
349 
350  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
351  int bA = m_arithmeticOpPage->getLayerBandA();
352 
353  //get raster
354  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
355  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
356  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
357 
358  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
359  int bB = m_arithmeticOpPage->getLayerBandB();
360 
361  //get raster
362  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
363  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
364  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
365 
366 //Gain * ( A * B ) + Offset
367  std::string arithmeticString = QString::number(gain).toLatin1().data();
368  arithmeticString += " * ( R0:";
369  arithmeticString += QString::number(bA).toLatin1().data();
370  arithmeticString += " * R1:";
371  arithmeticString += QString::number(bB).toLatin1().data();
372  arithmeticString += " ) + ";
373  arithmeticString += QString::number(offSet).toLatin1().data();
374 
375  params.m_arithmeticString = arithmeticString;
376  params.m_normalize = normalize;
377 
378  boost::shared_ptr<te::rst::Raster> rstAPtr(rstA.release());
379  params.m_rasterVec.push_back(rstAPtr);
380 
381  boost::shared_ptr<te::rst::Raster> rstBPtr(rstB.release());
382  params.m_rasterVec.push_back(rstBPtr);
383 
384  return params;
385 }
386 
388 {
390 
391  double gain = m_arithmeticOpPage->getGainValue();
392  double offSet = m_arithmeticOpPage->getOffSetValue();
393  bool normalize = m_arithmeticOpPage->normalize();
394 
395  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
396  int bA = m_arithmeticOpPage->getLayerBandA();
397 
398  //get raster
399  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
400  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
401  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
402 
403  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
404  int bB = m_arithmeticOpPage->getLayerBandB();
405 
406  //get raster
407  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
408  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
409  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
410 
411 //Gain * ( A / B ) + Offset
412  std::string arithmeticString = QString::number(gain).toLatin1().data();
413  arithmeticString += " * ( R0:";
414  arithmeticString += QString::number(bA).toLatin1().data();
415  arithmeticString += " / R1:";
416  arithmeticString += QString::number(bB).toLatin1().data();
417  arithmeticString += " ) + ";
418  arithmeticString += QString::number(offSet).toLatin1().data();
419 
420  params.m_arithmeticString = arithmeticString;
421  params.m_normalize = normalize;
422 
423  boost::shared_ptr<te::rst::Raster> rstAPtr(rstA.release());
424  params.m_rasterVec.push_back(rstAPtr);
425 
426  boost::shared_ptr<te::rst::Raster> rstBPtr(rstB.release());
427  params.m_rasterVec.push_back(rstBPtr);
428 
429  return params;
430 }
431 
433 {
435 
436  double gain = m_arithmeticOpPage->getGainValue();
437  double offSet = m_arithmeticOpPage->getOffSetValue();
438  bool normalize = m_arithmeticOpPage->normalize();
439 
440  te::map::AbstractLayerPtr lA = m_arithmeticOpPage->getLayerRasterA();
441  int bA = m_arithmeticOpPage->getLayerBandA();
442 
443  //get raster
444  std::auto_ptr<te::da::DataSet> dsA = lA->getData();
445  std::size_t rposA = te::da::GetFirstPropertyPos(dsA.get(), te::dt::RASTER_TYPE);
446  std::auto_ptr<te::rst::Raster> rstA = dsA->getRaster(rposA);
447 
448  te::map::AbstractLayerPtr lB = m_arithmeticOpPage->getLayerRasterB();
449  int bB = m_arithmeticOpPage->getLayerBandB();
450 
451  //get raster
452  std::auto_ptr<te::da::DataSet> dsB = lB->getData();
453  std::size_t rposB = te::da::GetFirstPropertyPos(dsB.get(), te::dt::RASTER_TYPE);
454  std::auto_ptr<te::rst::Raster> rstB = dsB->getRaster(rposB);
455 
456 //( Gain * ( A - B ) / ( A + B ) ) + Offset
457  std::string arithmeticString = "( ";
458  arithmeticString += QString::number(gain).toLatin1().data();
459  arithmeticString += " * ( R0:";
460  arithmeticString += QString::number(bA).toLatin1().data();
461  arithmeticString += " - R1:";
462  arithmeticString += QString::number(bB).toLatin1().data();
463  arithmeticString += " ) / ";
464  arithmeticString += "( R0:";
465  arithmeticString += QString::number(bA).toLatin1().data();
466  arithmeticString += " + R1:";
467  arithmeticString += QString::number(bB).toLatin1().data();
468  arithmeticString += " ) ) + ";
469  arithmeticString += QString::number(offSet).toLatin1().data();
470 
471  params.m_arithmeticString = arithmeticString;
472  params.m_normalize = normalize;
473 
474  boost::shared_ptr<te::rst::Raster> rstAPtr(rstA.release());
475  params.m_rasterVec.push_back(rstAPtr);
476 
477  boost::shared_ptr<te::rst::Raster> rstBPtr(rstB.release());
478  params.m_rasterVec.push_back(rstBPtr);
479 
480  return params;
481 }
482 
484 {
486 
487  double gain = m_arithmeticOpPage->getGainValue();
488  double offSet = m_arithmeticOpPage->getOffSetValue();
489  bool normalize = m_arithmeticOpPage->normalize();
490  std::string arithExpStr = m_arithmeticOpPage->getUserDefinedExpression();
491 
492  params.m_arithmeticString = arithExpStr;
493  params.m_normalize = normalize;
494 
495  //params.m_rasterVec.push_back();
496 
497  return params;
498 }
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).
std::vector< boost::shared_ptr< te::rst::Raster > > m_rasterVec
Input rasters vector.
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()
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:432
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.