Try to do automatic choice of the reference layer in GUI (using smallest cell area)

This commit is contained in:
Martin Dobias 2015-06-25 18:10:47 +08:00
parent fcc0160755
commit 11d1ac9540
5 changed files with 61 additions and 0 deletions

View File

@ -157,5 +157,8 @@ class QgsAlignRaster
//! write contents of the object to standard error stream - for debugging
void dump() const;
//! Return index of the layer which has smallest cell size (returns -1 on error)
int suggestedReferenceLayer() const;
};

View File

@ -23,6 +23,7 @@
#include <QPair>
#include <QString>
#include "qgscoordinatereferencesystem.h"
#include "qgsrectangle.h"
@ -386,6 +387,36 @@ void QgsAlignRaster::dump() const
qDebug( "extent %s", e.toString().toAscii().constData() );
}
int QgsAlignRaster::suggestedReferenceLayer() const
{
int bestIndex = -1;
double bestCellArea = qInf();
QSizeF cs;
int i = 0;
// using WGS84 as a destination CRS... but maybe some projected CRS
// would be a better a choice to more accurately compute areas?
// (Why earth is not flat???)
QgsCoordinateReferenceSystem destCRS( "EPSG:4326" );
QString destWkt = destCRS.toWkt();
foreach ( const Item& raster, mRasters )
{
if ( !suggestedWarpOutput( RasterInfo( raster.inputFilename ), destWkt, &cs ) )
return false;
double cellArea = cs.width() * cs.height();
if ( cellArea < bestCellArea )
{
bestCellArea = cellArea;
bestIndex = i;
}
++i;
}
return bestIndex;
}
bool QgsAlignRaster::createAndWarp( const Item& raster )
{

View File

@ -205,6 +205,9 @@ class ANALYSIS_EXPORT QgsAlignRaster
//! write contents of the object to standard error stream - for debugging
void dump() const;
//! Return index of the layer which has smallest cell size (returns -1 on error)
int suggestedReferenceLayer() const;
protected:
//! Internal function for processing of one raster (1. create output, 2. do the alignment)

View File

@ -112,7 +112,10 @@ void QgsAlignRasterDialog::populateLayersView()
{
mCboReferenceLayer->clear();
int refLayerIndex = mAlign->suggestedReferenceLayer();
QStandardItemModel* model = new QStandardItemModel();
int i = 0;
foreach ( QgsAlignRaster::Item item, mAlign->rasters() )
{
QString layerName = _rasterLayerName( item.inputFilename );
@ -120,13 +123,20 @@ void QgsAlignRasterDialog::populateLayersView()
QStandardItem* si = new QStandardItem( QgsLayerItem::iconRaster(), layerName );
model->appendRow( si );
if ( i == refLayerIndex )
layerName += tr( " [best reference]" );
mCboReferenceLayer->addItem( layerName );
++i;
}
mViewLayers->setModel( model );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( model->rowCount() > 0 );
if ( refLayerIndex >= 0 )
mCboReferenceLayer->setCurrentIndex( refLayerIndex );
updateAlignedRasterInfo();
}

View File

@ -252,6 +252,20 @@ class TestAlignRaster : public QObject
QVERIFY( !res );
}
void testSuggestedReferenceLayer()
{
QgsAlignRaster align;
QCOMPARE( align.suggestedReferenceLayer(), -1 );
QgsAlignRaster::List rasters;
rasters << QgsAlignRaster::Item( SRC_FILE, QString() );
align.setRasters( rasters );
QCOMPARE( align.suggestedReferenceLayer(), 0 );
}
};
QTEST_MAIN( TestAlignRaster )