All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SegmenterWizard.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/SegmenterWizard.cpp
22 
23  \brief A Qt dialog that allows users to run a segmenter operation defined by RP module.
24 */
25 
26 // TerraLib
27 #include "../../../common/progress/ProgressManager.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../raster/Raster.h"
31 #include "../../../rp/Segmenter.h"
32 #include "../../../rp/SegmenterRegionGrowingStrategy.h"
33 #include "../../../rp/Module.h"
34 #include "../help/HelpPushButton.h"
35 #include "../layer/search/LayerSearchWidget.h"
36 #include "../layer/search/LayerSearchWizardPage.h"
37 #include "../progress/ProgressViewerDialog.h"
38 #include "SegmenterWizard.h"
40 #include "SegmenterWizardPage.h"
41 #include "RasterInfoWidget.h"
42 #include "RasterInfoWizardPage.h"
43 #include "ui_SegmenterAdvancedOptionsWizardPageForm.h"
44 #include "Utils.h"
45 
46 // STL
47 #include <cassert>
48 
49 // Qt
50 #include <QMessageBox>
51 
52 // Boost
53 #include <boost/timer.hpp>
54 #include <boost/format.hpp>
55 
56 
58  : QWizard(parent)
59 {
60  //configure the wizard
61  this->setWizardStyle(QWizard::ModernStyle);
62  this->setWindowTitle(tr("Segmenter"));
63  //this->setFixedSize(640, 580);
64 
65  this->setOption(QWizard::HaveHelpButton, true);
66  this->setOption(QWizard::HelpButtonOnRight, false);
67 
69 
70  this->setButton(QWizard::HelpButton, helpButton);
71 
72  helpButton->setPageReference("plugins/rp/rp_segmenter.html");
73 
74  addPages();
75 }
76 
78 {
79 
80 }
81 
83 {
84  if(currentPage() == m_layerSearchPage.get())
85  {
86  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
87 
88  if(list.empty() == false)
89  {
90  te::map::AbstractLayerPtr l = *list.begin();
91 
92  m_segmenterPage->set(l);
93  }
94 
95  return m_layerSearchPage->isComplete();
96  }
97  else if(currentPage() == m_segmenterPage.get())
98  {
99  bool res = m_segmenterPage->isComplete();
100 
101  if(!res)
102  QMessageBox::warning(this, tr("Warning"), tr("Select at least one band."));
103 
104  return res;
105  }
106  else if(currentPage() == m_rasterInfoPage.get())
107  {
108  return execute();
109  }
110 
111  return true;
112 }
113 
114 void te::qt::widgets::SegmenterWizard::setList(std::list<te::map::AbstractLayerPtr>& layerList)
115 {
116  m_layerSearchPage->getSearchWidget()->setList(layerList);
117  m_layerSearchPage->getSearchWidget()->filterOnlyByRaster();
118 }
119 
121 {
122  removePage(m_layerSearchId);
123 
124  m_segmenterPage->set(layer);
125 }
126 
128 {
129  return m_outputLayer;
130 }
131 
133 {
134  m_layerSearchPage.reset(new te::qt::widgets::LayerSearchWizardPage(this));
135  m_segmenterPage.reset(new te::qt::widgets::SegmenterWizardPage(this));
136  m_segmenterAdvOptPage.reset(new te::qt::widgets::SegmenterAdvancedOptionsWizardPage(this));
137  m_rasterInfoPage.reset(new te::qt::widgets::RasterInfoWizardPage(this));
138 
139  m_layerSearchId = addPage(m_layerSearchPage.get());
140  addPage(m_segmenterPage.get());
141  addPage(m_segmenterAdvOptPage.get());
142  addPage(m_rasterInfoPage.get());
143 
144  //for contrast only one layer can be selected
145  m_layerSearchPage->getSearchWidget()->enableMultiSelection(false);
146 }
147 
149 {
150  if(m_rasterInfoPage->getWidget()->fileExists())
151  {
152  QMessageBox::warning(this, tr("Segmenter"), tr("File already exists."));
153  return false;
154  }
155 
156  //get layer
157  te::map::AbstractLayerPtr l = m_segmenterPage->get();
158  std::auto_ptr<te::da::DataSet> ds(l->getData());
159 
160  //run contrast
161  te::rp::Segmenter algorithmInstance;
162 
163  te::rp::Segmenter::InputParameters algoInputParams = m_segmenterPage->getInputParams();
164 
165  algoInputParams.m_enableProgress = true;
166  algoInputParams.m_enableThreadedProcessing = m_segmenterAdvOptPage->getForm()->m_enableThreadedProcessingcheckBox->isChecked();
167  algoInputParams.m_maxSegThreads = m_segmenterAdvOptPage->getForm()->m_maximumThreadsNumberLineEdit->text().toUInt();
168  algoInputParams.m_enableBlockProcessing = m_segmenterAdvOptPage->getForm()->m_enableBlockProcessingcheckBox->isChecked();
169  algoInputParams.m_maxBlockSize = m_segmenterAdvOptPage->getForm()->m_maximumBlockSizeLineEdit->text().toUInt();
170  algoInputParams.m_blocksOverlapPercent = m_segmenterAdvOptPage->getForm()->m_blockOverlapPercentSpinBox->value();
171 
172  te::rp::Segmenter::OutputParameters algoOutputParams;
173  algoOutputParams.m_rType = m_rasterInfoPage->getWidget()->getType();
174  algoOutputParams.m_rInfo = m_rasterInfoPage->getWidget()->getInfo();
175 
176  //progress
179 
180  QApplication::setOverrideCursor(Qt::WaitCursor);
181 
182  try
183  {
184  if(algorithmInstance.initialize(algoInputParams))
185  {
186  boost::timer timer;
187 
188  if(algorithmInstance.execute(algoOutputParams))
189  {
190  algoOutputParams.reset();
191 
192  //set output layer
193  m_outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
194  m_rasterInfoPage->getWidget()->getInfo());
195  std::string elapsedTimeStr = boost::str( boost::format( "%.2f" ) % timer.elapsed() );
196  QMessageBox::information(this, tr("Segmenter"), tr("Segmenter ended sucessfully") + ( " (" + elapsedTimeStr + " " ).c_str() + tr("seconds") + ")" );
197  }
198  else
199  {
200  QMessageBox::critical(this, tr("Segmenter"), tr("Segmenter execution error"));
201 
203 
204  QApplication::restoreOverrideCursor();
205 
206  return false;
207  }
208  }
209  else
210  {
211  QMessageBox::critical(this, tr("Segmenter"), tr("Segmenter initialization error") +
212  ( " " + te::rp::Module::getLastLogStr() ).c_str());
213 
215 
216  QApplication::restoreOverrideCursor();
217 
218  return false;
219  }
220  }
221  catch(const std::exception& e)
222  {
223  QMessageBox::warning(this, tr("Segmenter"), e.what());
224 
226 
227  QApplication::restoreOverrideCursor();
228 
229  return false;
230  }
231  catch(...)
232  {
233  QMessageBox::warning(this, tr("Segmenter"), tr("An exception has occurred!"));
234 
236 
237  QApplication::restoreOverrideCursor();
238 
239  return false;
240  }
241 
243 
244  QApplication::restoreOverrideCursor();
245 
246  return true;
247 }
248 
unsigned char m_blocksOverlapPercent
The percentage of blocks overlapped area (valid range:0-25, defaul:10).
Definition: Segmenter.h:99
Utility functions for the data access module.
Segmenter Output Parameters.
Definition: Segmenter.h:149
Raster segmentation.
Definition: Segmenter.h:73
void setPageReference(const QString &ref)
Sets the documentation page reference.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Segmenter.h:153
void setLayer(te::map::AbstractLayerPtr layer)
This file defines a class for a Segmenter Advanced Options Wizard page.
This file defines a class for a Raster Info Wizard page.
unsigned int m_maxBlockSize
The input image will be split into blocks with this width for processing, this parameter tells the ma...
Definition: Segmenter.h:97
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Segmenter.h:103
te::map::AbstractLayerPtr getOutputLayer()
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Segmenter.cpp:161
This class is GUI used to define the segmenter advanced options parameters for the RP constast operat...
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 segmenter parameters for the RP constast operation.
This class is GUI used to define the raster info parameters for raster factory.
This file has the RasterInfoWidget class.
Segmenter Input Parameters.
Definition: Segmenter.h:81
A Qt dialog that allows users to run a segmenter operation defined by RP module.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Segmenter.h:155
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
Definition: Segmenter.h:91
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
Definition: Segmenter.h:93
This file defines a class for a Segmenter Wizard page.
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
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...
Definition: Segmenter.h:95