Skip to content

Commit

Permalink
projection support
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@5411 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed May 3, 2006
1 parent 0910ad4 commit ab56c02
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
76 changes: 65 additions & 11 deletions src/plugins/grass/qgsgrassedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "qgslegend.h"
#include "qgsvertexmarker.h"
#include "qgsrubberband.h"
#include "qgsproject.h"

extern "C" {
#include <grass/gis.h>
Expand Down Expand Up @@ -120,6 +121,8 @@ QgsGrassEdit::QgsGrassEdit ( QgisApp *qgisApp, QgisIface *iface,
mIface = iface;
mNewMap = false;

mProjectionEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0);

mCanvas = mIface->getMapCanvas();

// TODO QGIS: crash if canvas is empty
Expand All @@ -130,10 +133,10 @@ QgsGrassEdit::QgsGrassEdit ( QgisApp *qgisApp, QgisIface *iface,
if ( !isEditable(layer) ) return;

//TODO dynamic_cast ?
QgsVectorLayer *vector = (QgsVectorLayer*)layer;
mLayer = (QgsVectorLayer*)layer;

//TODO dynamic_cast ?
mProvider = (QgsGrassProvider *) vector->getDataProvider();
mProvider = (QgsGrassProvider *) mLayer->getDataProvider();

init();

Expand Down Expand Up @@ -1097,11 +1100,20 @@ double QgsGrassEdit::threshold ( void )
int snapPixels = mSnapPixels->text().toInt();

// Convert to map units (not nice)
mTransform = mCanvas->getCoordinateTransform();
double x1 = mTransform->toMapCoordinates( 0, 0 ).x();
double x2 = mTransform->toMapCoordinates( snapPixels, 0 ).x();
QgsPoint p1, p2;
p1 = mTransform->toMapCoordinates(0, 0 );
p2 = mTransform->toMapCoordinates(snapPixels, 0);

return ( x2 - x1 );
if ( mProjectionEnabled )
{
p1 = mLayer->coordinateTransform()->transform(p1, QgsCoordinateTransform::INVERSE );
p2 = mLayer->coordinateTransform()->transform(p2, QgsCoordinateTransform::INVERSE );
}

double dx = p2.x() - p1.x();
double dy = p2.y() - p1.y();
double thresh = sqrt ( dx*dx + dy*dy );
return thresh;
}

void QgsGrassEdit::snap ( double *x, double *y )
Expand Down Expand Up @@ -1461,6 +1473,7 @@ void QgsGrassEdit::displayUpdated (void)
#endif

mTransform = mCanvas->getCoordinateTransform();
mProjectionEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0);

