Better options to specify output raster extent and resolution in interpolation plugin

git-svn-id: http://svn.osgeo.org/qgis/trunk@11523 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
mhugent 2009-08-28 20:32:23 +00:00
parent 4702b90a8e
commit d85624ae4e
5 changed files with 368 additions and 23 deletions

View File

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

View File

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

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

View File

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

View File

@ -6,7 +6,7 @@
<x>0</x>
<y>0</y>
<width>538</width>
<height>577</height>
<height>733</height>
</rect>
</property>
<property name="sizePolicy" >
@ -18,7 +18,7 @@
<property name="windowTitle" >
<string>Interpolation plugin</string>
</property>
<layout class="QGridLayout" name="gridLayout_2" >
<layout class="QGridLayout" name="gridLayout_3" >
<item row="0" column="0" >
<widget class="QGroupBox" name="mInputGroupBox" >
<property name="sizePolicy" >
@ -122,7 +122,7 @@
<property name="title" >
<string>Output</string>
</property>
<layout class="QGridLayout" >
<layout class="QGridLayout" name="gridLayout_2" >
<item row="0" column="0" >
<widget class="QLabel" name="mInterpolationLabel" >
<property name="text" >
@ -181,7 +181,111 @@
</property>
</widget>
</item>
<item row="3" column="0" >
<item row="3" column="0" colspan="3" >
<layout class="QHBoxLayout" name="horizontalLayout_3" >
<item>
<widget class="QLabel" name="mCellsizeXLabel" >
<property name="text" >
<string>Cellsize X:</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="mCellsizeXSpinBox" >
<property name="maximum" >
<double>999999.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="mCellsizeYLabel" >
<property name="text" >
<string>Cellsize Y:</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="mCellSizeYSpinBox" >
<property name="maximum" >
<double>999999.000000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0" colspan="3" >
<layout class="QHBoxLayout" name="horizontalLayout" >
<item>
<widget class="QLabel" name="mXMinLabel" >
<property name="text" >
<string>X Min:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mXMinLineEdit" />
</item>
<item>
<widget class="QLabel" name="mXMaxLabel" >
<property name="text" >
<string>X Max:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mXMaxLineEdit" />
</item>
</layout>
</item>
<item row="5" column="0" colspan="3" >
<layout class="QHBoxLayout" name="horizontalLayout_2" >
<item>
<widget class="QLabel" name="mYMinLabel" >
<property name="text" >
<string>Y Min:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mYMinLineEdit" />
</item>
<item>
<widget class="QLabel" name="mYMaxLabel" >
<property name="text" >
<string>Y Max:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mYMaxLineEdit" />
</item>
</layout>
</item>
<item row="6" column="0" colspan="3" >
<layout class="QHBoxLayout" name="horizontalLayout_4" >
<item>
<spacer name="horizontalSpacer" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0" >
<size>
<width>298</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="mBBoxToCurrentExtent" >
<property name="text" >
<string>Set to current extent</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="7" column="0" >
<widget class="QLabel" name="mOutputFileLabel" >
<property name="text" >
<string>Output file </string>
@ -191,10 +295,10 @@
</property>
</widget>
</item>
<item row="3" column="1" >
<item row="7" column="1" >
<widget class="QLineEdit" name="mOutputFileLineEdit" />
</item>
<item row="3" column="2" >
<item row="7" column="2" >
<widget class="QToolButton" name="mOutputFileButton" >
<property name="text" >
<string>...</string>