23 #include "../../../dataaccess/utils/Utils.h" 24 #include "../../../rp/Functions.h" 25 #include "../../../common/StringUtils.h" 26 #include "../../../raster/Band.h" 27 #include "../../../raster/BandProperty.h" 28 #include "../../../raster/RasterFactory.h" 29 #include "../../../raster/Grid.h" 30 #include "../help/HelpPushButton.h" 31 #include "../layer/search/LayerSearchWidget.h" 32 #include "../layer/search/LayerSearchWizardPage.h" 33 #include "../progress/ProgressViewerDialog.h" 34 #include "../utils/ScopedCursor.h" 36 #include "../raster/RasterInfoWidget.h" 40 #include <boost/lexical_cast.hpp> 43 #include <QApplication> 44 #include <QMessageBox> 53 this->setWizardStyle(QWizard::ModernStyle);
54 this->setWindowTitle(tr(
"Raster Slicing"));
56 this->setOption(QWizard::HaveHelpButton,
true);
57 this->setOption(QWizard::HelpButtonOnRight,
false);
61 this->setButton(QWizard::HelpButton, helpButton);
65 connect(
this, SIGNAL(currentIdChanged(
int)), SLOT(
onPageChanged(
int)));
77 std::list<te::map::AbstractLayerPtr> list =
m_layerSearchPage->getSearchWidget()->getSelecteds();
79 if(list.empty() ==
false)
83 if( layerPtr.get() != nullptr )
136 QMessageBox::warning(
this, tr(
"PCA"), tr(
"File already exists."));
142 std::list<te::map::AbstractLayerPtr> list =
m_layerSearchPage->getSearchWidget()->getSelecteds();
147 std::unique_ptr<te::rst::Raster> inputRst;
151 std::unique_ptr<te::da::DataSet>
ds = l->getData();
153 if (ds.get() == nullptr )
155 QMessageBox::critical(
this, tr(
"PCA"), tr(
"Invalid layer.") );
161 inputRst = ds->getRaster(rpos);
164 const std::vector< unsigned int > inputRasterBands =
m_wizardPage->getSelectedBands();
166 if( inputRasterBands.empty() )
168 QMessageBox::critical(
this, tr(
"PCA"), tr(
"No selected bands.") );
174 const std::string matrixFileName =
m_wizardPage->getMatrixFileName();
176 if(
m_wizardPage->inversePCASelected() && matrixFileName.empty() )
178 QMessageBox::critical(
this, tr(
"PCA"), tr(
"Invalid matrix file name.") );
182 std::fstream matrixFile;
183 boost::numeric::ublas::matrix< double > matrix;
187 matrixFile.open( matrixFileName.c_str(), std::fstream::in );
188 if( !matrixFile.good() )
190 QMessageBox::critical(
this, tr(
"PCA"), tr(
"Invalid matrix file.") );
194 matrixFile.seekg( 0 );
197 std::getline( matrixFile, lineStr );
199 std::vector< std::string > tokens;
202 const std::size_t matrixSize = (std::size_t)std::sqrt( tokens.size() );
203 std::size_t tokensIdx = 0;
205 matrix.resize( matrixSize, matrixSize );
207 for( std::size_t row = 0 ; row < matrixSize ; ++row )
209 for( std::size_t
col = 0 ;
col < matrixSize ; ++
col )
211 if( tokensIdx >= tokens.size() )
213 QMessageBox::critical(
this, tr(
"PCA"), tr(
"Invalid matrix file.") );
217 matrix( row,
col ) = boost::lexical_cast<
double >( tokens[ tokensIdx ] );
224 if( ! matrixFileName.empty() )
226 matrixFile.open( matrixFileName.c_str(), std::fstream::out | std::fstream::trunc );
228 if( !matrixFile.good() )
230 QMessageBox::critical(
this, tr(
"PCA"), tr(
"Invalid matrix file.") );
238 std::unique_ptr<te::rst::Raster> outputRasterPtr;
239 std::vector< unsigned int > outputRasterBands;
242 std::vector< te::rst::BandProperty * > bandProps;
244 for(
unsigned int bandIdx = 0 ; bandIdx < inputRasterBands.size() ;
248 *( inputRst->getBand( inputRasterBands[ bandIdx ] )->getProperty() ) ) );
249 bandProps.back()->m_type =
m_wizardPage->getOutputRasterDataType();
250 bandProps.back()->m_blkh = bandProps.front()->m_blkh;
251 bandProps.back()->m_blkw = bandProps.front()->m_blkw;
252 bandProps.back()->m_nblocksx = bandProps.front()->m_nblocksx;
253 bandProps.back()->m_nblocksy = bandProps.front()->m_nblocksy;
255 outputRasterBands.push_back( bandIdx );
267 outputRasterPtr.reset();
270 if( outputRasterPtr.get() == nullptr )
272 QMessageBox::critical(
this, tr(
"PCA"), tr(
"Output raster creation error.") );
279 QApplication::setOverrideCursor(Qt::WaitCursor);
286 outputRasterBands, 0 ) )
288 QMessageBox::critical(
this, tr(
"PCA"), tr(
"PCA processing error.") );
290 QApplication::restoreOverrideCursor();
300 *outputRasterPtr, outputRasterBands, 0 ) )
302 QMessageBox::critical(
this, tr(
"PCA"), tr(
"PCA processing error.") );
304 QApplication::restoreOverrideCursor();
309 for( std::size_t row = 0 ; row < matrix.size1() ; ++row )
311 for( std::size_t
col = 0 ;
col < matrix.size2() ; ++
col )
313 matrixFile << boost::lexical_cast< std::string >( matrix( row,
col ) );
319 QApplication::restoreOverrideCursor();
323 outputRasterPtr.reset();
328 QMessageBox::information(
this, tr(
"PCA"), tr(
"PCA ended sucessfully"));
A raster band description.
static te::dt::Date ds(2010, 01, 01)
bool DirectPrincipalComponents(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, boost::numeric::ublas::matrix< double > &pcaMatrix, te::rst::Raster &pcaRaster, const std::vector< unsigned int > &pcaRasterBands, const unsigned int maxThreads)
Generate all principal components from the given input raster.
This file defines a class for a Raster Info Wizard page.
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
This class is GUI used to define the raster info parameters for raster factory.
Utility functions for the data access module.
static Raster * make()
It creates and returns an empty raster with default raster driver.
bool InversePrincipalComponents(const te::rst::Raster &pcaRaster, const boost::numeric::ublas::matrix< double > &pcaMatrix, te::rst::Raster &outputRaster, const std::vector< unsigned int > &outputRasterBands, const unsigned int maxThreads)
Regenerate the original raster from its principal components.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr