Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Better options to specify output raster extent and resolution in inte…
…rpolation plugin

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11523 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Aug 28, 2009
1 parent 11119f3 commit b4cc8f7
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/plugins/interpolation/qgsgridfilewriter.cpp
Expand Up @@ -20,10 +20,10 @@
#include <QFile>
#include <QProgressDialog>

QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows ): mInterpolator( i ), mOutputFilePath( outputPath ), mInterpolationExtent( extent ), mNumColumns( nCols ), mNumRows( nRows )
QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows , double cellSizeX, double cellSizeY ): \
mInterpolator( i ), mOutputFilePath( outputPath ), mInterpolationExtent( extent ), mNumColumns( nCols ), mNumRows( nRows ), mCellSizeX( cellSizeX ), mCellSizeY( cellSizeY )
{
mCellSizeX = ( mInterpolationExtent.xMaximum() - mInterpolationExtent.xMinimum() ) / mNumColumns;
mCellSizeY = ( mInterpolationExtent.yMaximum() - mInterpolationExtent.yMinimum() ) / mNumRows;

}

QgsGridFileWriter::QgsGridFileWriter(): mInterpolator( 0 )
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/interpolation/qgsgridfilewriter.h
Expand Up @@ -29,7 +29,7 @@ class QgsInterpolator;
class QgsGridFileWriter
{
public:
QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows );
QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows, double cellSizeX, double cellSizeY );
~QgsGridFileWriter();

