Skip to content
Permalink
Browse files

Try to do automatic choice of the reference layer in GUI (using small…

…est cell area)
  • Loading branch information
wonder-sk committed Jun 25, 2015
1 parent fcc0160 commit 11d1ac95408783f0c160956bdfa8be40cf250430
@@ -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;

};

@@ -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 )
{
@@ -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)
@@ -112,21 +112,31 @@ 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 );

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();
}

@@ -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 )

0 comments on commit 11d1ac9

Please sign in to comment.
You can’t perform that action at this time.