ArithmeticOpDialogForm.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/ArithmeticOpDialogForm.cpp
22 
23  \brief This file defines a class for a ArithmeticOp Dialog.
24 */
25 
26 // TerraLib
27 #include "../../../common/progress/ProgressManager.h"
28 #include "../../../common/StringUtils.h"
29 #include "../../../core/translator/Translator.h"
30 #include "../../../core/utils/Platform.h"
31 #include "../../../dataaccess/dataset/DataSet.h"
32 #include "../../../dataaccess/datasource/DataSourceInfo.h"
33 #include "../../../dataaccess/utils/Utils.h"
34 #include "../../../raster/Raster.h"
35 #include "../../../raster/RasterProperty.h"
36 #include "../../../rp/Module.h"
37 #include "../../../maptools/Utils.h"
38 #include "../../widgets/progress/ProgressViewerDialog.h"
39 #include "../datasource/selector/DataSourceSelectorDialog.h"
40 #include "../layer/search/LayerSearchDialog.h"
41 #include "../layer/search/LayerSearchWidget.h"
42 #include "LoadArithmeticOpDialog.h"
43 #include "OutputRasterWidget.h"
44 #include "ArithmeticOpDialogForm.h"
45 #include "ui_ArithmeticOpDialogForm.h"
46 #include "Utils.h"
47 
48 //Boost
49 #include <boost/filesystem.hpp>
50 #include <boost/algorithm/string.hpp>
51 #include <boost/property_tree/json_parser.hpp>
52 
53 // Qt
54 #include <QApplication>
55 #include <QGridLayout>
56 #include <QMessageBox>
57 #include <QFileDialog>
58 #include <QLineEdit>
59 #include <QTextEdit>
60 #include <QPushButton>
61 #include <QTextStream>
62 
63 // stl
64 #include <memory>
65 
67 
68 te::qt::widgets::ArithmeticOpDialogForm::ArithmeticOpDialogForm(QWidget* parent)
69  : QDialog(parent),
70  m_ui(new Ui::ArithmeticOpDialogForm)
71 {
72 // setup controls
73  m_ui->setupUi(this);
74 
75  //build output parameters
76  QGridLayout* outputLayout = new QGridLayout(m_ui->m_outputWidget);
77  outputLayout->setContentsMargins(0, 0, 0, 0);
78  m_outputWidget = new te::qt::widgets::OutputRasterWidget(m_ui->m_outputWidget);
79  outputLayout->addWidget(m_outputWidget);
80 
81  //set icons
82  m_ui->m_addLayerPushButton->setIcon(QIcon::fromTheme("list-add"));
83  m_ui->m_loadPushButton->setIcon(QIcon::fromTheme("document-open"));
84  m_ui->m_savePushButton->setIcon(QIcon::fromTheme("document-save"));
85 
86  m_expressionHistoryCounter = -1;
87  m_nLayer = 0;
88  m_operation = "";
89 
90  connect(m_ui->m_searchLayerToolButton, SIGNAL(pressed()), this, SLOT(onSearchLayerToolButtonPressed()));
91  connect(m_ui->m_addLayerPushButton, SIGNAL(clicked()), this, SLOT(onAddLayerPushButtonClicked()));
92 
93  connect(m_ui->m_sqrtPushButton, SIGNAL(clicked()), this, SLOT(onSqrtPushButtonClicked()));
94  connect(m_ui->m_parRightPushButton, SIGNAL(clicked()), this, SLOT(onParRightPushButtonClicked()));
95  connect(m_ui->m_parLeftPushButton, SIGNAL(clicked()), this, SLOT(onParLeftPushButtonClicked()));
96  connect(m_ui->m_sinPushButton, SIGNAL(clicked()), this, SLOT(onSinPushButtonClicked()));
97  connect(m_ui->m_asinPushButton, SIGNAL(clicked()), this, SLOT(onASinPushButtonClicked()));
98  connect(m_ui->m_expPushButton, SIGNAL(clicked()), this, SLOT(onExpPushButtonClicked()));
99  connect(m_ui->m_cosPushButton, SIGNAL(clicked()), this, SLOT(onCosPushButtonClicked()));
100  connect(m_ui->m_acosPushButton, SIGNAL(clicked()), this, SLOT(onACosPushButtonClicked()));
101  connect(m_ui->m_logPushButton, SIGNAL(clicked()), this, SLOT(onLogPushButtonClicked()));
102  connect(m_ui->m_tanPushButton, SIGNAL(clicked()), this, SLOT(onTanPushButtonClicked()));
103  connect(m_ui->m_atanPushButton, SIGNAL(clicked()), this, SLOT(onATanPushButtonClicked()));
104  connect(m_ui->m_lnPushButton, SIGNAL(clicked()), this, SLOT(onLnPushButtonClicked()));
105  connect(m_ui->m_n0PushButton, SIGNAL(clicked()), this, SLOT(onN0PushButtonClicked()));
106  connect(m_ui->m_n1PushButton, SIGNAL(clicked()), this, SLOT(onN1PushButtonClicked()));
107  connect(m_ui->m_n2PushButton, SIGNAL(clicked()), this, SLOT(onN2PushButtonClicked()));
108  connect(m_ui->m_n3PushButton, SIGNAL(clicked()), this, SLOT(onN3PushButtonClicked()));
109  connect(m_ui->m_n4PushButton, SIGNAL(clicked()), this, SLOT(onN4PushButtonClicked()));
110  connect(m_ui->m_n5PushButton, SIGNAL(clicked()), this, SLOT(onN5PushButtonClicked()));
111  connect(m_ui->m_n6PushButton, SIGNAL(clicked()), this, SLOT(onN6PushButtonClicked()));
112  connect(m_ui->m_n7PushButton, SIGNAL(clicked()), this, SLOT(onN7PushButtonClicked()));
113  connect(m_ui->m_n8PushButton, SIGNAL(clicked()), this, SLOT(onN8PushButtonClicked()));
114  connect(m_ui->m_n9PushButton, SIGNAL(clicked()), this, SLOT(onN9PushButtonClicked()));
115  connect(m_ui->m_dotPushButton, SIGNAL(clicked()), this, SLOT(onDotPushButtonClicked()));
116  connect(m_ui->m_signalPushButton, SIGNAL(clicked()), this, SLOT(onSignalPushButtonClicked()));
117  connect(m_ui->m_divPushButton, SIGNAL(clicked()), this, SLOT(onDivPushButtonClicked()));
118  connect(m_ui->m_multPushButton, SIGNAL(clicked()), this, SLOT(onMultPushButtonClicked()));
119  connect(m_ui->m_subPushButton, SIGNAL(clicked()), this, SLOT(onSubPushButtonClicked()));
120  connect(m_ui->m_addPushButton, SIGNAL(clicked()), this, SLOT(onAddPushButtonClicked()));
121  connect(m_ui->m_cePushButton, SIGNAL(clicked()), this, SLOT(onCePushButtonClicked()));
122  connect(m_ui->m_cPushButton, SIGNAL(clicked()), this, SLOT(onCPushButtonClicked()));
123  connect(m_ui->m_loadPushButton, SIGNAL(clicked()), this, SLOT(onLoadPushButtonClicked()));
124  connect(m_ui->m_savePushButton, SIGNAL(clicked()), this, SLOT(onSavePushButtonClicked()));
125 
126  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
127 
128  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
129  m_ui->m_helpPushButton->setPageReference("plugins/rp/rp_arithOp.html");
130 }
131 
133 {
134  m_expressionHistory.clear();
135 }
136 
138 {
139  return true;
140 }
141 
142 void te::qt::widgets::ArithmeticOpDialogForm::setList(std::list<te::map::AbstractLayerPtr>& layerList)
143 {
144  m_layerList = layerList;
145 }
146 
148 {
149  m_userPath = path;
150 }
151 
153 {
154  return m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
155 }
156 
158 {
159  return m_operation;
160 }
161 
163 {
164  return m_outputLayer;
165 }
166 
167 std::map<std::string, te::map::AbstractLayer*> te::qt::widgets::ArithmeticOpDialogForm::getMapLayer()
168 {
169  return m_mapLayer;
170 }
171 
173 {
174  return m_bandsVec;
175 }
176 
178 {
179  if (m_ui->m_inputLayerLineEdit->text().isEmpty())
180  return;
181 
182  QString operation = m_ui->m_operationTextEdit->toPlainText() + m_ui->m_inputLayerLineEdit->text() + ":" + m_ui->m_bandComboBox->currentText();
183 
184  m_ui->m_operationTextEdit->setText(operation);
185 
186  std::string nLayer = "R" + te::common::Convert2String(m_nLayer);
187  m_mapLayer.insert(std::map<std::string, te::map::AbstractLayer*>::value_type(nLayer, m_layer));
188  std::string band = m_ui->m_bandComboBox->currentText().toUtf8().data();
189  m_bandsVec.push_back(band);
190 
191  m_nLayer++;
192 }
193 
195 {
197  dlg->getWidget()->setList(m_layerList);
198  dlg->getWidget()->filterOnlyByRaster();
199 
200  if (dlg->exec())
201  {
202  m_ui->m_bandComboBox->clear();
203 
204  std::list<te::map::AbstractLayerPtr> layerList = dlg->getWidget()->getSelecteds();
205 
206  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
207 
208  while (it != layerList.end())
209  {
210  std::unique_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
211 
212  if(dsType->hasRaster())
213  {
214  m_layer = it->get();
215 
216  std::string layerTitle = it->get()->getTitle();
217  m_ui->m_inputLayerLineEdit->setText(layerTitle.c_str());
218  std::unique_ptr<te::rst::Raster> raster(te::map::GetRaster(it->get()));
219 
220  for (std::size_t b = 0; b < raster->getNumberOfBands(); b++)
221  m_ui->m_bandComboBox->addItem(QString::number(b));
222  }
223 
224  it++;
225  }
226  }
227 }
228 
230 {
231  QString txt = m_ui->m_operationTextEdit->toPlainText();
232 
233  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
234  txt.replace(txt.size() - 1, txt.size(), "");
235 
236  m_ui->m_operationTextEdit->setText(txt + " sqrt( ");
237 }
238 
240 {
241  QString txt = m_ui->m_operationTextEdit->toPlainText();
242 
243  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
244  txt.replace(txt.size() - 1, txt.size(), "");
245 
246  m_ui->m_operationTextEdit->setText(txt + " ( ");
247 }
248 
250 {
251  QString txt = m_ui->m_operationTextEdit->toPlainText();
252 
253  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
254  txt.replace(txt.size() - 1, txt.size(), "");
255 
256  m_ui->m_operationTextEdit->setText(txt + " ) ");
257 }
258 
260 {
261  QString txt = m_ui->m_operationTextEdit->toPlainText();
262 
263  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
264  txt.replace(txt.size() - 1, txt.size(), "");
265 
266  m_ui->m_operationTextEdit->setText(txt + " sin( ");
267 }
268 
270 {
271  QString txt = m_ui->m_operationTextEdit->toPlainText();
272 
273  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
274  txt.replace(txt.size() - 1, txt.size(), "");
275 
276  m_ui->m_operationTextEdit->setText(txt + " asin( ");
277 }
278 
280 {
281  QString txt = m_ui->m_operationTextEdit->toPlainText();
282 
283  if (txt.isEmpty())
284  return;
285 
286  if (txt.at(txt.size() - 1) == ' ')
287  txt.replace(txt.size() - 1, txt.size(), "");
288 
289  m_ui->m_operationTextEdit->setText(txt + " ^ ");
290 }
291 
293 {
294  QString txt = m_ui->m_operationTextEdit->toPlainText();
295 
296  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
297  txt.replace(txt.size() - 1, txt.size(), "");
298 
299  m_ui->m_operationTextEdit->setText(txt + " cos( ");
300 }
301 
303 {
304  QString txt = m_ui->m_operationTextEdit->toPlainText();
305 
306  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
307  txt.replace(txt.size() - 1, txt.size(), "");
308 
309  m_ui->m_operationTextEdit->setText(txt + " acos( ");
310 }
311 
313 {
314  QString txt = m_ui->m_operationTextEdit->toPlainText();
315 
316  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
317  txt.replace(txt.size() - 1, txt.size(), "");
318 
319  m_ui->m_operationTextEdit->setText(txt + " log( ");
320 }
321 
323 {
324  QString txt = m_ui->m_operationTextEdit->toPlainText();
325 
326  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
327  txt.replace(txt.size() - 1, txt.size(), "");
328 
329  m_ui->m_operationTextEdit->setText(txt + " tan( ");
330 }
331 
333 {
334  QString txt = m_ui->m_operationTextEdit->toPlainText();
335 
336  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
337  txt.replace(txt.size() - 1, txt.size(), "");
338 
339  m_ui->m_operationTextEdit->setText(txt + " atan( ");
340 }
341 
343 {
344  QString txt = m_ui->m_operationTextEdit->toPlainText();
345 
346  if (!txt.isEmpty() && txt.at(txt.size() - 1) == ' ')
347  txt.replace(txt.size() - 1, txt.size(), "");
348 
349  m_ui->m_operationTextEdit->setText(txt + " ln( ");
350 }
351 
353 {
354  QString txt = m_ui->m_operationTextEdit->toPlainText();
355  m_ui->m_operationTextEdit->setText(txt + "0");
356 }
357 
359 {
360  QString txt = m_ui->m_operationTextEdit->toPlainText();
361  m_ui->m_operationTextEdit->setText(txt + "1");
362 }
363 
365 {
366  QString txt = m_ui->m_operationTextEdit->toPlainText();
367  m_ui->m_operationTextEdit->setText(txt + "2");
368 }
369 
371 {
372  QString txt = m_ui->m_operationTextEdit->toPlainText();
373  m_ui->m_operationTextEdit->setText(txt + "3");
374 }
375 
377 {
378  QString txt = m_ui->m_operationTextEdit->toPlainText();
379  m_ui->m_operationTextEdit->setText(txt + "4");
380 }
381 
383 {
384  QString txt = m_ui->m_operationTextEdit->toPlainText();
385  m_ui->m_operationTextEdit->setText(txt + "5");
386 }
387 
389 {
390  QString txt = m_ui->m_operationTextEdit->toPlainText();
391  m_ui->m_operationTextEdit->setText(txt + "6");
392 }
393 
395 {
396  QString txt = m_ui->m_operationTextEdit->toPlainText();
397  m_ui->m_operationTextEdit->setText(txt + "7");
398 }
399 
401 {
402  QString txt = m_ui->m_operationTextEdit->toPlainText();
403  m_ui->m_operationTextEdit->setText(txt + "8");
404 }
405 
407 {
408  QString txt = m_ui->m_operationTextEdit->toPlainText();
409  m_ui->m_operationTextEdit->setText(txt + "9");
410 }
411 
413 {
414  QString txt = m_ui->m_operationTextEdit->toPlainText();
415  m_ui->m_operationTextEdit->setText(txt + ".");
416 }
417 
419 {
420  if (m_ui->m_operationTextEdit->toPlainText().isEmpty())
421  return;
422 
423  std::string operation = m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
424 
425  std::string txt;
426  int i;
427  int decimal = 0;
428  for (i = static_cast<int>(operation.size() - 1); i >= 0; i--)
429  {
430  if (operation.at(i) == ' ' && i != static_cast<int>(operation.size()) - 1)
431  {
432  break;
433  }
434 
435  if (operation.at(i) == '.')
436  decimal = static_cast<int>(operation.size()) - 1 - i;
437 
438  txt = operation.at(i) + txt;
439  }
440 
441  QString txt1 = txt.c_str();
442  bool isOk;
443  double val = txt1.toDouble(&isOk);
444 
445  if (!isOk)
446  return;
447 
448  val *= -1;
449 
450  if (i == -1)
451  {
452  operation.clear();
453  operation = te::common::Convert2String(val, decimal);
454  }
455  else
456  operation.replace(i+1, operation.size() - 1, te::common::Convert2String(val, decimal));
457 
458  m_ui->m_operationTextEdit->clear();
459  m_ui->m_operationTextEdit->setText(operation.c_str());
460 }
461 
463 {
464  QString txt = m_ui->m_operationTextEdit->toPlainText();
465 
466  if (txt.isEmpty())
467  return;
468 
469  if (txt.at(txt.size() - 1) == ' ')
470  txt.replace(txt.size() - 1, txt.size(), "");
471 
472  m_ui->m_operationTextEdit->setText(txt + " / ");
473 }
474 
476 {
477  QString txt = m_ui->m_operationTextEdit->toPlainText();
478 
479  if (txt.isEmpty())
480  return;
481 
482  if (txt.at(txt.size() - 1) == ' ')
483  txt.replace(txt.size() - 1, txt.size(), "");
484 
485  m_ui->m_operationTextEdit->setText(txt + " * ");
486 }
487 
489 {
490  QString txt = m_ui->m_operationTextEdit->toPlainText();
491 
492  if (txt.isEmpty())
493  return;
494 
495  if (txt.at(txt.size() - 1) == ' ')
496  txt.replace(txt.size() - 1, txt.size(), "");
497 
498  m_ui->m_operationTextEdit->setText(txt + " - ");
499 }
500 
502 {
503  QString txt = m_ui->m_operationTextEdit->toPlainText();
504 
505  if (txt.isEmpty())
506  return;
507 
508  if (txt.at(txt.size() - 1) == ' ')
509  txt.replace(txt.size() - 1, txt.size(), "");
510 
511  m_ui->m_operationTextEdit->setText(txt + " + ");
512 }
513 
515 {
516  if (m_ui->m_operationTextEdit->toPlainText().isEmpty())
517  return;
518 
519  std::string operation = m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
520 
521  std::string txt;
522  for (int i = static_cast<int>(operation.size()) - 1; i >= 0; i--)
523  {
524  if (operation.at(i) == ' ' && i != static_cast<int>(operation.size()) - 1)
525  {
526  operation.replace(i + 1, operation.size() - 1, "");
527  break;
528  }
529  if (i == 0)
530  operation.replace(i, operation.size(), "");
531  }
532 
533  m_ui->m_operationTextEdit->clear();
534  m_ui->m_operationTextEdit->setText(operation.c_str());
535 }
536 
538 {
539  m_ui->m_operationTextEdit->setText("");
540 }
541 
543 {
544  try
545  {
547 
549 
550  dlg.setUserPath(m_userPath);
551 
552  dlg.loadOperations();
553 
554  dlg.setList(m_layerList);
555 
556  if (dlg.exec())
557  {
558  m_ui->m_operationTextEdit->setText(dlg.getExpression().c_str());
559  m_mapLayer = dlg.getLayers();
560  m_operation = dlg.getOperation();
561  }
562  }
563  catch(te::common::Exception& e)
564  {
565  QMessageBox::warning(this, tr("Arithmetic Operations"), tr(e.what()));
566  return;
567  }
568  catch(...)
569  {
570  QMessageBox::warning(this, tr("Arithmetic Operations"), tr("An error has occurred."));
571  return;
572  }
573 }
574 
576 {
577  if (m_ui->m_operationTextEdit->toPlainText().isEmpty())
578  return;
579 
580  std::string arithOp = m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
581  std::string arithExpStr;
582 
583  int nLayer = 0;
584  std::vector<std::string> arithExpVec;
585  boost::split(arithExpVec, arithOp, boost::is_any_of(" "));
586 
587  for (unsigned int i = 0; i < arithExpVec.size(); i++)
588  {
589  int pos = static_cast<int>(arithExpVec[i].find(":"));
590  if (pos != -1)
591  {
592  std::vector<std::string> strVec;
593  boost::split(strVec, arithExpVec[i], boost::is_any_of(":"));
594  std::string band = strVec[1];
595  if (strVec[1].at(0) == 'B')
596  band = strVec[1].erase(0, 1);
597 
598  arithExpStr += "R" + te::common::Convert2String(nLayer) + ":B ";
599  nLayer++;
600  }
601  else
602  arithExpStr += arithExpVec[i] + " ";
603  }
604 
605  QString newArithExpStr = QString::fromStdString(arithExpStr);
606 
607  arithExpStr = newArithExpStr.trimmed().toUtf8().data();
608 
609  try
610  {
612 
613  std::string jsonfile = m_userPath + "/arithmeticOperations.json";
614 
615  boost::property_tree::ptree pt;
616  boost::property_tree::ptree children;
617  boost::property_tree::ptree operations;
618 
619  boost::property_tree::json_parser::read_json(jsonfile, pt);
620 
621  for(boost::property_tree::ptree::value_type &v: pt.get_child("operations"))
622  {
623  std::string name = v.second.get<std::string>("name");
624  std::string op = v.second.get<std::string>("operation");
625 
627  {
628  QMessageBox::warning(this, tr("Arithmetic Operations"), tr("The operation already exist."));
629  return;
630  }
631 
632  boost::property_tree::ptree child;
633  child.put("name", name);
634  child.put("operation", op);
635  children.push_back(std::make_pair(v.first, child));
636  }
637 
638  boost::property_tree::ptree child;
639  child.put("name", "");
640  child.put("operation", arithExpStr);
641  children.push_back(std::make_pair("newOperation " + arithExpStr, child));
642 
643  operations.add_child("operations", children);
644 
645  boost::property_tree::write_json(jsonfile, operations);
646 
647  QMessageBox::information(this, tr("Arithmetic Operations"), tr("The operation was saved."));
648  }
649  catch(te::common::Exception& e)
650  {
651  QMessageBox::warning(this, tr("Arithmetic Operations"), tr(e.what()));
652  return;
653  }
654  catch (...)
655  {
656  QMessageBox::warning(this, tr("Arithmetic Operation"), tr("An exception has occurred!"));
657  return;
658  }
659 }
660 
662 {
663  //check output parameters
664  if (m_outputWidget->getOutputRaster().empty())
665  {
666  QMessageBox::information(this, "Arithmetic Operations", "Output image is not defined.");
667  return;
668  }
669 
670  if (!execute())
671  {
672  QMessageBox::information(this, "Arithmetic Operations", "Error to execute the operation.");
673  return;
674  }
675 }
676 
677 void te::qt::widgets::ArithmeticOpDialogForm::getRasterBands(QComboBox* layer, int index, QComboBox* band)
678 {
679  //get layer
680  QVariant varLayer = layer->itemData(index, Qt::UserRole);
682 
683  //get raster
684  std::unique_ptr<te::rst::Raster> rst(te::map::GetRaster(l.get()));
685 
686  //fill band info
687  if(rst.get())
688  {
689  band->clear();
690 
691  for(std::size_t t = 0; t < rst->getNumberOfBands(); ++t)
692  band->addItem(QString::number(t));
693  }
694 }
695 
697 {
699 }
700 
702 {
704  {
705  QMessageBox::warning(this, tr("Arithmetic Operations"), tr("File already exists."));
706  return false;
707  }
708 
709  //progress
711 
712  QApplication::setOverrideCursor(Qt::WaitCursor);
713 
715 
716  algoInputParams = paramsFromOpUserdDef();
717 
719 
720  algoOutputParams.m_rType = m_outputWidget->getType();
721 
722  std::map<std::string, std::string> rinfo;
723 
724  rinfo["URI"] = m_outputWidget->getOutputRaster();
725 
726  algoOutputParams.m_rInfo = rinfo;
727 
728  te::rp::ArithmeticOperations algorithmInstance;
729 
730  try
731  {
732  if (algorithmInstance.initialize(algoInputParams))
733  {
734  if (algorithmInstance.execute(algoOutputParams))
735  {
736  algoOutputParams.reset();
737 
738  //set output layer
740 
741  QApplication::restoreOverrideCursor();
742  QMessageBox::information(this, tr("Arithmetic Operation"), tr("Arithmetic Operation ended successfully."));
743  }
744  else
745  {
746  QApplication::restoreOverrideCursor();
747 
748  QMessageBox::critical(this, tr("Arithmetic Operation"),
749  tr("Arithmetic Operation execution error.") + (" " +
750  algorithmInstance.getErrorMessage() ).c_str());
751 
752  return false;
753  }
754  }
755  else
756  {
757  QApplication::restoreOverrideCursor();
758  QMessageBox::critical(this, tr("Arithmetic Operation"), tr("Arithmetic Operation initialization error") +
759  (" " + algorithmInstance.getErrorMessage()).c_str());
760 
761  return false;
762  }
763  }
764  catch (const std::exception& e)
765  {
766  QApplication::restoreOverrideCursor();
767  QMessageBox::warning(this, tr("Arithmetic Operation"), e.what());
768 
769  return false;
770  }
771  catch (...)
772  {
773  QApplication::restoreOverrideCursor();
774  QMessageBox::warning(this, tr("Arithmetic Operation"), tr("An exception has occurred!"));
775 
776  return false;
777  }
778 
779  QApplication::restoreOverrideCursor();
780 
782 
783  emit addLayer(m_outputLayer);
784 
785  return true;
786 }
787 
789 {
790  QString path = QString::fromStdString(m_userPath) + "/arithmeticOperations.json";
791 
792  if(QFile(path).exists())
793  return;
794 
795  QString defaultPath = QString::fromStdString(te::core::FindInTerraLibPath("share/terralib/json/arithmeticOperations.json"));
796 
797  QFile defaultFile(defaultPath);
798 
799  if(!defaultFile.exists())
800  throw te::common::Exception(TE_TR("The file with default operations was not found."));
801 
802  defaultFile.open(QIODevice::ReadOnly);
803  QString input = defaultFile.readAll();
804  defaultFile.close();
805 
806  QFile file(path);
807  file.open(QIODevice::WriteOnly);
808  QTextStream out(&file);
809  out << input.toUtf8().data();
810  file.close();
811 }
812 
814 {
815  std::string arithExpStr;
817 
818  std::map<std::string, te::map::AbstractLayer*> mapLayer = getMapLayer();
819 
820  std::map<std::string, te::map::AbstractLayer*>::iterator it = mapLayer.begin();
821  while (it != mapLayer.end())
822  {
823  //get raster
824  std::unique_ptr<te::rst::Raster> rstA(te::map::GetRaster(it->second));
825 
826  params.m_inputRasters.push_back(rstA.release());
827 
828  it++;
829  }
830 
831  arithExpStr = getExpression();
832 
833  if (arithExpStr.empty())
834  {
835  int nLayer = 0;
836  std::string expression = getUserDefinedExpression();
837 
838  std::vector<std::string> arithExpVec;
839  boost::split(arithExpVec, expression, boost::is_any_of(" "));
840 
841  for (unsigned int i = 0; i < arithExpVec.size(); i++)
842  {
843  int pos = static_cast<int>(arithExpVec[i].find(":"));
844  if (pos != -1)
845  {
846  std::vector<std::string> strVec;
847  boost::split(strVec, arithExpVec[i], boost::is_any_of(":"));
848  std::string band = strVec[1];
849  if (strVec[1].at(0) == 'B')
850  band = strVec[1].erase(0, 1);
851 
852  arithExpStr += "R" + te::common::Convert2String(nLayer) + ":" + band + " ";
853  nLayer++;
854  }
855  else
856  arithExpStr += arithExpVec[i] + " ";
857  }
858  }
859 
860  bool normalize = m_ui->m_normalizeCheckBox->isChecked();
861 
862  params.m_arithmeticString = arithExpStr;
863  params.m_normalize = normalize;
864 
865  return params;
866 }
867 
869 {
870  emit closeTool();
871 }
const std::string & getErrorMessage() const
Return the current error message if there is any.
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
unsigned int band
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
This class is GUI used to define the arithmeticOp parameters for the RP arithmeticOp operation...
This file defines a class for a ArithmeticOp Dialog.
std::list< te::map::AbstractLayerPtr > getSelecteds() const
virtual const std::string & getTitle() const
It returns the layer title.
std::map< std::string, te::map::AbstractLayer * > getLayers()
virtual const char * what() const
It outputs the exception message.
bool execute(AlgorithmOutputParameters &outputParams) _NOEXCEPT_OP(false)
Executes the algorithm using the supplied parameters.
bool m_normalize
Output values normalization will be performed to fit the original input raster values range (default:...
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
This class is a dialog for the LayerSearchWidget.
std::list< te::map::AbstractLayerPtr > m_layerList
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
bool initialize(const AlgorithmInputParameters &inputParams) _NOEXCEPT_OP(false)
Initialize the algorithm instance making it ready for execution.
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
std::map< std::string, te::map::AbstractLayer * > getMapLayer()
std::map< std::string, te::map::AbstractLayer * > m_mapLayer
int b
Definition: TsRtree.cpp:32
te::rp::ArithmeticOperations::InputParameters paramsFromOpUserdDef()
std::unique_ptr< Ui::ArithmeticOpDialogForm > m_ui
This file has the OutputRasterWidget class.
This file defines a class for a ArithmeticOp Dialog.
URI C++ Library.
Definition: Attributes.h:37
ArithmeticOperations output parameters.
This class is used to set output layer.
Utility functions for the data access module.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state...
std::vector< te::rst::Raster * > m_inputRasters
Input rasters vector.
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)
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
void addLayer(te::map::AbstractLayerPtr layer)
Performs arithmetic operation over raster data.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
void getRasterBands(QComboBox *layer, int index, QComboBox *band)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
ArithmeticOperations input parameters.
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
te::qt::widgets::OutputRasterWidget * m_outputWidget
std::string m_arithmeticString
Arithmetic string.