-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigour_maps_dialog.py
197 lines (174 loc) · 7.28 KB
/
vigour_maps_dialog.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
"""
/***************************************************************************
VigourmapsDialog
A QGIS plugin
This plugin allows to obtain the biomass in rocky beach
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2020-09-23
git sha : $Format:%H$
copyright : (C) 2020 by Projeto SWAV
email : liaduarte@fc.up.pt
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
from qgis.core import *
from qgis.gui import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from qgis.core import *
from qgis.utils import *
from qgis.core import Qgis
# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'vigour_maps_dialog_base.ui'))
class VigourMapsDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(VigourMapsDialog, self).__init__(parent)
# Set up the user interface from Designer through FORM_CLASS.
# After self.setupUi() you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
self.widget.show()
self.widget.setCanvasColor(Qt.white)
self.widget.enableAntiAliasing(True)
class RectangleMapTool(QgsMapToolEmitPoint):
rect_created = pyqtSignal(QgsRectangle)
def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.rubberBand = QgsRubberBand(self.canvas, True)
self.rubberBand.setColor(Qt.red)
self.rubberBand.setWidth(1)
self.points = []
self.finished = False
self.poly_bbox = False
self.double_click_flag = False
self.reset()
def reset(self):
#self.startPoint = self.endPoint = None
#self.isEmittingPoint = False
self.rubberBand.reset(True)
self.poly_bbox = False
self.points.clear()
#new
def keyPressEvent(self,e):
#pressing escape resets the canvas. pressing enter connects the polygon
if (e.key()==16777216):
self.reset()
if (e.key()==16777220):
self.finishPolygon()
#new
def canvasDoubleClickEvent(self,e):
#finishes the polygon on double click
self.double_click_flag = True
self.finishPolygon()
# def canvasPressEvent(self, e):
# self.startPoint = self.toMapCoordinates(e.pos())
# self.endPoint = self.startPoint
# self.isEmittingPoint = True
# self.showRect(self.startPoint, self.endPoint)
def canvasReleaseEvent(self, e):
#activated when user clicks on the canvas. gets coordinates, draws them on the map and adds to the list of points
if self.double_click_flag:
self.double_click_flag = False
return
#if the finished flag is activated, the canvas will be reset for a new polygon
if self.finished:
self.reset()
self.finished = False
self.click_point = self.toMapCoordinates(e.pos())
self.rubberBand.addPoint(self.click_point, True)
self.points.append(self.click_point)
self.rubberBand.show()
def finishPolygon(self):
# Activated by user or when the map window is closed without connecting
# the polygon. Makes the polygon valid by making first and last point
# the same. This is reflected visually. Up until now the user has been
# drawing a line: a polygon is created and shown on the map
# nothing will happen if the code below has already been ran
if self.finished:
return
# connecting the polygon is valid if there's already at least 3 points
elif len(self.points)>2:
first_point = self.points[0]
self.points.append(first_point)
self.rubberBand.closePoints()
self.rubberBand.addPoint(first_point, True)
self.finished = True
# a polygon is created and added to the map for visual purposes
map_polygon = QgsGeometry.fromPolygonXY([self.points])
self.rubberBand.setToGeometry(map_polygon)
# get the bounding box of this new polygon
self.poly_bbox = self.rubberBand.asGeometry().boundingBox()
else:
self.finished = True
def getPoints(self):
# Returns list of PointXY geometries, i.e. the polygon in list form
self.rubberBand.reset(True)
return self.points
# self.isEmittingPoint = False
# r = self.rectangle()
# if r is not None:
# self.rect_created.emit(r)
#QMessageBox.about(self.dlg, "teste", str('ola'))
# print("Rectangle:", r.xMinimum(),
# r.yMinimum(), r.xMaximum(), r.yMaximum()
# )
#
# def canvasMoveEvent(self, e):
# if not self.isEmittingPoint:
# return
#
# self.endPoint = self.toMapCoordinates(e.pos())
#
# self.showRect(self.startPoint, self.endPoint)
#
# def showRect(self, startPoint, endPoint):
# self.rubberBand.reset()
# if startPoint.x() == endPoint.x() or startPoint.y() == endPoint.y():
# return
#
# point1 = QgsPointXY(startPoint.x(), startPoint.y())
# point2 = QgsPointXY(startPoint.x(), endPoint.y())
# point3 = QgsPointXY(endPoint.x(), endPoint.y())
# point4 = QgsPointXY(endPoint.x(), startPoint.y())
# point5 = point1
#
# self.rubberBand.addPoint(point1, False)
# self.rubberBand.addPoint(point2, False)
# self.rubberBand.addPoint(point3, False)
# self.rubberBand.addPoint(point4, False)
# self.rubberBand.addPoint(point5, True)
# # true to update canvas
# self.rubberBand.show()
#
#
# def rectangle(self):
# if self.startPoint is None or self.endPoint is None:
# return None
# elif (self.startPoint.x() == self.endPoint.x() or \
# self.startPoint.y() == self.endPoint.y()):
# return None
#
# return QgsRectangle(self.startPoint, self.endPoint)
#
#
# def deactivate(self):
# QgsMapTool.deactivate(self)
# self.deactivated.emit()