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" 45 #include "ui_ArithmeticOpDialogForm.h" 49 #include <boost/filesystem.hpp> 50 #include <boost/algorithm/string.hpp> 51 #include <boost/property_tree/json_parser.hpp> 54 #include <QApplication> 55 #include <QGridLayout> 56 #include <QMessageBox> 57 #include <QFileDialog> 60 #include <QPushButton> 61 #include <QTextStream> 68 te::qt::widgets::ArithmeticOpDialogForm::ArithmeticOpDialogForm(
QWidget* parent)
70 m_ui(new
Ui::ArithmeticOpDialogForm)
76 QGridLayout* outputLayout =
new QGridLayout(m_ui->m_outputWidget);
77 outputLayout->setContentsMargins(0, 0, 0, 0);
79 outputLayout->addWidget(m_outputWidget);
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"));
86 m_expressionHistoryCounter = -1;
90 connect(m_ui->m_searchLayerToolButton, SIGNAL(pressed()),
this, SLOT(onSearchLayerToolButtonPressed()));
91 connect(m_ui->m_addLayerPushButton, SIGNAL(clicked()),
this, SLOT(onAddLayerPushButtonClicked()));
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()));
126 connect(m_ui->m_okPushButton, SIGNAL(clicked()),
this, SLOT(onOkPushButtonClicked()));
128 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
129 m_ui->m_helpPushButton->setPageReference(
"plugins/rp/rp_arithOp.html");
154 return m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
179 if (
m_ui->m_inputLayerLineEdit->text().isEmpty())
182 QString operation =
m_ui->m_operationTextEdit->toPlainText() +
m_ui->m_inputLayerLineEdit->text() +
":" +
m_ui->m_bandComboBox->currentText();
184 m_ui->m_operationTextEdit->setText(operation);
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();
202 m_ui->m_bandComboBox->clear();
206 std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
208 while (it != layerList.end())
210 std::unique_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
212 if(dsType->hasRaster())
216 std::string layerTitle = it->get()->
getTitle();
217 m_ui->m_inputLayerLineEdit->setText(layerTitle.c_str());
220 for (std::size_t
b = 0;
b < raster->getNumberOfBands();
b++)
221 m_ui->m_bandComboBox->addItem(QString::number(
b));
231 QString txt =
m_ui->m_operationTextEdit->toPlainText();
233 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
234 txt.replace(txt.size() - 1, txt.size(),
"");
236 m_ui->m_operationTextEdit->setText(txt +
" sqrt( ");
241 QString txt =
m_ui->m_operationTextEdit->toPlainText();
243 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
244 txt.replace(txt.size() - 1, txt.size(),
"");
246 m_ui->m_operationTextEdit->setText(txt +
" ( ");
251 QString txt =
m_ui->m_operationTextEdit->toPlainText();
253 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
254 txt.replace(txt.size() - 1, txt.size(),
"");
256 m_ui->m_operationTextEdit->setText(txt +
" ) ");
261 QString txt =
m_ui->m_operationTextEdit->toPlainText();
263 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
264 txt.replace(txt.size() - 1, txt.size(),
"");
266 m_ui->m_operationTextEdit->setText(txt +
" sin( ");
271 QString txt =
m_ui->m_operationTextEdit->toPlainText();
273 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
274 txt.replace(txt.size() - 1, txt.size(),
"");
276 m_ui->m_operationTextEdit->setText(txt +
" asin( ");
281 QString txt =
m_ui->m_operationTextEdit->toPlainText();
286 if (txt.at(txt.size() - 1) ==
' ')
287 txt.replace(txt.size() - 1, txt.size(),
"");
289 m_ui->m_operationTextEdit->setText(txt +
" ^ ");
294 QString txt =
m_ui->m_operationTextEdit->toPlainText();
296 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
297 txt.replace(txt.size() - 1, txt.size(),
"");
299 m_ui->m_operationTextEdit->setText(txt +
" cos( ");
304 QString txt =
m_ui->m_operationTextEdit->toPlainText();
306 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
307 txt.replace(txt.size() - 1, txt.size(),
"");
309 m_ui->m_operationTextEdit->setText(txt +
" acos( ");
314 QString txt =
m_ui->m_operationTextEdit->toPlainText();
316 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
317 txt.replace(txt.size() - 1, txt.size(),
"");
319 m_ui->m_operationTextEdit->setText(txt +
" log( ");
324 QString txt =
m_ui->m_operationTextEdit->toPlainText();
326 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
327 txt.replace(txt.size() - 1, txt.size(),
"");
329 m_ui->m_operationTextEdit->setText(txt +
" tan( ");
334 QString txt =
m_ui->m_operationTextEdit->toPlainText();
336 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
337 txt.replace(txt.size() - 1, txt.size(),
"");
339 m_ui->m_operationTextEdit->setText(txt +
" atan( ");
344 QString txt =
m_ui->m_operationTextEdit->toPlainText();
346 if (!txt.isEmpty() && txt.at(txt.size() - 1) ==
' ')
347 txt.replace(txt.size() - 1, txt.size(),
"");
349 m_ui->m_operationTextEdit->setText(txt +
" ln( ");
354 QString txt =
m_ui->m_operationTextEdit->toPlainText();
355 m_ui->m_operationTextEdit->setText(txt +
"0");
360 QString txt =
m_ui->m_operationTextEdit->toPlainText();
361 m_ui->m_operationTextEdit->setText(txt +
"1");
366 QString txt =
m_ui->m_operationTextEdit->toPlainText();
367 m_ui->m_operationTextEdit->setText(txt +
"2");
372 QString txt =
m_ui->m_operationTextEdit->toPlainText();
373 m_ui->m_operationTextEdit->setText(txt +
"3");
378 QString txt =
m_ui->m_operationTextEdit->toPlainText();
379 m_ui->m_operationTextEdit->setText(txt +
"4");
384 QString txt =
m_ui->m_operationTextEdit->toPlainText();
385 m_ui->m_operationTextEdit->setText(txt +
"5");
390 QString txt =
m_ui->m_operationTextEdit->toPlainText();
391 m_ui->m_operationTextEdit->setText(txt +
"6");
396 QString txt =
m_ui->m_operationTextEdit->toPlainText();
397 m_ui->m_operationTextEdit->setText(txt +
"7");
402 QString txt =
m_ui->m_operationTextEdit->toPlainText();
403 m_ui->m_operationTextEdit->setText(txt +
"8");
408 QString txt =
m_ui->m_operationTextEdit->toPlainText();
409 m_ui->m_operationTextEdit->setText(txt +
"9");
414 QString txt =
m_ui->m_operationTextEdit->toPlainText();
415 m_ui->m_operationTextEdit->setText(txt +
".");
420 if (
m_ui->m_operationTextEdit->toPlainText().isEmpty())
423 std::string operation =
m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
428 for (i = static_cast<int>(operation.size() - 1); i >= 0; i--)
430 if (operation.at(i) ==
' ' && i !=
static_cast<int>(operation.size()) - 1)
435 if (operation.at(i) ==
'.')
436 decimal = static_cast<int>(operation.size()) - 1 - i;
438 txt = operation.at(i) + txt;
441 QString txt1 = txt.c_str();
443 double val = txt1.toDouble(&isOk);
458 m_ui->m_operationTextEdit->clear();
459 m_ui->m_operationTextEdit->setText(operation.c_str());
464 QString txt =
m_ui->m_operationTextEdit->toPlainText();
469 if (txt.at(txt.size() - 1) ==
' ')
470 txt.replace(txt.size() - 1, txt.size(),
"");
472 m_ui->m_operationTextEdit->setText(txt +
" / ");
477 QString txt =
m_ui->m_operationTextEdit->toPlainText();
482 if (txt.at(txt.size() - 1) ==
' ')
483 txt.replace(txt.size() - 1, txt.size(),
"");
485 m_ui->m_operationTextEdit->setText(txt +
" * ");
490 QString txt =
m_ui->m_operationTextEdit->toPlainText();
495 if (txt.at(txt.size() - 1) ==
' ')
496 txt.replace(txt.size() - 1, txt.size(),
"");
498 m_ui->m_operationTextEdit->setText(txt +
" - ");
503 QString txt =
m_ui->m_operationTextEdit->toPlainText();
508 if (txt.at(txt.size() - 1) ==
' ')
509 txt.replace(txt.size() - 1, txt.size(),
"");
511 m_ui->m_operationTextEdit->setText(txt +
" + ");
516 if (
m_ui->m_operationTextEdit->toPlainText().isEmpty())
519 std::string operation =
m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
522 for (
int i = static_cast<int>(operation.size()) - 1; i >= 0; i--)
524 if (operation.at(i) ==
' ' && i !=
static_cast<int>(operation.size()) - 1)
526 operation.replace(i + 1, operation.size() - 1,
"");
530 operation.replace(i, operation.size(),
"");
533 m_ui->m_operationTextEdit->clear();
534 m_ui->m_operationTextEdit->setText(operation.c_str());
539 m_ui->m_operationTextEdit->setText(
"");
565 QMessageBox::warning(
this, tr(
"Arithmetic Operations"), tr(e.
what()));
570 QMessageBox::warning(
this, tr(
"Arithmetic Operations"), tr(
"An error has occurred."));
577 if (
m_ui->m_operationTextEdit->toPlainText().isEmpty())
580 std::string arithOp =
m_ui->m_operationTextEdit->toPlainText().toUtf8().data();
581 std::string arithExpStr;
584 std::vector<std::string> arithExpVec;
585 boost::split(arithExpVec, arithOp, boost::is_any_of(
" "));
587 for (
unsigned int i = 0; i < arithExpVec.size(); i++)
589 int pos =
static_cast<int>(arithExpVec[i].find(
":"));
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);
602 arithExpStr += arithExpVec[i] +
" ";
605 QString newArithExpStr = QString::fromStdString(arithExpStr);
607 arithExpStr = newArithExpStr.trimmed().toUtf8().data();
613 std::string jsonfile =
m_userPath +
"/arithmeticOperations.json";
615 boost::property_tree::ptree pt;
616 boost::property_tree::ptree children;
617 boost::property_tree::ptree operations;
619 boost::property_tree::json_parser::read_json(jsonfile, pt);
621 for(boost::property_tree::ptree::value_type &v: pt.get_child(
"operations"))
623 std::string name = v.second.get<std::string>(
"name");
624 std::string op = v.second.get<std::string>(
"operation");
628 QMessageBox::warning(
this, tr(
"Arithmetic Operations"), tr(
"The operation already exist."));
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));
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));
643 operations.add_child(
"operations", children);
645 boost::property_tree::write_json(jsonfile, operations);
647 QMessageBox::information(
this, tr(
"Arithmetic Operations"), tr(
"The operation was saved."));
651 QMessageBox::warning(
this, tr(
"Arithmetic Operations"), tr(e.
what()));
656 QMessageBox::warning(
this, tr(
"Arithmetic Operation"), tr(
"An exception has occurred!"));
666 QMessageBox::information(
this,
"Arithmetic Operations",
"Output image is not defined.");
672 QMessageBox::information(
this,
"Arithmetic Operations",
"Error to execute the operation.");
680 QVariant varLayer = layer->itemData(index, Qt::UserRole);
691 for(std::size_t t = 0; t < rst->getNumberOfBands(); ++t)
692 band->addItem(QString::number(t));
705 QMessageBox::warning(
this, tr(
"Arithmetic Operations"), tr(
"File already exists."));
712 QApplication::setOverrideCursor(Qt::WaitCursor);
722 std::map<std::string, std::string> rinfo;
726 algoOutputParams.
m_rInfo = rinfo;
732 if (algorithmInstance.
initialize(algoInputParams))
734 if (algorithmInstance.
execute(algoOutputParams))
736 algoOutputParams.
reset();
741 QApplication::restoreOverrideCursor();
742 QMessageBox::information(
this, tr(
"Arithmetic Operation"), tr(
"Arithmetic Operation ended successfully."));
746 QApplication::restoreOverrideCursor();
748 QMessageBox::critical(
this, tr(
"Arithmetic Operation"),
749 tr(
"Arithmetic Operation execution error.") + (
" " +
757 QApplication::restoreOverrideCursor();
758 QMessageBox::critical(
this, tr(
"Arithmetic Operation"), tr(
"Arithmetic Operation initialization error") +
764 catch (
const std::exception& e)
766 QApplication::restoreOverrideCursor();
767 QMessageBox::warning(
this, tr(
"Arithmetic Operation"), e.what());
773 QApplication::restoreOverrideCursor();
774 QMessageBox::warning(
this, tr(
"Arithmetic Operation"), tr(
"An exception has occurred!"));
779 QApplication::restoreOverrideCursor();
790 QString path = QString::fromStdString(
m_userPath) +
"/arithmeticOperations.json";
792 if(QFile(path).exists())
797 QFile defaultFile(defaultPath);
799 if(!defaultFile.exists())
802 defaultFile.open(QIODevice::ReadOnly);
803 QString input = defaultFile.readAll();
807 file.open(QIODevice::WriteOnly);
808 QTextStream out(&file);
809 out << input.toUtf8().data();
815 std::string arithExpStr;
818 std::map<std::string, te::map::AbstractLayer*> mapLayer =
getMapLayer();
820 std::map<std::string, te::map::AbstractLayer*>::iterator it = mapLayer.begin();
821 while (it != mapLayer.end())
833 if (arithExpStr.empty())
838 std::vector<std::string> arithExpVec;
839 boost::split(arithExpVec, expression, boost::is_any_of(
" "));
841 for (
unsigned int i = 0; i < arithExpVec.size(); i++)
843 int pos =
static_cast<int>(arithExpVec[i].find(
":"));
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);
856 arithExpStr += arithExpVec[i] +
" ";
860 bool normalize =
m_ui->m_normalizeCheckBox->isChecked();
const std::string & getErrorMessage() const
Return the current error message if there is any.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
This file defines a class for a ArithmeticOp Dialog.
virtual const std::string & getTitle() const
It returns the layer title.
virtual const char * what() const
It outputs the exception message.
bool execute(AlgorithmOutputParameters &outputParams) _NOEXCEPT_OP(false)
Executes the algorithm using the supplied parameters.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
#define TE_TR(message)
It marks a string in order to get translated.
bool initialize(const AlgorithmInputParameters &inputParams) _NOEXCEPT_OP(false)
Initialize the algorithm instance making it ready for execution.
ArithmeticOperations output parameters.
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...
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
Performs arithmetic operation over raster data.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
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 $