Skip to content

Commit aa615f0

Browse files
author
rblazek
committedApr 18, 2011
GRASS region reprojection
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15756 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

4 files changed

+121
-22
lines changed

4 files changed

+121
-22
lines changed
 

‎src/plugins/grass/qgsgrassplugin.cpp‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ QgsGrassPlugin::QgsGrassPlugin( QgisInterface * theQgisInterFace ):
6565
pluginNameQString = tr( "GrassVector" );
6666
pluginVersionQString = tr( "0.1" );
6767
pluginDescriptionQString = tr( "GRASS layer" );
68+
QString gisdbase = QgsGrass::getDefaultGisdbase();
69+
QString location = QgsGrass::getDefaultLocation();
70+
mCanvas = qGisInterface->mapCanvas();
71+
mCrs = QgsGrass::crs( gisdbase, location );
72+
QgsDebugMsg( "mCrs: " + mCrs.toWkt() );
73+
setTransform();
74+
connect( qGisInterface->mapCanvas()->mapRenderer(), SIGNAL( destinationSrsChanged() ), this, SLOT( setTransform() ) );
6875
}
6976

7077
QgsGrassPlugin::~QgsGrassPlugin()
@@ -114,7 +121,6 @@ void QgsGrassPlugin::initGui()
114121

115122
QgsGrass::init();
116123

117-
mCanvas = qGisInterface->mapCanvas();
118124
QWidget* qgis = qGisInterface->mainWindow();
119125

120126
// Connect project
@@ -565,6 +571,8 @@ void QgsGrassPlugin::newVector()
565571

566572
void QgsGrassPlugin::postRender( QPainter *painter )
567573
{
574+
// 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.
575+
displayRegion();
568576
// QgsDebugMsg("entered.");
569577
}
570578

@@ -599,19 +607,9 @@ void QgsGrassPlugin::displayRegion()
599607
return;
600608
}
601609

602-
std::vector<QgsPoint> points;
603-
points.resize( 5 );
604-
605-
points[0].setX( window.west ); points[0].setY( window.south );
606-
points[1].setX( window.east ); points[1].setY( window.south );
607-
points[2].setX( window.east ); points[2].setY( window.north );
608-
points[3].setX( window.west ); points[3].setY( window.north );
609-
points[4].setX( window.west ); points[4].setY( window.south );
610+
QgsRectangle rect( QgsPoint( window.west, window.north ), QgsPoint( window.east, window.south ) );
610611

611-
for ( int i = 0; i < 5; i++ )
612-
{
613-
mRegionBand->addPoint( points[i] );
614-
}
612+
QgsGrassRegionEdit::drawRegion( mCanvas, mRegionBand, rect, &mCoordinateTransform );
615613
}
616614

617615
void QgsGrassPlugin::switchRegion( bool on )
@@ -874,6 +872,15 @@ QIcon QgsGrassPlugin::getThemeIcon( const QString theName )
874872
}
875873
}
876874

