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 "../../../core/translator/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 
44 te::qt::widgets::MultiResolutionWidget::MultiResolutionWidget(QWidget* parent, Qt::WindowFlags f)
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 
59  fillInterpolatorTypes();
60  }
61 
63 
64  Ui::MultiResolutionWidgetForm*
66  {
67  return m_ui.get();
68 }
69 
71 {
72  if(m_ui->m_layerRadioButton->isChecked())
73  return fromLayer();
74  else if(m_ui->m_fileRadioButton->isChecked())
75  return fromFile();
76  else if(m_ui->m_folderRadioButton->isChecked())
77  return fromFolder();
78 
79  return false;
80 }
81 
82 void te::qt::widgets::MultiResolutionWidget::setLayerList(std::list<te::map::AbstractLayerPtr>& layerList)
83 {
84  m_ui->m_layerComboBox->clear();
85 
86  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
87 
88  while(it != layerList.end())
89  {
91 
92  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
93 
94  if(dsType->hasRaster())
95  m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
96 
97  ++it;
98  }
99 }
100 
102 {
103  if(m_ui->m_layerComboBox->currentText().isEmpty())
104  {
105  QMessageBox::warning(this, tr("Warning"), tr("Layer not selected."));
106  return false;
107  }
108 
109  int idx = m_ui->m_layerComboBox->currentIndex();
110  QVariant varLayer = m_ui->m_layerComboBox->itemData(idx, Qt::UserRole);
111  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
112 
113  if(!layer.get())
114  {
115  QMessageBox::warning(this, tr("Warning"), tr("Invalid layer selected."));
116  return false;
117  }
118 
119  std::unique_ptr<te::da::DataSet> ds = layer->getData();
120  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
121  std::unique_ptr<te::rst::Raster> rst = ds->getRaster(rpos);
122 
123  if(!rst.get())
124  {
125  QMessageBox::warning(this, tr("Warning"), tr("Selected layer does not have raster representation."));
126  return false;
127  }
128 
129  bool res = createMultiRes(rst.get());
130 
131  if(!res)
132  {
133  QMessageBox::warning(this, tr("Warning"), tr("Error creating multi resolution."));
134  }
135 
136  QMessageBox::information(this, tr("Information"), tr("Multi resolution created."));
137 
138  return res;
139 }
140 
142 {
143  if(m_ui->m_fileLineEdit->text().isEmpty())
144  {
145  QMessageBox::warning(this, tr("Warning"), tr("File not selected."));
146  return false;
147  }
148 
149  std::map<std::string, std::string> rinfo;
150  rinfo["URI"] = m_ui->m_fileLineEdit->text().toUtf8().data();
151 
152  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::open(rinfo));
153 
154  if(!rst.get())
155  {
156  QMessageBox::warning(this, tr("Warning"), tr("Invalid file selected."));
157  return false;
158  }
159 
160  bool res = createMultiRes(rst.get());
161 
162  if(!res)
163  {
164  QMessageBox::warning(this, tr("Warning"), tr("Error creating multi resolution."));
165  }
166 
167  QMessageBox::information(this, tr("Information"), tr("Multi resolution created."));
168 
169  return res;
170 }
171 
173 {
174  if(m_ui->m_folderLineEdit->text().isEmpty())
175  {
176  QMessageBox::warning(this, tr("Warning"), tr("Folder not selected."));
177  return false;
178  }
179 
180  const std::string dsInfo("file://" + std::string(m_ui->m_folderLineEdit->text().toUtf8().data()));
181  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL", dsInfo);
182 
183  ds->open();
184 
185  std::vector<std::string> rasterNames = ds->getDataSetNames();
186 
187  for(std::size_t t = 0; t < rasterNames.size(); ++t)
188  {
189  std::unique_ptr<te::da::DataSet> dataSet = ds->getDataSet(rasterNames[t]);
190 
191  if(!dataSet.get())
192  continue;
193 
194  std::size_t rpos = te::da::GetFirstPropertyPos(dataSet.get(), te::dt::RASTER_TYPE);
195  std::unique_ptr<te::rst::Raster> rst = dataSet->getRaster(rpos);
196 
197  if(rst.get())
198  {
199  bool res = createMultiRes(rst.get());
200 
201  if(!res)
202  {
203  std::string errorMsg = "Error creating multi resolution. File: " + rasterNames[t] + ".";
204  QMessageBox::warning(this, tr("Warning"), errorMsg.c_str());
205  return false;
206  }
207  }
208  }
209 
210  QMessageBox::information(this, tr("Information"), tr("Multi resolution created."));
211 
212  return true;
213 }
214 
216 {
217  if(!raster || raster->getAccessPolicy() == te::common::NoAccess)
218  {
219  QMessageBox::warning(this, tr("Warning"), tr("Invalid Raster."));
220  return false;
221  }
222 
223  QApplication::setOverrideCursor(Qt::WaitCursor);
224 
225  try
226  {
227  unsigned int nLevels = m_ui->m_levelsSpinBox->value();
228 
229  int interpolationIdx = m_ui->m_interpolatorComboBox->currentIndex();
230  te::rst::Interpolator::Method interpolator = (te::rst::Interpolator::Method)m_ui->m_interpolatorComboBox->itemData(interpolationIdx).toInt();
231 
232  raster->createMultiResolution(nLevels, interpolator);
233  }
234  catch(...)
235  {
236  QApplication::restoreOverrideCursor();
237 
238  return false;
239  }
240 
241  QApplication::restoreOverrideCursor();
242 
243  return true;
244 }
245 
247 {
248  m_ui->m_interpolatorComboBox->clear();
249 
250  m_ui->m_interpolatorComboBox->addItem(tr("Nearest Neighbor"), te::rst::NearestNeighbor);
251  m_ui->m_interpolatorComboBox->addItem(tr("Bilinear"), te::rst::Bilinear);
252  m_ui->m_interpolatorComboBox->addItem(tr("Bicubic"), te::rst::Bicubic);
253 }
254 
256 {
257  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 (*.*)");
258 
259  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Raster File"), "", filter);
260 
261  if(fileName.isEmpty())
262  return;
263 
264  m_ui->m_fileLineEdit->setText(fileName);
265 }
266 
268 {
269  QString folderName = QFileDialog::getExistingDirectory(this, tr("Select a directory"), "");
270 
271  if(folderName.isEmpty())
272  return;
273 
274  m_ui->m_folderLineEdit->setText(folderName);
275 }
Ui::MultiResolutionWidgetForm * getForm() const
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Near neighborhood interpolation method.
virtual bool createMultiResolution(const unsigned int levels, const InterpolationMethod interpMethod)=0
Create a sub-sampled multi-resolution pyramid.
std::unique_ptr< Ui::MultiResolutionWidgetForm > m_ui
static te::dt::Date ds(2010, 01, 01)
InterpolationMethod
Allowed interpolation methods.
InterpolationMethod Method
Allowed interpolation methods.
Definition: Interpolator.h:62
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
A widget to create multi resolution over a raster.
An abstract class for raster data strucutures.
URI C++ Library.
Definition: Attributes.h:37
Bicubic interpolation method.
bool createMultiRes(te::rst::Raster *raster)
void setLayerList(std::list< te::map::AbstractLayerPtr > &layerList)
Bilinear interpolation method.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
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.