-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpoint_tool.py
More file actions
52 lines (41 loc) · 2.03 KB
/
point_tool.py
File metadata and controls
52 lines (41 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
from qgis.gui import QgsMapTool
from qgis.core import QgsRectangle
from PyQt4.QtGui import QMessageBox, QApplication
from xy_to_osgb import xy_to_osgb
from grid_ref_utils import reproject_point_to_4326
class PointTool(QgsMapTool):
def __init__(self, canvas, precision, copy_to_clipboard):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.precision = precision
self.clipboard_enable = copy_to_clipboard
def canvasReleaseEvent(self, event):
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
bbox = QgsRectangle(4999.99, 4999.69, 660000.06, 1225000.12)
if bbox.contains(point):
os_ref = xy_to_osgb(point.x(), point.y(), self.precision)
point_4326 = reproject_point_to_4326(self.canvas, point)
msg = "Grid Ref: {}\n\nLong,Lat: {:.2f}, {:.2f}\n".format(
os_ref, point_4326.x(), point_4326.y())
if self.clipboard_enable:
QApplication.clipboard().setText(os_ref)
msg += "\nCopied to clipboard"
QMessageBox.information(None, "OS Grid Reference", msg)
else:
QMessageBox.information(None, "OS Grid Reference",
"Point out of bounds")