All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultiResolutionWidget.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/raster/MultiResolutionWidget.cpp
22 
23  \brief A widget to create multi resolution over a raster.
24 */
25 
26 // TerraLib
27 #include "../../../common/Translator.h"
28 #include "../../../dataaccess/datasource/DataSource.h"
29 #include "../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../raster/Interpolator.h"
32 #include "../../../raster/Raster.h"
33 #include "../../../raster/RasterFactory.h"
34 #include "../Exception.h"
35 #include "MultiResolutionWidget.h"
36 #include "ui_MultiResolutionWidgetForm.h"
37 
38 // Qt
39 #include <QFileDialog>
40 #include <QMessageBox>
41 
43 
45  : QWidget(parent, f),
46  m_ui(new Ui::MultiResolutionWidgetForm)
47 {
48 // add controls
49  m_ui->setupUi(this);
50 
51 // set button icons
52  m_ui->m_fileToolButton->setIcon(QIcon::fromTheme("file-raster"));
53  m_ui->m_folderToolButton->setIcon(QIcon::fromTheme("folder-open"));
54 
55 //connects
56  connect(m_ui->m_fileToolButton, SIGNAL(clicked()), this, SLOT(onFileToolButtonClicked()));
57  connect(m_ui->m_folderToolButton, SIGNAL(clicked()), this, SLOT(onFolderToolButtonClicked()));
58 
60  }
61 
63 {
64 }
65 
66 Ui::MultiResolutionWidgetForm* te::qt::widgets::MultiResolutionWidget::getForm() const
67 {
68  return m_ui.get();
69 }
70 
72 {
73  if(m_ui->m_layerRadioButton->isChecked())
74  return fromLayer();
75  else if(m_ui->m_fileRadioButton->isChecked())
76  return fromFile();
77  else if(m_ui->m_folderRadioButton->isChecked())
78  return fromFolder();
79 
80  return false;
81 }
82 
83 void te::qt::widgets::MultiResolutionWidget::setLayerList(std::list<te::map::AbstractLayerPtr>& layerList)
84 {
85  m_ui->m_layerComboBox->clear();
86 
87  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
88 
89  while(it != layerList.end())
90  {
92 
93  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
94 
95  if(dsType->hasRaster())
96  m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
97 
98  ++it;
99  }
100 }
101 
103 {
104  if(m_ui->m_layerComboBox->currentText().isEmpty())
105  {
106  QMessageBox::warning(this, tr("Warning"), tr("Layer not selected."));
107  return false;
108  }
109 
110  int idx = m_ui->m_layerComboBox->currentIndex();
111  QVariant varLayer = m_ui->m_layerComboBox->itemData(idx, Qt::UserRole);
112  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
113 
114  if(!layer.get())
115  {
116  QMessageBox::warning(this, tr("Warning"), tr("Invalid layer selected."));
117  return false;
118  }
119 
120  std::auto_ptr<te::da::DataSet> ds = layer->getData();
121  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
122  std::auto_ptr<te::rst::Raster> rst = ds->getRaster(rpos);
123 
124  if(!rst.get())
125  {
126  QMessageBox::warning(this, tr("Warning"), tr("Selected layer does not have raster representation."));
127  return false;
128  }
129 
130  bool res = createMultiRes(rst.get());
131 
132  if(!res)
133  {
134  QMessageBox::warning(this, tr("Warning"), tr("Error creating multi resolution."));
135  }
136 
137  QMessageBox::information(this, tr("Information"), tr("Multi resolution created."));
138 
139  return res;
140 }
141 
143 {
144  if(m_ui->m_fileLineEdit->text().isEmpty())
145  {
146  QMessageBox::warning(this, tr("Warning"), tr("File not selected."));
147  return false;
148  }
149 
150  std::map<std::string, std::string> rinfo;
151  rinfo["URI"] = m_ui->m_fileLineEdit->text().toStdString();
152 
153  std::auto_ptr<te::rst::Raster> rst(te::rst::RasterFactory::open(rinfo));
154 
155  if(!rst.get())
156  {
157  QMessageBox::warning(this, tr("Warning"), tr("Invalid file selected."));
158  return false;
159  }
160 
161  bool res = createMultiRes(rst.get());
162 
163  if(!res)
164  {
165  QMessageBox::warning(this, tr("Warning"), tr("Error creating multi resolution."));
166  }
167 
168  QMessageBox::information(this, tr("Information"), tr("Multi resolution created."));
169 
170  return res;
171 }
172 
174 {
175  if(m_ui->m_folderLineEdit->text().isEmpty())
176  {
177  QMessageBox::warning(this, tr("Warning"), tr("Folder not selected."));
178  return false;
179  }
180 
181  std::map<std::string, std::string> dsInfo;
182  dsInfo["SOURCE"] = m_ui->m_folderLineEdit->text().toStdString();
183 
184  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL");
185 
186  ds->setConnectionInfo(dsInfo);
187 
188  ds->open();
189 
190  std::vector<std::string> rasterNames = ds->getDataSetNames();
191 
192  for(std::size_t t = 0; t < rasterNames.size(); ++t)
193  {
194  std::auto_ptr<te::da::DataSet> dataSet = ds->getDataSet(rasterNames[t]);
195 
196  if(!dataSet.get())
197  continue;
198 
199  std::size_t rpos = te::da::GetFirstPropertyPos(dataSet.get(), te::dt::RASTER_TYPE);
200  std::auto_ptr<te::rst::Raster> rst = dataSet->getRaster(rpos);
201 
202  if(rst.get())
203  {
204  bool res = createMultiRes(rst.get());
205 
206  if(!res)
207  {
208  std::string errorMsg = "Error creating multi resolution. File: " + rasterNames[t] + ".";
209  QMessageBox::warning(this, tr("Warning"), errorMsg.c_str());
210  return false;
211  }
212  }
213  }
214 
215  QMessageBox::information(this, tr("Information"), tr("Multi resolution created."));
216 
217  return true;
218 }
219 
221 {
222  if(!raster || raster->getAccessPolicy() == te::common::NoAccess)
223  {
224  QMessageBox::warning(this, tr("Warning"), tr("Invalid Raster."));
225  return false;
226  }
227 
228  QApplication::setOverrideCursor(Qt::WaitCursor);
229 
230  try
231  {
232  unsigned int nLevels = m_ui->m_levelsSpinBox->value();
233 
234  int interpolationIdx = m_ui->m_interpolatorComboBox->currentIndex();
235  te::rst::Interpolator::Method interpolator = (te::rst::Interpolator::Method)m_ui->m_interpolatorComboBox->itemData(interpolationIdx).toInt();
236 
237  raster->createMultiResolution(nLevels, interpolator);
238  }
239  catch(...)
240  {
241  QApplication::restoreOverrideCursor();
242 
243  return false;
244  }
245 
246  QApplication::restoreOverrideCursor();
247 
248  return true;
249 }
250 
252 {
253  m_ui->m_interpolatorComboBox->clear();
254 
255  m_ui->m_interpolatorComboBox->addItem(tr("Nearest Neighbor"), te::rst::NearestNeighbor);
256  m_ui->m_interpolatorComboBox->addItem(tr("Bilinear"), te::rst::Bilinear);
257  m_ui->m_interpolatorComboBox->addItem(tr("Bicubic"), te::rst::Bicubic);
258 }
259 
261 {
262  QString filter = tr("Image File (*.png *.jpg *.jpeg *.tif *.tiff *.geotif *.geotiff);; Web Map Service - WMS (*.xml *.wms);; Web Coverage Service - WCS (*.xml *.wcs);; All Files (*.*)");
263 
264  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Raster File"), "", filter);
265 
266  if(fileName.isEmpty())
267  return;
268 
269  m_ui->m_fileLineEdit->setText(fileName);
270 }
271 
273 {
274  QString folderName = QFileDialog::getExistingDirectory(this, tr("Select a directory"), "");
275 
276  if(folderName.isEmpty())
277  return;
278 
279  m_ui->m_folderLineEdit->setText(folderName);
280 }
Ui::MultiResolutionWidgetForm * getForm() const
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
Near neighborhood interpolation method.
Definition: Enums.h:95
virtual bool createMultiResolution(const unsigned int levels, const InterpolationMethod interpMethod)=0
Create a sub-sampled multi-resolution pyramid.
MultiResolutionWidget(QWidget *parent=0, Qt::WindowFlags f=0)
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:92
InterpolationMethod Method
Allowed interpolation methods.
Definition: Interpolator.h:62
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
A widget to create multi resolution over a raster.
An abstract class for raster data strucutures.
Definition: Raster.h:71
static std::auto_ptr< DataSource > make(const std::string &dsType)
std::auto_ptr< Ui::MultiResolutionWidgetForm > m_ui
Bicubic interpolation method.
Definition: Enums.h:97
bool createMultiRes(te::rst::Raster *raster)
void setLayerList(std::list< te::map::AbstractLayerPtr > &layerList)
Bilinear interpolation method.
Definition: Enums.h:96
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.