Skip to content

Commit ab56c02

Browse files
author
rblazek
committed
projection support
git-svn-id: http://svn.osgeo.org/qgis/trunk@5411 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0910ad4 commit ab56c02

File tree

3 files changed

+86
-14
lines changed

3 files changed

+86
-14
lines changed

src/plugins/grass/qgsgrassedit.cpp

+65-11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "qgslegend.h"
5656
#include "qgsvertexmarker.h"
5757
#include "qgsrubberband.h"
58+
#include "qgsproject.h"
5859

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

124+
mProjectionEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0);
125+
123126
mCanvas = mIface->getMapCanvas();
124127

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

132135
//TODO dynamic_cast ?
133-
QgsVectorLayer *vector = (QgsVectorLayer*)layer;
136+
mLayer = (QgsVectorLayer*)layer;
134137

135138
//TODO dynamic_cast ?
136-
mProvider = (QgsGrassProvider *) vector->getDataProvider();
139+
mProvider = (QgsGrassProvider *) mLayer->getDataProvider();
137140

138141
init();
139142

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

10991102
// Convert to map units (not nice)
1100-
mTransform = mCanvas->getCoordinateTransform();
1101-
double x1 = mTransform->toMapCoordinates( 0, 0 ).x();
1102-
double x2 = mTransform->toMapCoordinates( snapPixels, 0 ).x();
1103+
QgsPoint p1, p2;
1104+
p1 = mTransform->toMapCoordinates(0, 0 );
1105+
p2 = mTransform->toMapCoordinates(snapPixels, 0);
11031106

1104-
return ( x2 - x1 );
1107+
if ( mProjectionEnabled )
1108+
{
1109+
p1 = mLayer->coordinateTransform()->transform(p1, QgsCoordinateTransform::INVERSE );
1110+
p2 = mLayer->coordinateTransform()->transform(p2, QgsCoordinateTransform::INVERSE );
1111+
}
1112+
1113+
double dx = p2.x() - p1.x();
1114+
double dy = p2.y() - p1.y();
1115+
double thresh = sqrt ( dx*dx + dy*dy );
1116+
return thresh;
11051117
}
11061118

11071119
void QgsGrassEdit::snap ( double *x, double *y )
@@ -1461,6 +1473,7 @@ void QgsGrassEdit::displayUpdated (void)
14611473
#endif
14621474

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

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

1589-
mTransform = mCanvas->getCoordinateTransform();
1602+
//mTransform = mCanvas->getCoordinateTransform();
1603+
1604+
// Dynamic points are in layer coordinate system, we have to
1605+
// reproject them to current coordinate system if necessary
15901606

15911607
mRubberBandLine->reset();
15921608

@@ -1596,13 +1612,15 @@ void QgsGrassEdit::displayDynamic ( struct line_pnts *Points, double x, double y
15961612
{
15971613
point.setX(Points->x[i]);
15981614
point.setY(Points->y[i]);
1599-
mRubberBandLine->addPoint(point);
1615+
point = transformLayerToMap ( point );
1616+
mRubberBandLine->addPoint(point);
16001617
}
16011618
}
16021619

16031620
mRubberBandIcon->setIconType(type);
16041621
mRubberBandIcon->setIconSize(size);
1605-
mRubberBandIcon->setCenter(QgsPoint(x,y));
1622+
point = transformLayerToMap (QgsPoint(x,y) );
1623+
mRubberBandIcon->setCenter(point);
16061624
}
16071625

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

