-
Notifications
You must be signed in to change notification settings - Fork 3
/
coordinate_capture_map_tool.py
69 lines (57 loc) · 3.25 KB
/
coordinate_capture_map_tool.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
/***************************************************************************
CoordinateCaptureMapTool
A QGIS plugin
Python port of the deprecated Coordinate Capture core plugin
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2020-07-04
git sha : $Format:%H$
copyright : (C) 2020 by Stefanos Natsis
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import pyqtSignal, Qt
from qgis.gui import QgsMapToolEmitPoint, QgsRubberBand
from qgis.core import QgsWkbTypes, QgsPointXY, QgsApplication
class CoordinateCaptureMapTool(QgsMapToolEmitPoint):
mouseMoved = pyqtSignal(QgsPointXY)
mouseClicked = pyqtSignal(QgsPointXY)
def __init__(self, canvas):
super(CoordinateCaptureMapTool, self).__init__(canvas)
self.mapCanvas = canvas
self.rubberBand = QgsRubberBand(self.mapCanvas, QgsWkbTypes.PolygonGeometry)
self.rubberBand.setColor(Qt.red)
self.rubberBand.setWidth(1)
self.setCursor(QgsApplication.getThemeCursor(QgsApplication.Cursor.CrossHair))
def canvasMoveEvent(self, e):
originalPoint = QgsPointXY(self.mapCanvas.getCoordinateTransform().toMapCoordinates(e.x(), e.y()))
self.mouseMoved.emit(originalPoint)
def canvasPressEvent(self, e):
if e.button() == Qt.LeftButton:
originalPoint = QgsPointXY(self.mapCanvas.getCoordinateTransform().toMapCoordinates(e.x(), e.y()))
self.mouseClicked.emit(originalPoint)
point1 = QgsPointXY(self.mapCanvas.getCoordinateTransform().toMapCoordinates(e.x() - 1, e.y() - 1))
point2 = QgsPointXY(self.mapCanvas.getCoordinateTransform().toMapCoordinates(e.x() + 1, e.y() - 1))
point3 = QgsPointXY(self.mapCanvas.getCoordinateTransform().toMapCoordinates(e.x() + 1, e.y() + 1))
point4 = QgsPointXY(self.mapCanvas.getCoordinateTransform().toMapCoordinates(e.x() - 1, e.y() + 1))
self.rubberBand.reset(QgsWkbTypes.PolygonGeometry )
self.rubberBand.addPoint(point1, False)
self.rubberBand.addPoint(point2, False)
self.rubberBand.addPoint(point3, False)
self.rubberBand.addPoint(point4, True)
self.rubberBand.show()
elif e.button() == Qt.RightButton:
self.deactivate()
def deactivate(self):
self.rubberBand.reset(QgsWkbTypes.LineGeometry)
super(CoordinateCaptureMapTool, self).deactivate()