[QGIS Commit] r15756 - trunk/qgis/src/plugins/grass

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Apr 18 08:24:28 EDT 2011


Author: rblazek
Date: 2011-04-18 05:24:28 -0700 (Mon, 18 Apr 2011)
New Revision: 15756

Modified:
   trunk/qgis/src/plugins/grass/qgsgrassplugin.cpp
   trunk/qgis/src/plugins/grass/qgsgrassplugin.h
   trunk/qgis/src/plugins/grass/qgsgrassregion.cpp
   trunk/qgis/src/plugins/grass/qgsgrassregion.h
Log:
GRASS region reprojection

Modified: trunk/qgis/src/plugins/grass/qgsgrassplugin.cpp
===================================================================
--- trunk/qgis/src/plugins/grass/qgsgrassplugin.cpp	2011-04-18 06:41:33 UTC (rev 15755)
+++ trunk/qgis/src/plugins/grass/qgsgrassplugin.cpp	2011-04-18 12:24:28 UTC (rev 15756)
@@ -65,6 +65,13 @@
   pluginNameQString = tr( "GrassVector" );
   pluginVersionQString = tr( "0.1" );
   pluginDescriptionQString = tr( "GRASS layer" );
+  QString gisdbase = QgsGrass::getDefaultGisdbase();
+  QString location = QgsGrass::getDefaultLocation();
+  mCanvas = qGisInterface->mapCanvas();
+  mCrs = QgsGrass::crs( gisdbase, location );
+  QgsDebugMsg( "mCrs: " + mCrs.toWkt() );
+  setTransform();
+  connect( qGisInterface->mapCanvas()->mapRenderer(), SIGNAL( destinationSrsChanged() ), this, SLOT( setTransform() ) );
 }
 
 QgsGrassPlugin::~QgsGrassPlugin()
@@ -114,7 +121,6 @@
 
   QgsGrass::init();
 
-  mCanvas = qGisInterface->mapCanvas();
   QWidget* qgis = qGisInterface->mainWindow();
 
   // Connect project
@@ -565,6 +571,8 @@
 
 void QgsGrassPlugin::postRender( QPainter *painter )
 {
+  // We have to redraw rectangle, because canvas->mapRenderer()->destinationCrs is set after GRASS plugin constructor! This way it is redrawn also if canvas CRS has changed.
+  displayRegion();
 // QgsDebugMsg("entered.");
 }
 
@@ -599,19 +607,9 @@
     return;
   }
 
-  std::vector<QgsPoint> points;
-  points.resize( 5 );
+  QgsRectangle rect( QgsPoint( window.west, window.north ), QgsPoint( window.east, window.south ) );
 
-  points[0].setX( window.west ); points[0].setY( window.south );
-  points[1].setX( window.east ); points[1].setY( window.south );
-  points[2].setX( window.east ); points[2].setY( window.north );
-  points[3].setX( window.west ); points[3].setY( window.north );
-  points[4].setX( window.west ); points[4].setY( window.south );
-
-  for ( int i = 0; i < 5; i++ )
-  {
-    mRegionBand->addPoint( points[i] );
-  }
+  QgsGrassRegionEdit::drawRegion( mCanvas, mRegionBand, rect, &mCoordinateTransform );
 }
 
 void QgsGrassPlugin::switchRegion( bool on )
@@ -874,6 +872,15 @@
   }
 }
 