875+
void QgsGrassPlugin::setTransform()
876+
{
877+
if ( mCrs.isValid() && mCanvas->mapRenderer()->destinationCrs().isValid() )
878+
{
879+
mCoordinateTransform.setSourceCrs( mCrs );
880+
mCoordinateTransform.setDestCRS( mCanvas->mapRenderer()->destinationCrs() );
881+
}
882+
}
883+
877884
/**
878885
* Required extern functions needed for every plugin
879886
* These functions can be called prior to creating an instance

‎src/plugins/grass/qgsgrassplugin.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
#ifndef QGSGRASSPLUGIN_H
1818
#define QGSGRASSPLUGIN_H
1919
#include "../qgisplugin.h"
20+
#include "qgscoordinatereferencesystem.h"
21+
#include "qgscoordinatetransform.h"
2022
#include <QObject>
2123
#include <QPen>
2224

25+
2326
class QgsGrassTools;
2427
class QgsGrassNewMapset;
2528
class QgsGrassRegion;
@@ -125,6 +128,7 @@ class QgsGrassPlugin: public QObject, public QgisPlugin
125128
void cleanUp();
126129
//! update plugin icons when the app tells us its theme is changed
127130
void setCurrentTheme( QString theThemeName );
131+
void setTransform();
128132
private:
129133
//! Name of the plugin
130134
QString pluginNameQString;
@@ -155,6 +159,9 @@ class QgsGrassPlugin: public QObject, public QgisPlugin
155159
QgsGrassNewMapset *mNewMapset;
156160
QgsGrassEdit *mEdit;
157161

162+
QgsCoordinateReferenceSystem mCrs;
163+
QgsCoordinateTransform mCoordinateTransform;
164+
158165
// Actions
159166
QAction *mOpenMapsetAction;
160167
QAction *mNewMapsetAction;

‎src/plugins/grass/qgsgrassregion.cpp‎

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgisinterface.h"
2323
#include "qgslogger.h"
2424
#include "qgsmapcanvas.h"
25+
#include "qgsmaprenderer.h"
2526
#include "qgsmaptool.h"
2627

2728
#include <QButtonGroup>
@@ -38,11 +39,19 @@ QgsGrassRegionEdit::QgsGrassRegionEdit( QgsMapCanvas* canvas )
3839
{
3940
mDraw = false;
4041
mRubberBand = new QgsRubberBand( mCanvas, true );
42+
mSrcRubberBand = new QgsRubberBand( mCanvas, true );
43+
QString gisdbase = QgsGrass::getDefaultGisdbase();
44+
QString location = QgsGrass::getDefaultLocation();
45+
mCrs = QgsGrass::crs( gisdbase, location );
46+
QgsDebugMsg( "mCrs: " + mCrs.toWkt() );
47+
setTransform();
48+
connect( canvas->mapRenderer(), SIGNAL( destinationSrsChanged() ), this, SLOT( setTransform() ) );
4149
}
4250

4351
QgsGrassRegionEdit::~QgsGrassRegionEdit()
4452
{
4553
delete mRubberBand;
54+
delete mSrcRubberBand;
4655
}
4756

4857
//! mouse pressed in map canvas
@@ -51,6 +60,7 @@ void QgsGrassRegionEdit::canvasPressEvent( QMouseEvent * event )
5160
QgsDebugMsg( "entered." );
5261
mDraw = true;
5362
mRubberBand->reset( true );
63+
mSrcRubberBand->reset( true );
5464
emit captureStarted();
5565

5666
mStartPoint = toMapCoordinates( event->pos() );
@@ -82,28 +92,88 @@ void QgsGrassRegionEdit::canvasReleaseEvent( QMouseEvent * event )
8292
void QgsGrassRegionEdit::deactivate()
8393
{
8494
mRubberBand->reset( true );
95+
mSrcRubberBand->reset( true );
8596
QgsMapTool::deactivate();
8697
}
8798

8899
void QgsGrassRegionEdit::setRegion( const QgsPoint& ul, const QgsPoint& lr )
89100
{
90101
mStartPoint = ul;
91102
mEndPoint = lr;
103+
calcSrcRegion();
104+
drawRegion( canvas(), mRubberBand, mSrcRectangle, &mCoordinateTransform );
105+
drawRegion( canvas(), mSrcRubberBand, QgsRectangle( mStartPoint, mEndPoint ) );
106+
}
92107

93-
mRubberBand->reset( true );
94-
mRubberBand->addPoint( ul, false );
95-
mRubberBand->addPoint( QgsPoint( ul.x(), lr.y() ), false );
96-
mRubberBand->addPoint( lr, false );
97-
mRubberBand->addPoint( QgsPoint( lr.x(), ul.y() ), true ); // true to update canvas
108+
void QgsGrassRegionEdit::calcSrcRegion()
109+
{
110+
mSrcRectangle.set( mStartPoint, mEndPoint );
111+
112+
if ( mCanvas->mapRenderer()->hasCrsTransformEnabled() && mCrs.isValid() && mCanvas->mapRenderer()->destinationCrs().isValid() )
113+
{
114+
QgsCoordinateTransform coordinateTransform;
115+
coordinateTransform.setSourceCrs( mCanvas->mapRenderer()->destinationCrs() );
116+
coordinateTransform.setDestCRS( mCrs );
117+
mSrcRectangle = coordinateTransform.transformBoundingBox( mSrcRectangle );
118+
}
119+
}
120+
121+
void QgsGrassRegionEdit::setTransform()
122+
{
123+
if ( mCrs.isValid() && canvas()->mapRenderer()->destinationCrs().isValid() )
124+
{
125+
mCoordinateTransform.setSourceCrs( mCrs );
126+
mCoordinateTransform.setDestCRS( canvas()->mapRenderer()->destinationCrs() );
127+
}
128+
}
98129

99-
mRubberBand->show();
130+
void QgsGrassRegionEdit::transform( QgsMapCanvas *canvas, QVector<QgsPoint> &points, QgsCoordinateTransform *coordinateTransform, QgsCoordinateTransform::TransformDirection direction )
131+
{
132+
QgsDebugMsg( "Entered" );
133+
/** Coordinate transform */
134+
if ( canvas->mapRenderer()->hasCrsTransformEnabled() )
135+
{
136+
//QgsDebugMsg ( "srcCrs = " + coordinateTransform->sourceCrs().toWkt() );
137+
//QgsDebugMsg ( "destCrs = " + coordinateTransform->destCRS().toWkt() );
138+
for ( int i = 0; i < points.size(); i++ )
139+
{
140+
points[i] = coordinateTransform->transform( points[i], direction );
141+
}
142+
}
143+
}
144+
145+
void QgsGrassRegionEdit::drawRegion( QgsMapCanvas *canvas, QgsRubberBand* rubberBand, const QgsRectangle &rect, QgsCoordinateTransform * coordinateTransform )
146+
{
147+
QVector<QgsPoint> points;
148+
points.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
149+
points.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
150+
points.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
151+
points.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
152+
153+
if ( coordinateTransform )
154+
{
155+
transform( canvas, points, coordinateTransform );
156+
}
157+
rubberBand->reset( true );
158+
for ( int i = 0; i < points.size(); i++ )
159+
{
160+
bool update = false; // true to update canvas
161+
if ( i == points.size() - 1 ) update = true;
162+
rubberBand->addPoint( points[i], update );
163+
}
164+
rubberBand->show();
100165
}
101166

