Skip to content

Commit b4cc8f7

Browse files
author
mhugent
committed
Better options to specify output raster extent and resolution in interpolation plugin
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11523 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 11119f3 commit b4cc8f7

File tree

5 files changed

+368
-23
lines changed

5 files changed

+368
-23
lines changed

src/plugins/interpolation/qgsgridfilewriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
#include <QFile>
2121
#include <QProgressDialog>
2222

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

2929
QgsGridFileWriter::QgsGridFileWriter(): mInterpolator( 0 )

src/plugins/interpolation/qgsgridfilewriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class QgsInterpolator;
2929
class QgsGridFileWriter
3030
{
3131
public:
32-
QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows );
32+
QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows, double cellSizeX, double cellSizeY );
3333
~QgsGridFileWriter();
3434

3535
/**Writes the grid file.

src/plugins/interpolation/qgsinterpolationdialog.cpp

Lines changed: 227 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsgridfilewriter.h"
2323
#include "qgsidwinterpolatordialog.h"
2424
#include "qgstininterpolatordialog.h"
25+
#include "qgsmapcanvas.h"
2526
#include "qgsmaplayerregistry.h"
2627
#include "qgsvectordataprovider.h"
2728
#include "qgsvectorlayer.h"
@@ -93,6 +94,12 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
9394
return;
9495
}
9596

97+
QgsRectangle outputBBox = currentBoundingBox();
98+
if ( outputBBox.isEmpty() )
99+
{
100+
return;
101+
}
102+
96103
//warn the user if there isn't any input layer
97104
if ( mLayersTreeWidget->topLevelItemCount() < 1 )
98105
{
@@ -111,7 +118,6 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
111118

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

116122
for ( int i = 0; i < nLayers; ++i )
117123
{
@@ -128,17 +134,6 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
128134
continue;
129135
}
130136

131-
//update extent
132-
QgsRectangle currentLayerExtent = theVectorLayer->extent();
133-
if ( combinedLayerExtent.isEmpty() )
134-
{
135-
combinedLayerExtent = currentLayerExtent;
136-
}
137-
else
138-
{
139-
combinedLayerExtent.combineExtentWith( &currentLayerExtent );
140-
}
141-
142137
QgsInterpolator::LayerData currentLayerData;
143138
currentLayerData.vectorLayer = theVectorLayer;
144139

@@ -189,7 +184,8 @@ void QgsInterpolationDialog::on_buttonBox_accepted()
189184
}
190185

191186
//create grid file writer
192-
QgsGridFileWriter theWriter( theInterpolator, fileName, combinedLayerExtent, mNumberOfColumnsSpinBox->value(), mNumberOfRowsSpinBox->value() );
187+
QgsGridFileWriter theWriter( theInterpolator, fileName, outputBBox, mNumberOfColumnsSpinBox->value(), \
188+
mNumberOfRowsSpinBox->value(), mCellsizeXSpinBox->value(), mCellSizeYSpinBox->value() );
193189
if ( theWriter.writeFile( true ) == 0 )
194190
{
195191
mIface->addRasterLayer( fileName, "Interpolation" );
@@ -273,6 +269,9 @@ void QgsInterpolationDialog::on_mAddPushButton_clicked()
273269
typeComboBox->setCurrentIndex( 0 );
274270
mLayersTreeWidget->setItemWidget( newLayerItem, 2, typeComboBox );
275271

272+
//keep bounding box up to date
273+
setLayersBoundingBox();
274+
276275
enableOrDisableOkButton();
277276
}
278277

@@ -345,3 +344,218 @@ void QgsInterpolationDialog::on_mInterpolationMethodComboBox_currentIndexChanged
345344
mInterpolatorDialog = new QgsTINInterpolatorDialog( 0, mIface );
346345
}
347346
}
347+
348+
void QgsInterpolationDialog::on_mNumberOfColumnsSpinBox_valueChanged( int value )
349+
{
350+
setNewCellsizeXOnNColumnsChange();
351+
}
352+
353+
void QgsInterpolationDialog::on_mNumberOfRowsSpinBox_valueChanged( int value )
354+
{
355+
setNewCellsizeYOnNRowschange();
356+
}
357+
358+
void QgsInterpolationDialog::on_mCellsizeXSpinBox_valueChanged( double value )
359+
{
360+
setNColsOnCellsizeXChange();
361+
}
362+
363+
void QgsInterpolationDialog::on_mCellSizeYSpinBox_valueChanged( double value )
364+
{
365+
setNRowsOnCellsizeYChange();
366+
}
367+
368+
void QgsInterpolationDialog::on_mXMinLineEdit_textEdited( const QString& text )
369+
{
370+
setNewCellsizeOnBoundingBoxChange();
371+
}
372+
373+
void QgsInterpolationDialog::on_mXMaxLineEdit_textEdited( const QString& text )
374+
{
375+
setNewCellsizeOnBoundingBoxChange();
376+
}
377+
378+
void QgsInterpolationDialog::on_mYMinLineEdit_textEdited( const QString& text )
379+
{
380+
setNewCellsizeOnBoundingBoxChange();
381+
}
382+
383+
void QgsInterpolationDialog::on_mYMaxLineEdit_textEdited( const QString& text )
384+
{
385+
setNewCellsizeOnBoundingBoxChange();
386+
}
387+
388+
void QgsInterpolationDialog::on_mBBoxToCurrentExtent_clicked()
389+
{
390+
if ( mIface )
391+
{
392+
QgsMapCanvas* canvas = mIface->mapCanvas();
393+
if ( canvas )
394+
{
395+
QgsRectangle extent = canvas->extent();
396+
mXMinLineEdit->setText( QString::number( extent.xMinimum() ) );
397+
mXMaxLineEdit->setText( QString::number( extent.xMaximum() ) );
398+
mYMinLineEdit->setText( QString::number( extent.yMinimum() ) );
399+
mYMaxLineEdit->setText( QString::number( extent.yMaximum() ) );
400+
}
401+
}
402+
}
403+
404+
QgsRectangle QgsInterpolationDialog::boundingBoxOfLayers()
405+
{
406+
int nLayers = mLayersTreeWidget->topLevelItemCount();
407+
QList< QgsInterpolator::LayerData > inputLayerList;
408+
QgsRectangle combinedLayerExtent;
409+
410+
for ( int i = 0; i < nLayers; ++i )
411+
{
412+
QString layerName = mLayersTreeWidget->topLevelItem( i )->text( 0 );
413+
QgsVectorLayer* theVectorLayer = vectorLayerFromName( layerName );
414+
if ( !theVectorLayer )
415+
{
416+
continue;
417+
}
418+
419+
QgsVectorDataProvider* theProvider = theVectorLayer->dataProvider();
420+
if ( !theProvider )
421+
{
422+
continue;
423+
}
424+
425+
//update extent
426+
QgsRectangle currentLayerExtent = theVectorLayer->extent();
427+
if ( combinedLayerExtent.isEmpty() )
428+
{
429+
combinedLayerExtent = currentLayerExtent;
430+
}
431+
else
432+
{
433+
combinedLayerExtent.combineExtentWith( &currentLayerExtent );
434+
}
435+
}
436+
return combinedLayerExtent;
437+
}
438+
439+
void QgsInterpolationDialog::setLayersBoundingBox()
440+
{
441+
QgsRectangle layersBoundingBox = boundingBoxOfLayers();
442+
mXMinLineEdit->setText( QString::number( layersBoundingBox.xMinimum() ) );
443+
mXMaxLineEdit->setText( QString::number( layersBoundingBox.xMaximum() ) );
444+
mYMinLineEdit->setText( QString::number( layersBoundingBox.yMinimum() ) );
445+
mYMaxLineEdit->setText( QString::number( layersBoundingBox.yMaximum() ) );
446+
setNewCellsizeOnBoundingBoxChange();
447+
}
448+
449+
void QgsInterpolationDialog::setNewCellsizeOnBoundingBoxChange()
450+
{
451+
QgsRectangle currentBbox = currentBoundingBox();
452+
if ( currentBbox.isEmpty() )
453+
{
454+
return;
455+
}
456+
457+
if ( currentBbox.width() > 0 && mNumberOfColumnsSpinBox->value() > 0 )
458+
{
459+
mCellsizeXSpinBox->blockSignals( true );
460+
mCellsizeXSpinBox->setValue( currentBbox.width() / mNumberOfColumnsSpinBox->value() );
461+
mCellsizeXSpinBox->blockSignals( false );
462+
}
463+
if ( currentBbox.height() > 0 && mNumberOfRowsSpinBox->value() > 0 )
464+
{
465+
mCellSizeYSpinBox->blockSignals( true );
466+
mCellSizeYSpinBox->setValue( currentBbox.height() / mNumberOfRowsSpinBox->value() );
467+
mCellSizeYSpinBox->blockSignals( false );
468+
}
469+
}
470+
471+
void QgsInterpolationDialog::setNewCellsizeXOnNColumnsChange()
472+
{
473+
QgsRectangle currentBBox = currentBoundingBox();
474+
if ( !currentBBox.isEmpty() && mNumberOfColumnsSpinBox->value() > 0 )
475+
{
476+
mCellsizeXSpinBox->blockSignals( true );
477+
mCellsizeXSpinBox->setValue( currentBBox.width() / mNumberOfColumnsSpinBox->value() );
478+
mCellsizeXSpinBox->blockSignals( false );
479+
}
480+
}
481+
482+
void QgsInterpolationDialog::setNewCellsizeYOnNRowschange()
483+
{
484+
QgsRectangle currentBBox = currentBoundingBox();
485+
if ( !currentBBox.isEmpty() && mNumberOfRowsSpinBox->value() > 0 )
486+
{
487+
mCellSizeYSpinBox->blockSignals( true );
488+
mCellSizeYSpinBox->setValue( currentBBox.height() / mNumberOfRowsSpinBox->value() );
489+
mCellSizeYSpinBox->blockSignals( false );
490+
}
491+
}
492+
493+
void QgsInterpolationDialog::setNColsOnCellsizeXChange()
494+
{
495+
QgsRectangle currentBBox = currentBoundingBox();
496+
int newSize;
497+
498+
if ( !mCellsizeXSpinBox->value() > 0 )
499+
{
500+
return;
501+
}
502+
503+
if ( !currentBBox.width() > 0 )
504+
{
505+
newSize = 0;
506+
}
507+
else
508+
{
509+
newSize = ( int )( currentBBox.width() / mCellsizeXSpinBox->value() );
510+
}
511+
512+
mNumberOfColumnsSpinBox->blockSignals( true );
513+
mNumberOfColumnsSpinBox->setValue( newSize );
514+
mNumberOfColumnsSpinBox->blockSignals( false );
515+
}
516+
517+
void QgsInterpolationDialog::setNRowsOnCellsizeYChange()
518+
{
519+
QgsRectangle currentBBox = currentBoundingBox();
520+
int newSize;
521+
522+
if ( !mCellSizeYSpinBox->value() > 0 )
523+
{
524+
return;
525+
}
526+
527+
if ( !currentBBox.height() > 0 )
528+
{
529+
newSize = 0;
530+
}
531+
else
532+
{
533+
newSize = ( int )( currentBBox.height() / mCellSizeYSpinBox->value() );
534+
}
535+
536+
mNumberOfRowsSpinBox->blockSignals( true );
537+
mNumberOfRowsSpinBox->setValue( newSize );
538+
mNumberOfRowsSpinBox->blockSignals( false );
539+
}
540+
541+
QgsRectangle QgsInterpolationDialog::currentBoundingBox()
542+
{
543+
QString xMinString = mXMinLineEdit->text();
544+
QString xMaxString = mXMaxLineEdit->text();
545+
QString yMinString = mYMinLineEdit->text();
546+
QString yMaxString = mYMaxLineEdit->text();
547+
548+
bool xMinOk, xMaxOk, yMinOk, yMaxOk;
549+
double xMin = xMinString.toDouble( &xMinOk );
550+
double xMax = xMaxString.toDouble( &xMaxOk );
551+
double yMin = yMinString.toDouble( &yMinOk );
552+
double yMax = yMaxString.toDouble( &yMaxOk );
553+
554+
if ( !xMinOk || !xMaxOk || !yMinOk || !yMaxOk )
555+
{
556+
QgsRectangle emptyBbox;
557+
return emptyBbox; //error, return empty bounding box
558+
}
559+
560+
return QgsRectangle( xMin, yMin, xMax, yMax );
561+
}

src/plugins/interpolation/qgsinterpolationdialog.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSINTERPOLATIONDIALOG_H
2020

2121
#include "ui_qgsinterpolationdialogbase.h"
22+
#include "qgsrectangle.h"
2223
#include "qgisinterface.h"
2324
#include <QFileInfo>
2425

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

46+
void on_mNumberOfColumnsSpinBox_valueChanged( int value );
47+
void on_mNumberOfRowsSpinBox_valueChanged( int value );
48+
void on_mCellsizeXSpinBox_valueChanged( double value );
49+
void on_mCellSizeYSpinBox_valueChanged( double value );
50+
void on_mBBoxToCurrentExtent_clicked();
51+
52+
void on_mXMinLineEdit_textEdited( const QString& text );
53+
void on_mXMaxLineEdit_textEdited( const QString& text );
54+
void on_mYMinLineEdit_textEdited( const QString& text );
55+
void on_mYMaxLineEdit_textEdited( const QString& text );
56+
57+
58+
4559
private:
4660
QgisInterface* mIface;
4761
/**Dialog to get input for the current interpolation method*/
@@ -52,6 +66,19 @@ class QgsInterpolationDialog: public QDialog, private Ui::QgsInterpolationDialog
5266
QgsVectorLayer* vectorLayerFromName( const QString& name );
5367
/**Enables or disables the Ok button depending on the availability of input layers and the output file*/
5468
void enableOrDisableOkButton();
69+
/**Get the current output bounding box (might be different to the compound layers bounding box because of user edits)
70+
@return the bounding box or an empty bounding box in case of error*/
71+
QgsRectangle currentBoundingBox();
72+
/**Returns the compound bounding box of the inserted layers*/
73+
QgsRectangle boundingBoxOfLayers();
74+
/**Inserts the compound bounding box of the input layers into the line edits for the output bounding box*/
75+
void setLayersBoundingBox();
76+
/**Set cellsizes according to nex bounding box and number of columns / rows */
77+
void setNewCellsizeOnBoundingBoxChange();
78+
void setNewCellsizeXOnNColumnsChange();
79+
void setNewCellsizeYOnNRowschange();
80+
void setNColsOnCellsizeXChange();
81+
void setNRowsOnCellsizeYChange();
5582
};
5683

5784
#endif

0 commit comments

Comments
 (0)