RasterInfoWidget.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/RasterInfoWidget.cpp
22 
23  \brief This file has the RasterInfoWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/datasource/DataSource.h"
28 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
29 #include "../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../srs/SpatialReferenceSystemManager.h"
31 #include "../../../core/filesystem/FileSystem.h"
32 #include "../../../common/StringUtils.h"
33 #include "../srs/SRSManagerDialog.h"
34 #include "../utils/ParameterTableWidget.h"
35 #include "../Utils.h"
36 #include "../utils/FileDialog.h"
37 #include "../Exception.h"
38 #include "RasterInfoWidget.h"
39 #include "ui_RasterInfoWidgetForm.h"
40 #include "ui_ParameterTableWidgetForm.h"
41 
42 // QT
43 #include <QFileDialog>
44 #include <QString>
45 #include <QMessageBox>
46 
47 // BOOST Includes
48 #include <boost/tokenizer.hpp>
49 #include <boost/lexical_cast.hpp>
50 #include <boost/filesystem.hpp>
51 
52 // STL
53 #include <fstream>
54 
55 
57  const bool outputMode,
58  QWidget* parent, Qt::WindowFlags f)
59  : QWidget(parent, f),
60  m_outputMode( outputMode ),
61  m_ui(new Ui::RasterInfoWidgetForm)
62 {
63  m_ui->setupUi(this);
64 
65  QGridLayout* layout = new QGridLayout(m_ui->m_widget);
66  m_table.reset( new te::qt::widgets::ParameterTableWidget(m_ui->m_widget));
67  layout->addWidget(m_table.get());
68  layout->setContentsMargins(0,0,0,0);
69 
70  m_ui->m_openFileDlgToolButton->setIcon(QIcon::fromTheme("file-raster"));
71  m_ui->m_openSRIDDlgToolButton->setIcon(QIcon::fromTheme("srs"));
72 
73  m_table->getForm()->m_parameterTitle->setText(tr("Extra parameters"));
74 
75  if( outputMode )
76  {
77  m_ui->m_forceRAWRastercheckBox->setEnabled( false );
78  }
79  else
80  {
81  m_ui->m_forceRAWRastercheckBox->setEnabled( true );
82  }
83 
84  m_table->setVisible(false);
85 
86  //connects
87  connect(m_ui->m_openFileDlgToolButton, SIGNAL(clicked()), this, SLOT(onOpenFileDlgToolButtonClicked()));
88  connect(m_ui->m_openSRIDDlgToolButton, SIGNAL(clicked()), this, SLOT(onOpenSRIDDlgToolButtonClicked()));
89 
90  connect(m_ui->m_forceRAWRastercheckBox, SIGNAL(stateChanged(int)), this, SLOT(rawRasterCheckBoxStateChanged(int)));
91  connect(m_ui->m_rowsLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
92  connect(m_ui->m_columnsLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
93  connect(m_ui->m_bandsLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
94  connect(m_ui->m_dataTypeComboBox, SIGNAL(currentTextChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
95 
96  connect(m_ui->m_resolutionXLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
97  connect(m_ui->m_resolutionYLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
98  connect(m_ui->m_sridLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
99 
100  connect(m_ui->m_upperLeftXLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
101  connect(m_ui->m_upperLeftYLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
102  connect(m_ui->m_byteOrderComboBox, SIGNAL(currentTextChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
103 
104  connect(m_ui->m_interleaveComboBox, SIGNAL(currentTextChanged(const QString &)), this, SLOT(rawRasterInfoChanged(const QString &)));
105  connect(m_ui->m_extraParametersCheckBox, SIGNAL(clicked(bool)), this, SLOT(onExtraParametersCheckBoxChecked(bool)));
106 }
107 
109 
110 Ui::RasterInfoWidgetForm* te::qt::widgets::RasterInfoWidget::getForm() const
111 {
112  return m_ui.get();
113 }
114 
116 {
117  return "GDAL";
118 }
119 
120 std::map<std::string, std::string> te::qt::widgets::RasterInfoWidget::getInfo() const
121 {
122  std::map<std::string, std::string> rinfo;
123 
124  rinfo["URI"] = m_ui->m_fileNameLineEdit->text().toUtf8().data();
125 
126  //get extra parameters
127 
128  std::map<std::string, std::string> extra = m_table->getMap();
129 
130  rinfo.insert(extra.begin(), extra.end());
131 
132  return rinfo;
133 }
134 
135 std::map<std::string, std::string> te::qt::widgets::RasterInfoWidget::getInfo(int count, std::string name) const
136 {
137  std::map<std::string, std::string> rinfo;
138 
139  boost::filesystem::path path( m_ui->m_fileNameLineEdit->text().toUtf8().data() );
140 
141  //std::string name = getBaseName();
142 
143  std::string fileName =
144  path.parent_path().string()
145  + std::string( "/" )
146  + path.stem().string()
147  + std::string( "_" );
148 
149  if(name != "")
150  {
151  fileName =
152  fileName
153  + name
154  + std::string( "_" );
155  }
156 
157  fileName =
158  fileName
159  + boost::lexical_cast< std::string >( count )
160  + path.extension().string();
161 
162  rinfo["URI"] = fileName;
163 
164  //get extra parameters
165 
166  std::map<std::string, std::string> extra = m_table->getMap();
167 
168  rinfo.insert(extra.begin(), extra.end());
169 
170  return rinfo;
171 }
172 
173 std::unique_ptr<te::da::DataSource> te::qt::widgets::RasterInfoWidget::getDataSource() const
174 {
175  boost::filesystem::path path(m_ui->m_fileNameLineEdit->text().toUtf8().data());
176 
177  const std::string connInfoRaster("file://" + path.parent_path().string());
178 
179  std::unique_ptr< te::da::DataSource > dsPtr(te::da::DataSourceFactory::make("GDAL", connInfoRaster));
180  dsPtr->open();
181 
182  return dsPtr;
183 }
184 
186 {
187  boost::filesystem::path path( m_ui->m_fileNameLineEdit->text().toUtf8().data() );
188  return path.filename().string();
189 }
190 
192 {
193  boost::filesystem::path path( m_ui->m_fileNameLineEdit->text().toUtf8().data() );
194  std::string name = path.filename().string();
195 
196  std::size_t pos = name.find_last_of(".");
197  if(pos != std::string::npos)
198  {
199  name = name.substr(0, pos);
200  }
201 
202  return name;
203 }
204 
206 {
207  return m_ui->m_fileNameLineEdit->text().toUtf8().data();
208 }
209 
211 {
212  boost::filesystem::path path( m_ui->m_fileNameLineEdit->text().toUtf8().data() );
213  return path.extension().string();
214 }
215 
217 {
218  boost::filesystem::path path( m_ui->m_fileNameLineEdit->text().toUtf8().data() );
219  return path.parent_path().string();
220 }
221 
223 {
224  boost::filesystem::path path( m_ui->m_fileNameLineEdit->text().toUtf8().data() );
225  return te::core::FileSystem::exists( path.string() );
226 }
227 
229 {
230  m_ui->m_rasterPropertiesFrame->setVisible(false);
231 }
232 
234 {
235  m_ui->m_nameLineEdit->clear();
236  m_ui->m_fileNameLineEdit->clear();
237  m_originalFullFileName.clear();
238 
239  // Acquiring the initial full file name
240 
241  if( m_outputMode )
242  {
243  te::qt::widgets::FileDialog fileDialog(this, te::qt::widgets::FileDialog::RASTER, "rp_raster_info");
244 
245  try {
246  fileDialog.exec();
247  }
248  catch (te::common::Exception& ex) {
249  QMessageBox::warning(this, tr("File information"), ex.what());
250  return;
251  }
252 
253  m_originalFullFileName = fileDialog.getPath();
254  }
255  else
256  {
257  m_originalFullFileName = QFileDialog::getOpenFileName(this, tr("Select File"),
258  te::qt::widgets::GetFilePathFromSettings("rp_raster_info"),
259  te::qt::widgets::GetDiskRasterFileSelFilter( false ), nullptr ,QFileDialog::ReadOnly).toUtf8().data();
260  }
261 
262  // Configure the interface
263  if( ! m_originalFullFileName.empty() )
264  {
265  boost::filesystem::path fullFilePath( m_originalFullFileName );
266 
267  m_ui->m_fileNameLineEdit->setText(m_originalFullFileName.c_str());
268  m_ui->m_nameLineEdit->setText(fullFilePath.stem().string().c_str());
269 
270  te::qt::widgets::AddFilePathToSettings(fullFilePath.parent_path().string().c_str(),
271  "rp_raster_info");
272  }
273 
275 }
276 
278 {
279  m_ui->m_sridLineEdit->clear();
280 
281  te::qt::widgets::SRSManagerDialog diag( this, nullptr );
282  diag.exec();
283 
284  if(
285  ( diag.getSelectedSRS().first != 0 )
286  &&
287  ( ! diag.getSelectedSRS().second.empty() )
288  )
289  {
290  m_ui->m_sridLineEdit->setText( QString::number( diag.getSelectedSRS().first ) );
291  }
292 }
293 
295 {
297 }
298 
300 {
301  if( state == Qt::Unchecked )
302  {
303  m_ui->m_fileNameLineEdit->setText( m_originalFullFileName.c_str() );
304  }
305  else
306  {
308  }
309 }
310 
312 {
313  m_table->setVisible(checked);
314 }
315 
317 {
318  // Is this a RAW image file ?
319 
320  if(
321  ( ! m_originalFullFileName.empty() )
322  &&
323  m_ui->m_forceRAWRastercheckBox->isChecked()
324  )
325  {
326  boost::filesystem::path fullFilePath( m_originalFullFileName );
327 
328  std::string upCaseExtension = te::common::Convert2UCase( fullFilePath.extension().string() );
329 
330  if( upCaseExtension != ".VRT" )
331  {
332  // Create a new vrt file and point the file name to it.
333 
334  std::string vrtFullFileName = fullFilePath.string();
335  std::size_t pos = vrtFullFileName.find_last_of(".");
336  if(pos != std::string::npos)
337  {
338  vrtFullFileName = vrtFullFileName.substr(0, pos);
339  }
340  vrtFullFileName = vrtFullFileName + ".vrt";
341 
342  boost::filesystem::path fullVrtPath( vrtFullFileName );
343 
344  te::core::FileSystem::remove( vrtFullFileName );
345 
346  if( !te::core::FileSystem::isRegularFile( fullVrtPath.string() ) )
347  {
348  const unsigned int bandsNumber = m_ui->m_bandsLineEdit->text().toUInt();
349  const unsigned int colsNumber = m_ui->m_columnsLineEdit->text().toUInt();
350  const unsigned int rowsNumber = m_ui->m_rowsLineEdit->text().toUInt();
351  const double resX = m_ui->m_resolutionXLineEdit->text().toDouble();
352  const double resY = m_ui->m_resolutionYLineEdit->text().toDouble();
353  const double upperLeftX = m_ui->m_upperLeftXLineEdit->text().toDouble();
354  const double upperLeftY = m_ui->m_upperLeftYLineEdit->text().toDouble();
355  const int SRID = m_ui->m_sridLineEdit->text().toInt();
356  const std::string byteOrder = m_ui->m_byteOrderComboBox->currentText().toUtf8().data();
357  const std::string dataTypeString = m_ui->m_dataTypeComboBox->currentText().toUtf8().data();
358  const std::string interleaveString = m_ui->m_interleaveComboBox->currentText().toUtf8().data();
359 
360  unsigned int dataTypeSizeBytes = 0;
361  if( dataTypeString == "Byte" )
362  {
363  dataTypeSizeBytes = 1;
364  }
365  else if( dataTypeString == "UInt16" )
366  {
367  dataTypeSizeBytes = 2;
368  }
369  else if( dataTypeString == "Int16" )
370  {
371  dataTypeSizeBytes = 2;
372  }
373  else if( dataTypeString == "UInt32" )
374  {
375  dataTypeSizeBytes = 4;
376  }
377  else if( dataTypeString == "Int32" )
378  {
379  dataTypeSizeBytes = 4;
380  }
381  else if( dataTypeString == "Float32" )
382  {
383  dataTypeSizeBytes = 4;
384  }
385  else if( dataTypeString == "Float64" )
386  {
387  dataTypeSizeBytes = 8;
388  }
389  else if( dataTypeString == "CInt16" )
390  {
391  dataTypeSizeBytes = 4;
392  }
393  else if( dataTypeString == "CInt32" )
394  {
395  dataTypeSizeBytes = 8;
396  }
397  else if( dataTypeString == "CFloat32" )
398  {
399  dataTypeSizeBytes = 8;
400  }
401  else if( dataTypeString == "CFloat64" )
402  {
403  dataTypeSizeBytes = 16;
404  }
405  else
406  {
407  throw te::qt::widgets::Exception( "Invalid data type" );
408  }
409 
410  std::ofstream vrtfile;
411  vrtfile.open ( vrtFullFileName.c_str() );
412 
413  if( !vrtfile.is_open() )
414  {
415  QMessageBox::critical( this, "Error", "Unable to create a VRT file" );
416  return;
417  }
418  else
419  {
420  vrtfile.precision( 20 );
421 
422  vrtfile
423  << "<VRTDataset rasterXSize=\"" << colsNumber << "\""
424  << " rasterYSize=\"" << rowsNumber << "\""
425  << ">";
426 
427  if( SRID > 0 )
428  {
429  const std::string wktStr = te::srs::SpatialReferenceSystemManager::getInstance().getWkt( SRID );
430 
431  if( ! wktStr.empty() )
432  {
433  vrtfile << std::endl << "<SRS>" << wktStr << "</SRS>";
434  }
435  }
436 
437  if( ( resX != 0.0 ) && ( resY != 0.0 ) )
438  {
439  vrtfile << std::endl << "<GeoTransform>" << upperLeftX << ","
440  << resX << ",0," << upperLeftY << ",0," << (-1.0 * resY)
441  << "</GeoTransform>";
442  }
443 
444  unsigned int pixelOffset = 0;
445  unsigned int lineOffset = 0;
446  unsigned int imageOffset = 0;
447 
448  for( unsigned int bandIdx = 0 ; bandIdx < bandsNumber ; ++bandIdx )
449  {
450 
451  if( interleaveString == "BIP" ) // Band interleaved by pixel
452  {
453  pixelOffset = dataTypeSizeBytes * bandsNumber;
454  lineOffset = pixelOffset * colsNumber;
455  imageOffset = bandIdx * dataTypeSizeBytes;
456  }
457  else
458  {
459  // BSQ (Band sequential) interleave
460 
461  pixelOffset = dataTypeSizeBytes;
462  lineOffset = pixelOffset * colsNumber;
463  imageOffset = bandIdx * lineOffset * rowsNumber;
464  }
465 
466  vrtfile
467  << std::endl
468  << "<VRTRasterBand dataType=\"" << dataTypeString << "\" band=\"" << ( bandIdx + 1 )<< "\" subClass=\"VRTRawRasterBand\">" << std::endl
469  << "<SourceFilename relativetoVRT=\"1\">" << fullFilePath.filename().string() << "</SourceFilename>" << std::endl
470  << "<ImageOffset>" << imageOffset << "</ImageOffset>" << std::endl
471  << "<PixelOffset>" << pixelOffset << "</PixelOffset>" << std::endl
472  << "<LineOffset>" << lineOffset << "</LineOffset>";
473 
474  if( !byteOrder.empty() ) vrtfile << std::endl << "<ByteOrder>" << byteOrder << "</ByteOrder>" << std::endl;
475 
476  vrtfile << std::endl << "</VRTRasterBand>";
477  }
478 
479  vrtfile << std::endl << "</VRTDataset>" << std::endl;
480 
481  m_ui->m_fileNameLineEdit->setText( vrtFullFileName.c_str() );
482  }
483  }
484 
485 
486  }
487  }
488 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Defines a component for choose a file.
Definition: FileDialog.h:52
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
std::string m_originalFullFileName
The selected raster file full file name.
TEQTWIDGETSEXPORT QString GetDiskRasterFileSelFilter(const bool creationSupport)
Returns a disk raster file selection filter base on current supported formats.
virtual const char * what() const
It outputs the exception message.
std::string getFullName() const
Returns the raster full name.
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
std::string getExtension() const
Returns the raster name extension.
std::unique_ptr< te::da::DataSource > getDataSource() const
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
Ui::RasterInfoWidgetForm * getForm() const
std::string getName() const
Returns the raster name.
RasterInfoWidget(const bool outputMode, QWidget *parent, Qt::WindowFlags f)
Default constructor.
std::map< std::string, std::string > getInfo() const
static bool remove(const std::string &path)
Removes a file or directory from a given path in UTF-8.
Definition: FileSystem.cpp:166
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
void exec()
This method will open the dialog of file selection and populate the class members with the chosen fil...
Definition: FileDialog.cpp:54
std::unique_ptr< Ui::RasterInfoWidgetForm > m_ui
This file has the RasterInfoWidget class.
std::string getPath() const
Returns the raster directory.
void onExtraParametersCheckBoxChecked(bool checked)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::string getPath()
This method will return the chosen path.
Definition: FileDialog.cpp:103
bool m_outputMode
Allows select input rasters (outputMode=false - rasters that already exists) or output raster info - ...
void rawRasterInfoChanged(const QString &text)
std::string getShortName() const
Returns the raster short name.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
std::unique_ptr< te::qt::widgets::ParameterTableWidget > m_table
A dialog used to build a SRSManagerDialog element.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
const std::pair< int, std::string > & getSelectedSRS() const
Returns the selected SRS in the window.