forked from etiennesky/profiletool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profileplugin.py
321 lines (275 loc) · 12.6 KB
/
profileplugin.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# -*- coding: utf-8 -*-
#-----------------------------------------------------------
#
# Profile
# Copyright (C) 2008 Borys Jurgiel
# Copyright (C) 2012 Patrice Verchere
#-----------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# 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 progsram; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#---------------------------------------------------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import resources
from ui.ui_ptdockwidget import Ui_PTDockWidget
from tools.doprofile import DoProfile
from tools.ptmaptool import ProfiletoolMapTool
from tools.tableviewtool import TableViewTool
from tools.selectlinetool import SelectLineTool
from tools.plottingtool import PlottingTool
from tools.utils import isProfilable
class ProfilePlugin:
def __init__(self, iface):
self.iface = iface
self.canvas = iface.mapCanvas()
self.wdg = None
self.tool = None
self.lastFreeHandPoints = []
def initGui(self):
# create action
self.action = QAction(QIcon(":/plugins/profiletool/icons/profileIcon.png"), "Terrain profile", self.iface.mainWindow())
self.action.setWhatsThis("Plots terrain profiles")
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
self.aboutAction = QAction("About", self.iface.mainWindow())
QObject.connect(self.aboutAction, SIGNAL("triggered()"), self.about)
# add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("&Profile Tool", self.action)
self.iface.addPluginToMenu("&Profile Tool", self.aboutAction)
#Init classe variables
self.tool = ProfiletoolMapTool(self.iface.mapCanvas(),self.action) #the mouselistener
self.dockOpened = False #remember for not reopening dock if there's already one opened
self.pointstoDraw = None #Polyline in mapcanvas CRS analysed
self.dblclktemp = None #enable disctinction between leftclick and doubleclick
self.mdl = None #the model whitch in are saved layers analysed caracteristics
self.selectionmethod = 0 #The selection method defined in option
self.saveTool = self.canvas.mapTool() #Save the standard mapttool for restoring it at the end
self.layerindex = None #for selection mode
self.previousLayer = None #for selection mode
self.plotlibrary = None #The plotting library to use
self.textquit0 = "Click for polyline and double click to end (right click to cancel then quit)"
self.textquit1 = "Select the polyline in a vector layer (Right click to quit)"
def unload(self):
if not self.wdg is None:
self.wdg.close()
self.iface.removeToolBarIcon(self.action)
self.iface.removePluginMenu("&Profile Tool", self.action)
self.iface.removePluginMenu("&Profile Tool", self.aboutAction)
def run(self):
# first, check posibility
if self.checkIfOpening() == False:
return
#if dock not already opened, open the dock and all the necessary thing (model,doProfile...)
if self.dockOpened == False:
self.mdl = QStandardItemModel(0, 5)
self.wdg = Ui_PTDockWidget(self.iface.mainWindow(), self.iface, self.mdl)
self.wdg.showIt()
self.doprofile = DoProfile(self.iface,self.wdg,self.tool)
self.tableViewTool = TableViewTool()
QObject.connect(self.wdg, SIGNAL( "closed(PyQt_PyObject)" ), self.cleaning2)
QObject.connect(self.wdg.tableView,SIGNAL("clicked(QModelIndex)"), self._onClick)
QObject.connect(self.wdg.pushButton_2, SIGNAL("clicked()"), self.addLayer)
QObject.connect(self.wdg.pushButton, SIGNAL("clicked()"), self.removeLayer)
QObject.connect(self.wdg.comboBox, SIGNAL("currentIndexChanged(int)"), self.selectionMethod)
QObject.connect(self.wdg.comboBox_2, SIGNAL("currentIndexChanged(int)"), self.changePlotLibrary)
self.tableViewTool.layerAddedOrRemoved.connect(self.refreshPlot)
self.wdg.addOptionComboboxItems()
self.addLayer()
self.dockOpened = True
#Listeners of mouse
self.connectTool()
#init the mouse listener comportement and save the classic to restore it on quit
self.canvas.setMapTool(self.tool)
#init the temp layer where the polyline is draw
self.polygon = False
self.rubberband = QgsRubberBand(self.canvas, self.polygon)
self.rubberband.setWidth(2)
self.rubberband.setColor(QColor(Qt.red))
#init the table where is saved the poyline
self.pointstoDraw = []
self.pointstoCal = []
self.lastClicked = [[-9999999999.9,9999999999.9]]
# The last valid line we drew to create a free-hand profile
self.lastFreeHandPoints = []
#Help about what doing
if self.selectionmethod == 0:
self.iface.mainWindow().statusBar().showMessage(self.textquit0)
elif self.selectionmethod == 1:
self.iface.mainWindow().statusBar().showMessage(self.textquit1)
#************************************* Mouse listener actions ***********************************************
def moved(self,position): #draw the polyline on the temp layer (rubberband)
if self.selectionmethod == 0:
if len(self.pointstoDraw) > 0:
#Get mouse coords
mapPos = self.canvas.getCoordinateTransform().toMapCoordinates(position["x"],position["y"])
#Draw on temp layer
if QGis.QGIS_VERSION_INT >= 10900:
self.rubberband.reset(QGis.Line)
else:
self.rubberband.reset(self.polygon)
for i in range(0,len(self.pointstoDraw)):
self.rubberband.addPoint(QgsPoint(self.pointstoDraw[i][0],self.pointstoDraw[i][1]))
self.rubberband.addPoint(QgsPoint(mapPos.x(),mapPos.y()))
if self.selectionmethod == 1:
return
def rightClicked(self,position): #used to quit the current action
if self.selectionmethod == 0:
mapPos = self.canvas.getCoordinateTransform().toMapCoordinates(position["x"],position["y"])
newPoints = [[mapPos.x(), mapPos.y()]]
#if newPoints == self.lastClicked: return # sometimes a strange "double click" is given
if len(self.pointstoDraw) > 0:
self.pointstoDraw = []
self.pointstoCal = []
self.rubberband.reset(self.polygon)
else:
self.cleaning()
if self.selectionmethod == 1:
try:
self.previousLayer.removeSelection( False )
except:
self.iface.mainWindow().statusBar().showMessage("error right click")
self.cleaning()
def leftClicked(self,position): #Add point to analyse
mapPos = self.canvas.getCoordinateTransform().toMapCoordinates(position["x"],position["y"])
newPoints = [[mapPos.x(), mapPos.y()]]
if self.selectionmethod == 0:
if newPoints == self.dblclktemp:
self.dblclktemp = None
return
else :
if len(self.pointstoDraw) == 0:
self.rubberband.reset(self.polygon)
self.pointstoDraw += newPoints
if self.selectionmethod == 1:
result = SelectLineTool().getPointTableFromSelectedLine(self.iface, self.tool, newPoints, self.layerindex, self.previousLayer , self.pointstoDraw)
self.pointstoDraw = result[0]
self.layerindex = result[1]
self.previousLayer = result[2]
self.doprofile.calculateProfil(self.pointstoDraw, self.mdl,self.plotlibrary, False)
self.pointstoDraw = []
self.iface.mainWindow().statusBar().showMessage(self.textquit1)
def doubleClicked(self,position):
if self.selectionmethod == 0:
#Validation of line
mapPos = self.canvas.getCoordinateTransform().toMapCoordinates(position["x"],position["y"])
newPoints = [[mapPos.x(), mapPos.y()]]
self.pointstoDraw += newPoints
#launch analyses
self.iface.mainWindow().statusBar().showMessage(str(self.pointstoDraw))
self.doprofile.calculateProfil(self.pointstoDraw,self.mdl, self.plotlibrary)
#Reset
self.lastFreeHandPoints = self.pointstoDraw
self.pointstoDraw = []
#temp point to distinct leftclick and dbleclick
self.dblclktemp = newPoints
self.iface.mainWindow().statusBar().showMessage(self.textquit0)
if self.selectionmethod == 1:
return
#***************************** open and quit options *******************************************
def checkIfOpening(self):
ver = str(QGis.QGIS_VERSION)
if ver[0] == "0" and ((ver[2] != "1") or (ver[3] != "1")): #Check qgis version
QMessageBox.warning(self.iface.mainWindow(), "Profile tool", "Quantum GIS version detected: "+ver+"\nProfile plugin requires version at least 0.11")
return False
if self.iface.mapCanvas().layerCount() == 0: #Check a layer is opened
QMessageBox.warning(self.iface.mainWindow(), "Profile", "First open any raster layer, please")
return False
layer = self.iface.activeLayer()
if layer == None or not isProfilable(layer) : #Check if a raster layer is opened and selectionned
if self.mdl == None:
QMessageBox.warning(self.iface.mainWindow(), "Profile Tool", "Please select one raster layer")
return False
if self.mdl.rowCount() == 0:
QMessageBox.warning(self.iface.mainWindow(), "Profile Tool", "Please select one raster layer")
return False
return True
def connectTool(self):
QObject.connect(self.tool, SIGNAL("moved"), self.moved)
QObject.connect(self.tool, SIGNAL("rightClicked"), self.rightClicked)
QObject.connect(self.tool, SIGNAL("leftClicked"), self.leftClicked)
QObject.connect(self.tool, SIGNAL("doubleClicked"), self.doubleClicked)
QObject.connect(self.tool, SIGNAL("deactivate"), self.deactivate)
def deactivate(self): #enable clean exit of the plugin
QObject.disconnect(self.tool, SIGNAL("moved"), self.moved)
QObject.disconnect(self.tool, SIGNAL("leftClicked"), self.leftClicked)
QObject.disconnect(self.tool, SIGNAL("rightClicked"), self.rightClicked)
QObject.disconnect(self.tool, SIGNAL("doubleClicked"), self.doubleClicked)
self.rubberband.reset(self.polygon)
self.iface.mainWindow().statusBar().showMessage("")
def cleaning(self): #used on right click
try:
#print str(self.previousLayer)
self.previousLayer.removeSelection(False)
#self.previousLayer.select(None)
except:
pass
self.canvas.unsetMapTool(self.tool)
self.canvas.setMapTool(self.saveTool)
#self.rubberband.reset(self.polygon)
self.iface.mainWindow().statusBar().showMessage( "" )
def cleaning2(self): #used when Dock dialog is closed
QObject.disconnect(self.wdg.tableView,SIGNAL("clicked(QModelIndex)"), self._onClick)
QObject.disconnect(self.wdg.comboBox, SIGNAL("currentIndexChanged(int)"), self.selectionMethod)
self.tableViewTool.layerAddedOrRemoved.disconnect(self.refreshPlot)
self.mdl = None
self.dockOpened = False
self.cleaning()
self.wdg = None
#***************************** Options *******************************************
def selectionMethod(self,item):
if item == 0:
self.selectionmethod = 0
self.tool.setCursor(Qt.CrossCursor)
elif item == 1:
self.selectionmethod = 1
self.tool.setCursor(Qt.PointingHandCursor)
self.pointstoDraw = []
self.pointstoCal = []
self.rubberband.reset(self.polygon)
if self.canvas.mapTool() == self.tool:
self.canvas.setMapTool(self.tool)
self.connectTool()
if self.selectionmethod == 0:
self.iface.mainWindow().statusBar().showMessage(self.textquit0)
elif self.selectionmethod == 1:
self.iface.mainWindow().statusBar().showMessage(self.textquit1)
def changePlotLibrary(self, item):
self.plotlibrary = self.wdg.comboBox_2.itemText(item)
self.wdg.addPlotWidget(self.plotlibrary)
#************************* tableview function ******************************************
def addLayer(self, layer1 = None):
if layer1 is None:
layer1 = self.iface.activeLayer()
self.tableViewTool.addLayer(self.iface, self.mdl, layer1)
def _onClick(self,index1): #action when clicking the tableview
self.tableViewTool.onClick(self.iface, self.wdg, self.mdl, self.plotlibrary, index1)
def removeLayer(self):
self.tableViewTool.removeLayer(self.iface, self.mdl)
def about(self):
from ui.ui_dlgabout import DlgAbout
DlgAbout(self.iface.mainWindow()).exec_()
def refreshPlot(self):
"""
Refreshes/updates the plot without requiring the user to
redraw the plot line (rubberband)
"""
if self.selectionmethod == 0:
if len(self.lastFreeHandPoints) > 1:
self.doprofile.calculateProfil(self.lastFreeHandPoints, self.mdl, self.plotlibrary)