+void QgsGrassPlugin::setTransform()
+{
+  if ( mCrs.isValid() && mCanvas->mapRenderer()->destinationCrs().isValid() )
+  {
+    mCoordinateTransform.setSourceCrs( mCrs );
+    mCoordinateTransform.setDestCRS( mCanvas->mapRenderer()->destinationCrs() );
+  }
+}
+
 /**
  * Required extern functions needed  for every plugin
  * These functions can be called prior to creating an instance

Modified: trunk/qgis/src/plugins/grass/qgsgrassplugin.h
===================================================================
--- trunk/qgis/src/plugins/grass/qgsgrassplugin.h	2011-04-18 06:41:33 UTC (rev 15755)
+++ trunk/qgis/src/plugins/grass/qgsgrassplugin.h	2011-04-18 12:24:28 UTC (rev 15756)
@@ -17,9 +17,12 @@
 #ifndef QGSGRASSPLUGIN_H
 #define QGSGRASSPLUGIN_H
 #include "../qgisplugin.h"
+#include "qgscoordinatereferencesystem.h"
+#include "qgscoordinatetransform.h"
 #include <QObject>
 #include <QPen>
 
+
 class QgsGrassTools;
 class QgsGrassNewMapset;
 class QgsGrassRegion;
@@ -125,6 +128,7 @@
     void cleanUp();
     //! update plugin icons when the app tells us its theme is changed
     void setCurrentTheme( QString theThemeName );
+    void setTransform();
   private:
     //! Name of the plugin
     QString pluginNameQString;
@@ -155,6 +159,9 @@
     QgsGrassNewMapset *mNewMapset;
     QgsGrassEdit *mEdit;
 
+    QgsCoordinateReferenceSystem mCrs;
+    QgsCoordinateTransform mCoordinateTransform;
+
     // Actions
     QAction *mOpenMapsetAction;
     QAction *mNewMapsetAction;

Modified: trunk/qgis/src/plugins/grass/qgsgrassregion.cpp
===================================================================
--- trunk/qgis/src/plugins/grass/qgsgrassregion.cpp	2011-04-18 06:41:33 UTC (rev 15755)
+++ trunk/qgis/src/plugins/grass/qgsgrassregion.cpp	2011-04-18 12:24:28 UTC (rev 15756)
@@ -22,6 +22,7 @@
 #include "qgisinterface.h"
 #include "qgslogger.h"
 #include "qgsmapcanvas.h"
+#include "qgsmaprenderer.h"
 #include "qgsmaptool.h"
 
 #include <QButtonGroup>
@@ -38,11 +39,19 @@
 {
   mDraw = false;
   mRubberBand = new QgsRubberBand( mCanvas, true );
+  mSrcRubberBand = new QgsRubberBand( mCanvas, true );
+  QString gisdbase = QgsGrass::getDefaultGisdbase();
+  QString location = QgsGrass::getDefaultLocation();
+  mCrs = QgsGrass::crs( gisdbase, location );
+  QgsDebugMsg( "mCrs: " + mCrs.toWkt() );
+  setTransform();
+  connect( canvas->mapRenderer(), SIGNAL( destinationSrsChanged() ), this, SLOT( setTransform() ) );
 }
 
 QgsGrassRegionEdit::~QgsGrassRegionEdit()
 {
   delete mRubberBand;
+  delete mSrcRubberBand;
 }
 
 //! mouse pressed in map canvas
@@ -51,6 +60,7 @@
   QgsDebugMsg( "entered." );
   mDraw = true;
   mRubberBand->reset( true );
+  mSrcRubberBand->reset( true );
   emit captureStarted();
 
   mStartPoint = toMapCoordinates( event->pos() );
@@ -82,6 +92,7 @@
 void QgsGrassRegionEdit::deactivate()
 {
   mRubberBand->reset( true );
+  mSrcRubberBand->reset( true );
   QgsMapTool::deactivate();
 }
 
@@ -89,21 +100,80 @@
 {
   mStartPoint = ul;
   mEndPoint = lr;
+  calcSrcRegion();
+  drawRegion( canvas(), mRubberBand, mSrcRectangle, &mCoordinateTransform );
+  drawRegion( canvas(), mSrcRubberBand, QgsRectangle( mStartPoint, mEndPoint ) );
+}
 
-  mRubberBand->reset( true );
-  mRubberBand->addPoint( ul, false );
-  mRubberBand->addPoint( QgsPoint( ul.x(), lr.y() ), false );
-  mRubberBand->addPoint( lr, false );
-  mRubberBand->addPoint( QgsPoint( lr.x(), ul.y() ), true ); // true to update canvas
+void QgsGrassRegionEdit::calcSrcRegion()
+{
+  mSrcRectangle.set( mStartPoint, mEndPoint );
 
-  mRubberBand->show();
+  if ( mCanvas->mapRenderer()->hasCrsTransformEnabled() && mCrs.isValid() && mCanvas->mapRenderer()->destinationCrs().isValid() )
+  {
+    QgsCoordinateTransform coordinateTransform;
+    coordinateTransform.setSourceCrs( mCanvas->mapRenderer()->destinationCrs() );
+    coordinateTransform.setDestCRS( mCrs );
+    mSrcRectangle = coordinateTransform.transformBoundingBox( mSrcRectangle );
+  }
 }
 
+void QgsGrassRegionEdit::setTransform()
+{
+  if ( mCrs.isValid() && canvas()->mapRenderer()->destinationCrs().isValid() )
+  {
+    mCoordinateTransform.setSourceCrs( mCrs );
+    mCoordinateTransform.setDestCRS( canvas()->mapRenderer()->destinationCrs() );
+  }
+}
+
+void QgsGrassRegionEdit::transform( QgsMapCanvas *canvas, QVector<QgsPoint> &points, QgsCoordinateTransform *coordinateTransform, QgsCoordinateTransform::TransformDirection direction )
+{
+  QgsDebugMsg( "Entered" );
+  /** Coordinate transform */
+  if ( canvas->mapRenderer()->hasCrsTransformEnabled() )
+  {
+    //QgsDebugMsg ( "srcCrs = " +  coordinateTransform->sourceCrs().toWkt() );
+    //QgsDebugMsg ( "destCrs = " +  coordinateTransform->destCRS().toWkt() );
+    for ( int i = 0; i < points.size(); i++ )
+    {
+      points[i] = coordinateTransform->transform( points[i], direction );
+    }
+  }
+}
+
+void QgsGrassRegionEdit::drawRegion( QgsMapCanvas *canvas, QgsRubberBand* rubberBand, const QgsRectangle &rect, QgsCoordinateTransform * coordinateTransform )
+{
+  QVector<QgsPoint> points;
+  points.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
+  points.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
+  points.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
+  points.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
+
+  if ( coordinateTransform )
+  {
+    transform( canvas, points, coordinateTransform );
+  }
+  rubberBand->reset( true );
+  for ( int i = 0; i < points.size(); i++ )
+  {
+    bool update = false; // true to update canvas
+    if ( i == points.size() - 1 ) update = true;
+    rubberBand->addPoint( points[i], update );
+  }
+  rubberBand->show();
+}
+
 QgsRectangle QgsGrassRegionEdit::getRegion()
 {
-  return QgsRectangle( mStartPoint, mEndPoint );
+  //return QgsRectangle( mStartPoint, mEndPoint );
+  return mSrcRectangle;
 }
 