1641+
QgsPoint QgsGrassEdit::transformLayerToCanvas ( QgsPoint point)
1642+
{
1643+
if ( mProjectionEnabled && mLayer->coordinateTransform() )
1644+
{
1645+
try
1646+
{
1647+
point = mLayer->coordinateTransform()->transform(point);
1648+
}
1649+
catch(QgsCsException &cse)
1650+
{
1651+
std::cout << "cannot transform point" << std::endl;
1652+
}
1653+
1654+
}
1655+
mTransform->transform(&point);
1656+
return point;
1657+
}
1658+
1659+
QgsPoint QgsGrassEdit::transformLayerToMap ( QgsPoint point)
1660+
{
1661+
if ( mProjectionEnabled && mLayer->coordinateTransform() )
1662+
{
1663+
try
1664+
{
1665+
point = mLayer->coordinateTransform()->transform(point);
1666+
}
1667+
catch(QgsCsException &cse)
1668+
{
1669+
std::cout << "cannot transform point" << std::endl;
1670+
}
1671+
1672+
}
1673+
return point;
1674+
}
1675+
16231676
void QgsGrassEdit::displayIcon ( double x, double y, const QPen & pen,
16241677
int type, int size, QPainter *painter )
16251678
{
@@ -1632,7 +1685,8 @@ void QgsGrassEdit::displayIcon ( double x, double y, const QPen & pen,
16321685

16331686
point.setX(x);
16341687
point.setY(y);
1635-
mTransform->transform(&point);
1688+
1689+
point = transformLayerToCanvas ( point );
16361690

16371691
int px = static_cast<int>(round(point.x()));
16381692
int py = static_cast<int>(round(point.y()));

src/plugins/grass/qgsgrassedit.h

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class QCloseEvent;
3737
#include "qgsmaptopixel.h"
3838
class QgsRubberBand;
3939
class QgsVertexMarker;
40+
class QgsVectorLayer;
4041
class QgsGrassEditLayer;
4142
class QgsGrassAttributes;
4243

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

155+
//! pointer to layer
156+
QgsVectorLayer *layer() { return mLayer; }
157+
154158
public slots:
155159
// TODO: once available in QGIS, use only one reciver for all signals
156160

@@ -219,10 +223,19 @@ public slots:
219223
private:
220224
//! Editing is already running
221225
static bool mRunning;
226+
227+
//! Pointer to edited layer
228+
QgsVectorLayer *mLayer;
222229

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

233+
//! Transform from layer coordinates to canvas including reprojection
234+
QgsPoint transformLayerToCanvas ( QgsPoint point);
235+
236+
//! Transform from layer coordinates to current projection
237+
QgsPoint transformLayerToMap ( QgsPoint point);
238+
226239
//! Display all lines and nodes
227240
void displayMap ();
228241

@@ -416,6 +429,9 @@ public slots:
416429
QAction *mEditAttributesAction;
417430
QAction *mCloseEditAction;
418431

432+
// Is projection enabled?
433+
bool mProjectionEnabled;
434+
419435
// Canvas items
420436
QgsRubberBand *mRubberBandLine;
421437
QgsVertexMarker *mRubberBandIcon;

src/plugins/grass/qgsgrassedittools.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
***************************************************************************/
1616

1717
#include "qgsmapcanvas.h"
18+
#include "qgsmaplayer.h"
19+
#include "qgsvectorlayer.h"
1820
#include "qgsgrassedittools.h"
1921
#include "qgsgrassedit.h"
2022
#include "qgsgrassattributes.h"
@@ -35,7 +37,7 @@ QgsGrassEditTool::QgsGrassEditTool(QgsGrassEdit* edit)
3537

3638
void QgsGrassEditTool::canvasPressEvent(QMouseEvent * event)
3739
{
38-
QgsPoint point = toMapCoords(event->pos());
40+
QgsPoint point = toLayerCoords(e->layer(), event->pos());
3941
mouseClick(point, event->button());
4042

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

5153
void QgsGrassEditTool::canvasMoveEvent(QMouseEvent * event)
5254
{
53-
QgsPoint point = toMapCoords(event->pos());
55+
QgsPoint point = toLayerCoords(e->layer(), event->pos());
5456
mouseMove(point);
5557

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

635637
if ( e->mSelectedLine == 0 )
636638
e->mSelectedLine = e->mProvider->findLine ( point.x(), point.y(), GV_LINE|GV_BOUNDARY, thresh );
637-
639+
638640
if ( e->mSelectedLine ) { // highlite, propmt
639641
e->displayElement ( e->mSelectedLine, e->mSymb[QgsGrassEdit::SYMB_HIGHLIGHT], e->mSize );
640642
e->setCanvasPropmt( QObject::tr("Delete selected / select next"), "", QObject::tr("Release selected") );

0 commit comments

Comments
 (0)