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 ( ¤tLayerExtent );
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 ( ¤tLayerExtent );
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+ }
0 commit comments