102167
QgsRectangle QgsGrassRegionEdit::getRegion()
103168
{
104-
return QgsRectangle( mStartPoint, mEndPoint );
169+
//return QgsRectangle( mStartPoint, mEndPoint );
170+
return mSrcRectangle;
105171
}
106172

173+
void QgsGrassRegionEdit::setSrcRegion( const QgsRectangle &rect )
174+
{
175+
mSrcRectangle = rect;
176+
}
107177

108178
QgsGrassRegion::QgsGrassRegion( QgsGrassPlugin *plugin, QgisInterface *iface,
109179
QWidget * parent, Qt::WFlags f )
@@ -397,7 +467,8 @@ void QgsGrassRegion::displayRegion()
397467
QgsPoint ul( mWindow.west, mWindow.north );
398468
QgsPoint lr( mWindow.east, mWindow.south );
399469

400-
mRegionEdit->setRegion( ul, lr );
470+
//mRegionEdit->setRegion( ul, lr );
471+
mRegionEdit->setSrcRegion( QgsRectangle( ul, lr ) );
401472
}
402473

403474
void QgsGrassRegion::accept()
@@ -448,4 +519,4 @@ void QgsGrassRegion::saveWindowLocation()
448519
{
449520
QSettings settings;
450521
settings.setValue( "/GRASS/windows/region/geometry", saveGeometry() );
451-
}
522+
}

‎src/plugins/grass/qgsgrassregion.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define QGSGRASSREGION_H
1818

1919
#include "ui_qgsgrassregionbase.h"
20+
#include "qgscoordinatereferencesystem.h"
21+
#include "qgscoordinatetransform.h"
2022
#include "qgsmaptool.h"
2123
#include "qgsrubberband.h"
2224
#include "qgspoint.h"
@@ -152,6 +154,12 @@ class QgsGrassRegionEdit : public QgsMapTool
152154

153155
//! refresh the rectangle displayed in canvas
154156
void setRegion( const QgsPoint&, const QgsPoint& );
157+
void setSrcRegion( const QgsRectangle &rect );
158+
159+
void setTransform();
160+
static void drawRegion( QgsMapCanvas *canvas, QgsRubberBand* rubberBand, const QgsRectangle &rect, QgsCoordinateTransform *coordinateTransform = 0 );
161+
void calcSrcRegion();
162+
static void transform( QgsMapCanvas *canvas, QVector<QgsPoint> &points, QgsCoordinateTransform *coordinateTransform, QgsCoordinateTransform::TransformDirection direction = QgsCoordinateTransform::ForwardTransform );
155163

156164
signals:
157165
void captureStarted();
@@ -161,6 +169,7 @@ class QgsGrassRegionEdit : public QgsMapTool
161169
private:
162170
//! Rubber band for selecting grass region
163171
QgsRubberBand* mRubberBand;
172+
QgsRubberBand* mSrcRubberBand;
164173

165174
//! Status of input from canvas
166175
bool mDraw;
@@ -170,6 +179,11 @@ class QgsGrassRegionEdit : public QgsMapTool
170179
//! Last rectangle point
171180
QgsPoint mEndPoint;
172181

182+
//! Region rectangle in source CRS
183+
QgsRectangle mSrcRectangle;
184+
185+
QgsCoordinateReferenceSystem mCrs;
186+
QgsCoordinateTransform mCoordinateTransform;
173187
};
174188

175189
#endif // QGSGRASSREGION_H

0 commit comments

Comments
 (0)
Please sign in to comment.