Skip to content

Commit 534abc8

Browse files
author
wonder
committed
Added utility function toLayerCoords
git-svn-id: http://svn.osgeo.org/qgis/trunk@5192 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 50ced72 commit 534abc8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/gui/qgsmaptool.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
/* $Id$ */
1616

1717
#include "qgsmaptool.h"
18+
#include "qgsmaplayer.h"
1819
#include "qgsmapcanvas.h"
1920
#include "qgsmaptopixel.h"
21+
#include "qgsproject.h"
2022
#include <QAction>
2123

2224
QgsMapTool::QgsMapTool(QgsMapCanvas* canvas)
@@ -36,6 +38,30 @@ QgsPoint QgsMapTool::toMapCoords(const QPoint& point)
3638
}
3739

3840

41+
QgsPoint QgsMapTool::toLayerCoords(QgsMapLayer* layer, const QPoint& point)
42+
{
43+
// FIXME: this information should be accessible elsewhere without accessing QgsProject!
44+
bool projectionsEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0);
45+
46+
if (projectionsEnabled)
47+
{
48+
49+
if (!layer || !layer->coordinateTransform())
50+
return QgsPoint(0,0);
51+
52+
// first transform from canvas coords to map coordinates
53+
QgsPoint pnt = toMapCoords(point);
54+
55+
// then convert from
56+
return layer->coordinateTransform()->transform(pnt, QgsCoordinateTransform::INVERSE);
57+
}
58+
else
59+
{
60+
return toMapCoords(point);
61+
}
62+
}
63+
64+
3965
QPoint QgsMapTool::toCanvasCoords(const QgsPoint& point)
4066
{
4167
double x = point.x(), y = point.y();

src/gui/qgsmaptool.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QCursor>
2121
#include <QString>
2222

23+
class QgsMapLayer;
2324
class QgsMapCanvas;
2425
class QMouseEvent;
2526
class QgsPoint;
@@ -74,6 +75,9 @@ class QgsMapTool
7475
//! transformation from screen coordinates to map coordinates
7576
QgsPoint toMapCoords(const QPoint& point);
7677

78+
//! transformation from screen coordinates to layer's coordinates
79+
QgsPoint toLayerCoords(QgsMapLayer* layer, const QPoint& point);
80+
7781
//! transformation from map coordinates to screen coordinates
7882
QPoint toCanvasCoords(const QgsPoint& point);
7983

0 commit comments

Comments
 (0)