/**Writes the grid file.
Expand Down
240 changes: 227 additions & 13 deletions src/plugins/interpolation/qgsinterpolationdialog.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsgridfilewriter.h"
#include "qgsidwinterpolatordialog.h"
#include "qgstininterpolatordialog.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
Expand Down Expand Up @@ -93,6 +94,12 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
return;
}

QgsRectangle outputBBox = currentBoundingBox();
if ( outputBBox.isEmpty() )
{
return;
}

//warn the user if there isn't any input layer
if ( mLayersTreeWidget->topLevelItemCount() < 1 )
{
Expand All @@ -111,7 +118,6 @@ void QgsInterpolationDialog::on_buttonBox_accepted()

int nLayers = mLayersTreeWidget->topLevelItemCount();
QList< QgsInterpolator::LayerData > inputLayerList;
QgsRectangle combinedLayerExtent;

for ( int i = 0; i < nLayers; ++i )
{
Expand All @@ -128,17 +134,6 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
continue;
}

//update extent
QgsRectangle currentLayerExtent = theVectorLayer->extent();
if ( combinedLayerExtent.isEmpty() )
{
combinedLayerExtent = currentLayerExtent;
}
else
{
combinedLayerExtent.combineExtentWith( &currentLayerExtent );
}

QgsInterpolator::LayerData currentLayerData;
currentLayerData.vectorLayer = theVectorLayer;

Expand Down Expand Up @@ -189,7 +184,8 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
}

//create grid file writer
QgsGridFileWriter theWriter( theInterpolator, fileName, combinedLayerExtent, mNumberOfColumnsSpinBox->value(), mNumberOfRowsSpinBox->value() );
QgsGridFileWriter theWriter( theInterpolator, fileName, outputBBox, mNumberOfColumnsSpinBox->value(), \
mNumberOfRowsSpinBox->value(), mCellsizeXSpinBox->value(), mCellSizeYSpinBox->value() );
if ( theWriter.writeFile( true ) == 0 )
{
mIface->addRasterLayer( fileName, "Interpolation" );
Expand Down Expand Up @@ -273,6 +269,9 @@ void QgsInterpolationDialog::on_mAddPushButton_clicked()
typeComboBox->setCurrentIndex( 0 );
mLayersTreeWidget->setItemWidget( newLayerItem, 2, typeComboBox );

//keep bounding box up to date
setLayersBoundingBox();

enableOrDisableOkButton();
}

Expand Down Expand Up @@ -345,3 +344,218 @@ void QgsInterpolationDialog::on_mInterpolationMethodComboBox_currentIndexChanged
mInterpolatorDialog = new QgsTINInterpolatorDialog( 0, mIface );
}
}

void QgsInterpolationDialog::on_mNumberOfColumnsSpinBox_valueChanged( int value )
{
setNewCellsizeXOnNColumnsChange();
}

void QgsInterpolationDialog::on_mNumberOfRowsSpinBox_valueChanged( int value )
{
setNewCellsizeYOnNRowschange();
}

void QgsInterpolationDialog::on_mCellsizeXSpinBox_valueChanged( double value )
{
setNColsOnCellsizeXChange();
}

void QgsInterpolationDialog::on_mCellSizeYSpinBox_valueChanged( double value )
{
setNRowsOnCellsizeYChange();
}

void QgsInterpolationDialog::on_mXMinLineEdit_textEdited( const QString& text )
{
setNewCellsizeOnBoundingBoxChange();
}

void QgsInterpolationDialog::on_mXMaxLineEdit_textEdited( const QString& text )
{
setNewCellsizeOnBoundingBoxChange();
}

void QgsInterpolationDialog::on_mYMinLineEdit_textEdited( const QString& text )
{
setNewCellsizeOnBoundingBoxChange();
}

void QgsInterpolationDialog::on_mYMaxLineEdit_textEdited( const QString& text )
{
setNewCellsizeOnBoundingBoxChange();
}

void QgsInterpolationDialog::on_mBBoxToCurrentExtent_clicked()
{
if ( mIface )
{
QgsMapCanvas* canvas = mIface->mapCanvas();
if ( canvas )
{
QgsRectangle extent = canvas->extent();
mXMinLineEdit->setText( QString::number( extent.xMinimum() ) );
mXMaxLineEdit->setText( QString::number( extent.xMaximum() ) );
mYMinLineEdit->setText( QString::number( extent.yMinimum() ) );
mYMaxLineEdit->setText( QString::number( extent.yMaximum() ) );
}
}
}

QgsRectangle QgsInterpolationDialog::boundingBoxOfLayers()
{
int nLayers = mLayersTreeWidget->topLevelItemCount();
QList< QgsInterpolator::LayerData > inputLayerList;
QgsRectangle combinedLayerExtent;

for ( int i = 0; i < nLayers; ++i )
{
QString layerName = mLayersTreeWidget->topLevelItem( i )->text( 0 );
QgsVectorLayer* theVectorLayer = vectorLayerFromName( layerName );
if ( !theVectorLayer )
{
continue;
}

QgsVectorDataProvider* theProvider = theVectorLayer->dataProvider();
if ( !theProvider )
{
continue;
}

//update extent
QgsRectangle currentLayerExtent = theVectorLayer->extent();
if ( combinedLayerExtent.isEmpty() )
{
combinedLayerExtent = currentLayerExtent;
}
else
{
combinedLayerExtent.combineExtentWith( &currentLayerExtent );
}
}
return combinedLayerExtent;
}

void QgsInterpolationDialog::setLayersBoundingBox()
{
QgsRectangle layersBoundingBox = boundingBoxOfLayers();
mXMinLineEdit->setText( QString::number( layersBoundingBox.xMinimum() ) );
mXMaxLineEdit->setText( QString::number( layersBoundingBox.xMaximum() ) );
mYMinLineEdit->setText( QString::number( layersBoundingBox.yMinimum() ) );
mYMaxLineEdit->setText( QString::number( layersBoundingBox.yMaximum() ) );
setNewCellsizeOnBoundingBoxChange();
}

void QgsInterpolationDialog::setNewCellsizeOnBoundingBoxChange()
{
QgsRectangle currentBbox = currentBoundingBox();
if ( currentBbox.isEmpty() )
{
return;
}

if ( currentBbox.width() > 0 && mNumberOfColumnsSpinBox->value() > 0 )
{
mCellsizeXSpinBox->blockSignals( true );
mCellsizeXSpinBox->setValue( currentBbox.width() / mNumberOfColumnsSpinBox->value() );
mCellsizeXSpinBox->blockSignals( false );
}
if ( currentBbox.height() > 0 && mNumberOfRowsSpinBox->value() > 0 )
{
mCellSizeYSpinBox->blockSignals( true );
mCellSizeYSpinBox->setValue( currentBbox.height() / mNumberOfRowsSpinBox->value() );
mCellSizeYSpinBox->blockSignals( false );
}
}

void QgsInterpolationDialog::setNewCellsizeXOnNColumnsChange()
{
QgsRectangle currentBBox = currentBoundingBox();
if ( !currentBBox.isEmpty() && mNumberOfColumnsSpinBox->value() > 0 )
{
mCellsizeXSpinBox->blockSignals( true );
mCellsizeXSpinBox->setValue( currentBBox.width() / mNumberOfColumnsSpinBox->value() );
mCellsizeXSpinBox->blockSignals( false );
}
}

void QgsInterpolationDialog::setNewCellsizeYOnNRowschange()
{
QgsRectangle currentBBox = currentBoundingBox();
if ( !currentBBox.isEmpty() && mNumberOfRowsSpinBox->value() > 0 )
{
mCellSizeYSpinBox->blockSignals( true );
mCellSizeYSpinBox->setValue( currentBBox.height() / mNumberOfRowsSpinBox->value() );
mCellSizeYSpinBox->blockSignals( false );
}
}

void QgsInterpolationDialog::setNColsOnCellsizeXChange()
{
QgsRectangle currentBBox = currentBoundingBox();
int newSize;

if ( !mCellsizeXSpinBox->value() > 0 )
{
return;
}

if ( !currentBBox.width() > 0 )
{
newSize = 0;
}
else
{
newSize = ( int )( currentBBox.width() / mCellsizeXSpinBox->value() );
}

mNumberOfColumnsSpinBox->blockSignals( true );
mNumberOfColumnsSpinBox->setValue( newSize );
mNumberOfColumnsSpinBox->blockSignals( false );
}

void QgsInterpolationDialog::setNRowsOnCellsizeYChange()
{
QgsRectangle currentBBox = currentBoundingBox();
int newSize;

if ( !mCellSizeYSpinBox->value() > 0 )
{
return;
}

if ( !currentBBox.height() > 0 )
{
newSize = 0;
}
else
{
newSize = ( int )( currentBBox.height() / mCellSizeYSpinBox->value() );
}

mNumberOfRowsSpinBox->blockSignals( true );
mNumberOfRowsSpinBox->setValue( newSize );
mNumberOfRowsSpinBox->blockSignals( false );
}

QgsRectangle QgsInterpolationDialog::currentBoundingBox()
{
QString xMinString = mXMinLineEdit->text();
QString xMaxString = mXMaxLineEdit->text();
QString yMinString = mYMinLineEdit->text();
QString yMaxString = mYMaxLineEdit->text();

bool xMinOk, xMaxOk, yMinOk, yMaxOk;
double xMin = xMinString.toDouble( &xMinOk );
double xMax = xMaxString.toDouble( &xMaxOk );
double yMin = yMinString.toDouble( &yMinOk );
double yMax = yMaxString.toDouble( &yMaxOk );

if ( !xMinOk || !xMaxOk || !yMinOk || !yMaxOk )
{
QgsRectangle emptyBbox;
return emptyBbox; //error, return empty bounding box
}

return QgsRectangle( xMin, yMin, xMax, yMax );
}
27 changes: 27 additions & 0 deletions src/plugins/interpolation/qgsinterpolationdialog.h
Expand Up @@ -19,6 +19,7 @@
#define QGSINTERPOLATIONDIALOG_H

#include "ui_qgsinterpolationdialogbase.h"
#include "qgsrectangle.h"
#include "qgisinterface.h"
#include <QFileInfo>

Expand All @@ -42,6 +43,19 @@ class QgsInterpolationDialog: public QDialog, private Ui::QgsInterpolationDialog
void on_mAddPushButton_clicked();
void on_mRemovePushButton_clicked();

void on_mNumberOfColumnsSpinBox_valueChanged( int value );
void on_mNumberOfRowsSpinBox_valueChanged( int value );
void on_mCellsizeXSpinBox_valueChanged( double value );
void on_mCellSizeYSpinBox_valueChanged( double value );
void on_mBBoxToCurrentExtent_clicked();

void on_mXMinLineEdit_textEdited( const QString& text );
void on_mXMaxLineEdit_textEdited( const QString& text );
void on_mYMinLineEdit_textEdited( const QString& text );
void on_mYMaxLineEdit_textEdited( const QString& text );



private:
QgisInterface* mIface;
/**Dialog to get input for the current interpolation method*/
Expand All @@ -52,6 +66,19 @@ class QgsInterpolationDialog: public QDialog, private Ui::QgsInterpolationDialog
QgsVectorLayer* vectorLayerFromName( const QString& name );
/**Enables or disables the Ok button depending on the availability of input layers and the output file*/
void enableOrDisableOkButton();
/**Get the current output bounding box (might be different to the compound layers bounding box because of user edits)
@return the bounding box or an empty bounding box in case of error*/
QgsRectangle currentBoundingBox();
/**Returns the compound bounding box of the inserted layers*/
QgsRectangle boundingBoxOfLayers();
/**Inserts the compound bounding box of the input layers into the line edits for the output bounding box*/
void setLayersBoundingBox();
/**Set cellsizes according to nex bounding box and number of columns / rows */
void setNewCellsizeOnBoundingBoxChange();
void setNewCellsizeXOnNColumnsChange();
void setNewCellsizeYOnNRowschange();
void setNColsOnCellsizeXChange();
void setNRowsOnCellsizeYChange();
};

#endif

0 comments on commit b4cc8f7

Please sign in to comment.