Skip to content
Permalink
Browse files

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
mhugent committed Aug 28, 2009
1 parent 11119f3 commit b4cc8f715006d2ca422823fc01f2699845e4125d
@@ -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 )
@@ -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.
@@ -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"
@@ -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 )
{
@@ -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 )
{
@@ -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;

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

@@ -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 );
}
@@ -19,6 +19,7 @@
#define QGSINTERPOLATIONDIALOG_H

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

@@ -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*/
@@ -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.
You can’t perform that action at this time.