Skip to content

Commit

Permalink
GRASS region reprojection
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@15756 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Apr 18, 2011
1 parent f116c35 commit 614c3b3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 22 deletions.
33 changes: 20 additions & 13 deletions src/plugins/grass/qgsgrassplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ QgsGrassPlugin::QgsGrassPlugin( QgisInterface * theQgisInterFace ):
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()
Expand Down Expand Up @@ -114,7 +121,6 @@ void QgsGrassPlugin::initGui()

QgsGrass::init();

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

// Connect project
Expand Down Expand Up @@ -565,6 +571,8 @@ void QgsGrassPlugin::newVector()

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.");
}

Expand Down Expand Up @@ -599,19 +607,9 @@ void QgsGrassPlugin::displayRegion()
return;
}

std::vector<QgsPoint> points;
points.resize( 5 );

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 );
QgsRectangle rect( QgsPoint( window.west, window.north ), QgsPoint( window.east, window.south ) );

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

void QgsGrassPlugin::switchRegion( bool on )
Expand Down Expand Up @@ -874,6 +872,15 @@ QIcon QgsGrassPlugin::getThemeIcon( const QString theName )
}
}

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
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/grass/qgsgrassplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,6 +128,7 @@ class QgsGrassPlugin: public QObject, public QgisPlugin
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;
Expand Down Expand Up @@ -155,6 +159,9 @@ class QgsGrassPlugin: public QObject, public QgisPlugin
QgsGrassNewMapset *mNewMapset;
QgsGrassEdit *mEdit;

QgsCoordinateReferenceSystem mCrs;
QgsCoordinateTransform mCoordinateTransform;

// Actions
QAction *mOpenMapsetAction;
QAction *mNewMapsetAction;
Expand Down
89 changes: 80 additions & 9 deletions src/plugins/grass/qgsgrassregion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "qgisinterface.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgsmaptool.h"

#include <QButtonGroup>
Expand All @@ -38,11 +39,19 @@ QgsGrassRegionEdit::QgsGrassRegionEdit( QgsMapCanvas* canvas )
{
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
Expand All @@ -51,6 +60,7 @@ void QgsGrassRegionEdit::canvasPressEvent( QMouseEvent * event )
QgsDebugMsg( "entered." );
mDraw = true;
mRubberBand->reset( true );
mSrcRubberBand->reset( true );
emit captureStarted();

mStartPoint = toMapCoordinates( event->pos() );
Expand Down Expand Up @@ -82,28 +92,88 @@ void QgsGrassRegionEdit::canvasReleaseEvent( QMouseEvent * event )
void QgsGrassRegionEdit::deactivate()
{
mRubberBand->reset( true );
mSrcRubberBand->reset( true );
QgsMapTool::deactivate();
}

void QgsGrassRegionEdit::setRegion( const QgsPoint& ul, const QgsPoint& lr )
{
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 );

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

mRubberBand->show();
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 )
Expand Down Expand Up @@ -397,7 +467,8 @@ void QgsGrassRegion::displayRegion()
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()
Expand Down Expand Up @@ -448,4 +519,4 @@ void QgsGrassRegion::saveWindowLocation()
{
QSettings settings;
settings.setValue( "/GRASS/windows/region/geometry", saveGeometry() );
}
}
14 changes: 14 additions & 0 deletions src/plugins/grass/qgsgrassregion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -152,6 +154,12 @@ class QgsGrassRegionEdit : public QgsMapTool

//! 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();
Expand All @@ -161,6 +169,7 @@ class QgsGrassRegionEdit : public QgsMapTool
private:
//! Rubber band for selecting grass region
QgsRubberBand* mRubberBand;
QgsRubberBand* mSrcRubberBand;

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

//! Region rectangle in source CRS
QgsRectangle mSrcRectangle;

QgsCoordinateReferenceSystem mCrs;
QgsCoordinateTransform mCoordinateTransform;
};

#endif // QGSGRASSREGION_H

0 comments on commit 614c3b3

Please sign in to comment.