QPainter *painter = new QPainter();
painter->begin(mPixmap);
Expand Down Expand Up @@ -1518,7 +1531,7 @@ void QgsGrassEdit::displayElement ( int line, const QPen & pen, int size, QPaint
for ( int i = 0; i < mPoints->n_points; i++ ) {
point.setX(mPoints->x[i]);
point.setY(mPoints->y[i]);
mTransform->transform(&point);
point = transformLayerToCanvas ( point );
pointArray.setPoint( i, static_cast<int>(round(point.x())),
static_cast<int>(round(point.y())) );
}
Expand Down Expand Up @@ -1586,7 +1599,10 @@ void QgsGrassEdit::displayDynamic ( struct line_pnts *Points, double x, double y
#endif
QgsPoint point;

mTransform = mCanvas->getCoordinateTransform();
//mTransform = mCanvas->getCoordinateTransform();

// Dynamic points are in layer coordinate system, we have to
// reproject them to current coordinate system if necessary

mRubberBandLine->reset();

Expand All @@ -1596,13 +1612,15 @@ void QgsGrassEdit::displayDynamic ( struct line_pnts *Points, double x, double y
{
point.setX(Points->x[i]);
point.setY(Points->y[i]);
mRubberBandLine->addPoint(point);
point = transformLayerToMap ( point );
mRubberBandLine->addPoint(point);
}
}

mRubberBandIcon->setIconType(type);
mRubberBandIcon->setIconSize(size);
mRubberBandIcon->setCenter(QgsPoint(x,y));
point = transformLayerToMap (QgsPoint(x,y) );
mRubberBandIcon->setCenter(point);
}

void QgsGrassEdit::displayNode ( int node, const QPen & pen, int size, QPainter *painter )
Expand All @@ -1620,6 +1638,41 @@ void QgsGrassEdit::displayNode ( int node, const QPen & pen, int size, QPainter
displayIcon ( x, y, pen, QgsVertexMarker::ICON_X, size, painter );
}

QgsPoint QgsGrassEdit::transformLayerToCanvas ( QgsPoint point)
{
if ( mProjectionEnabled && mLayer->coordinateTransform() )
{
try
{
point = mLayer->coordinateTransform()->transform(point);
}
catch(QgsCsException &cse)
{
std::cout << "cannot transform point" << std::endl;
}

}
mTransform->transform(&point);
return point;
}

QgsPoint QgsGrassEdit::transformLayerToMap ( QgsPoint point)
{
if ( mProjectionEnabled && mLayer->coordinateTransform() )
{
try
{
point = mLayer->coordinateTransform()->transform(point);
}
catch(QgsCsException &cse)
{
std::cout << "cannot transform point" << std::endl;
}

}
return point;
}

void QgsGrassEdit::displayIcon ( double x, double y, const QPen & pen,
int type, int size, QPainter *painter )
{
Expand All @@ -1632,7 +1685,8 @@ void QgsGrassEdit::displayIcon ( double x, double y, const QPen & pen,

point.setX(x);
point.setY(y);
mTransform->transform(&point);

point = transformLayerToCanvas ( point );

int px = static_cast<int>(round(point.x()));
int py = static_cast<int>(round(point.y()));
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/grass/qgsgrassedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class QCloseEvent;
#include "qgsmaptopixel.h"
class QgsRubberBand;
class QgsVertexMarker;
class QgsVectorLayer;
class QgsGrassEditLayer;
class QgsGrassAttributes;

Expand Down Expand Up @@ -151,6 +152,9 @@ class QgsGrassEdit: public QMainWindow, private Ui::QgsGrassEditBase
//! Check orphan database records
void checkOrphan ( int field, int cat );

//! pointer to layer
QgsVectorLayer *layer() { return mLayer; }

public slots:
// TODO: once available in QGIS, use only one reciver for all signals

Expand Down Expand Up @@ -219,10 +223,19 @@ public slots:
private:
//! Editing is already running
static bool mRunning;

//! Pointer to edited layer
QgsVectorLayer *mLayer;

//! Point / node size (later make editable array of Sizes)
int mSize;

//! Transform from layer coordinates to canvas including reprojection
QgsPoint transformLayerToCanvas ( QgsPoint point);

//! Transform from layer coordinates to current projection
QgsPoint transformLayerToMap ( QgsPoint point);

//! Display all lines and nodes
void displayMap ();

Expand Down Expand Up @@ -416,6 +429,9 @@ public slots:
QAction *mEditAttributesAction;
QAction *mCloseEditAction;

// Is projection enabled?
bool mProjectionEnabled;

// Canvas items
QgsRubberBand *mRubberBandLine;
QgsVertexMarker *mRubberBandIcon;
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/grass/qgsgrassedittools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
***************************************************************************/

#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgsgrassedittools.h"
#include "qgsgrassedit.h"
#include "qgsgrassattributes.h"
Expand All @@ -35,7 +37,7 @@ QgsGrassEditTool::QgsGrassEditTool(QgsGrassEdit* edit)

void QgsGrassEditTool::canvasPressEvent(QMouseEvent * event)
{
QgsPoint point = toMapCoords(event->pos());
QgsPoint point = toLayerCoords(e->layer(), event->pos());
mouseClick(point, event->button());

// Set last click
Expand All @@ -50,7 +52,7 @@ void QgsGrassEditTool::canvasPressEvent(QMouseEvent * event)

void QgsGrassEditTool::canvasMoveEvent(QMouseEvent * event)
{
QgsPoint point = toMapCoords(event->pos());
QgsPoint point = toLayerCoords(e->layer(), event->pos());
mouseMove(point);

e->statusBar()->message(e->mCanvasPrompt);
Expand Down Expand Up @@ -634,7 +636,7 @@ void QgsGrassEditDeleteLine::mouseClick(QgsPoint & point, Qt::ButtonState button

if ( e->mSelectedLine == 0 )
e->mSelectedLine = e->mProvider->findLine ( point.x(), point.y(), GV_LINE|GV_BOUNDARY, thresh );

if ( e->mSelectedLine ) { // highlite, propmt
e->displayElement ( e->mSelectedLine, e->mSymb[QgsGrassEdit::SYMB_HIGHLIGHT], e->mSize );
e->setCanvasPropmt( QObject::tr("Delete selected / select next"), "", QObject::tr("Release selected") );
Expand Down

0 comments on commit ab56c02

Please sign in to comment.