+void QgsGrassRegionEdit::setSrcRegion( const QgsRectangle &rect )
+{
+  mSrcRectangle = rect;
+}
 
 QgsGrassRegion::QgsGrassRegion( QgsGrassPlugin *plugin,  QgisInterface *iface,
                                 QWidget * parent, Qt::WFlags f )
@@ -397,7 +467,8 @@
   QgsPoint ul( mWindow.west, mWindow.north );
   QgsPoint lr( mWindow.east, mWindow.south );
 
-  mRegionEdit->setRegion( ul, lr );
+  //mRegionEdit->setRegion( ul, lr );
+  mRegionEdit->setSrcRegion( QgsRectangle( ul, lr ) );
 }
 
 void QgsGrassRegion::accept()
@@ -448,4 +519,4 @@
 {
   QSettings settings;
   settings.setValue( "/GRASS/windows/region/geometry", saveGeometry() );
-}
+}
\ No newline at end of file

Modified: trunk/qgis/src/plugins/grass/qgsgrassregion.h
===================================================================
--- trunk/qgis/src/plugins/grass/qgsgrassregion.h	2011-04-18 06:41:33 UTC (rev 15755)
+++ trunk/qgis/src/plugins/grass/qgsgrassregion.h	2011-04-18 12:24:28 UTC (rev 15756)
@@ -17,6 +17,8 @@
 #define QGSGRASSREGION_H
 
 #include "ui_qgsgrassregionbase.h"
+#include "qgscoordinatereferencesystem.h"
+#include "qgscoordinatetransform.h"
 #include "qgsmaptool.h"
 #include "qgsrubberband.h"
 #include "qgspoint.h"
@@ -152,7 +154,13 @@
 
     //! refresh the rectangle displayed in canvas
     void setRegion( const QgsPoint&, const QgsPoint& );
+    void setSrcRegion( const QgsRectangle &rect );
 
+    void setTransform();
+    static void drawRegion( QgsMapCanvas *canvas, QgsRubberBand* rubberBand, const QgsRectangle &rect, QgsCoordinateTransform *coordinateTransform = 0 );
+    void calcSrcRegion();
+    static void transform( QgsMapCanvas *canvas, QVector<QgsPoint> &points, QgsCoordinateTransform *coordinateTransform, QgsCoordinateTransform::TransformDirection direction = QgsCoordinateTransform::ForwardTransform );
+
   signals:
     void captureStarted();
     void captureEnded();
@@ -161,6 +169,7 @@
   private:
     //! Rubber band for selecting grass region
     QgsRubberBand* mRubberBand;
+    QgsRubberBand* mSrcRubberBand;
 
     //! Status of input from canvas
     bool mDraw;
@@ -170,6 +179,11 @@
     //! Last rectangle point
     QgsPoint mEndPoint;
 
+    //! Region rectangle in source CRS
+    QgsRectangle mSrcRectangle;
+
+    QgsCoordinateReferenceSystem mCrs;
+    QgsCoordinateTransform mCoordinateTransform;
 };
 
 #endif // QGSGRASSREGION_H



More information about the QGIS-commit mailing list