-
Notifications
You must be signed in to change notification settings - Fork 0
/
valuewidget.py
561 lines (466 loc) · 22.4 KB
/
valuewidget.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""
/***************************************************************************
Value Tool - A QGIS plugin to get values at the mouse pointer
-------------------
begin : 2008-08-26
copyright : (C) 2008 by G. Picard
email :
***************************************************************************/
/***************************************************************************
* *
* 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 script initializes the plugin, making it known to QGIS.
"""
import logging
# change the level back to logging.WARNING(the default) before releasing
logging.basicConfig(level=logging.DEBUG)
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from ui_valuewidgetbase import Ui_ValueWidgetBase as Ui_Widget
hasqwt=True
try:
from PyQt4.Qwt5 import QwtPlot,QwtPlotCurve,QwtScaleDiv,QwtSymbol
except:
hasqwt=False
#test if matplotlib >= 1.0
hasmpl=True
try:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
except:
hasmpl=False
if hasmpl:
if int(matplotlib.__version__[0]) < 1:
hasmpl = False
class ValueWidget(QWidget, Ui_Widget):
def __init__(self, iface):
self.hasqwt=hasqwt
self.hasmpl=hasmpl
self.layerMap=dict()
self.statsChecked=False
self.ymin=0
self.ymax=250
self.iface=iface
self.canvas=self.iface.mapCanvas()
self.legend=self.iface.legendInterface()
self.logger = logging.getLogger('.'.join((__name__,
self.__class__.__name__)))
QWidget.__init__(self)
self.setupUi(self)
self.setupUi_extra()
QObject.connect(self.cbxActive,SIGNAL("stateChanged(int)"),self.changeActive)
QObject.connect(self.cbxGraph,SIGNAL("stateChanged(int)"),self.changePage)
QObject.connect(self.canvas, SIGNAL( "keyPressed( QKeyEvent * )" ), self.pauseDisplay )
QObject.connect(self.plotSelector, SIGNAL( "currentIndexChanged ( int )" ), self.changePlot )
def setupUi_extra(self):
# checkboxes
#self.changeActive(Qt.Checked)
#set inactive by default - should save last state in user config
self.cbxActive.setCheckState(Qt.Unchecked)
# plot
self.plotSelector.setVisible( False )
self.cbxStats.setVisible( False )
self.graphControls.setVisible( False )
if self.hasqwt:
self.plotSelector.addItem( 'Qwt' )
if self.hasmpl:
self.plotSelector.addItem( 'mpl' )
self.plotSelector.setCurrentIndex( 0 );
if (not self.hasqwt or not self.hasmpl):
#self.plotSelector.setVisible(False)
self.plotSelector.setEnabled(False)
# Page 2 - qwt
if self.hasqwt:
self.qwtPlot = QwtPlot(self.stackedWidget)
self.qwtPlot.setAutoFillBackground(False)
self.qwtPlot.setObjectName("qwtPlot")
self.curve = QwtPlotCurve()
self.curve.setSymbol(
QwtSymbol(QwtSymbol.Ellipse,
QBrush(Qt.white),
QPen(Qt.red, 2),
QSize(9, 9)))
self.curve.attach(self.qwtPlot)
self.qwtPlot.setVisible(False)
else:
self.qwtPlot = QtGui.QLabel("Need Qwt >= 5.0 or matplotlib >= 1.0 !")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.qwtPlot.sizePolicy().hasHeightForWidth())
self.qwtPlot.setSizePolicy(sizePolicy)
self.qwtPlot.setObjectName("qwtPlot")
self.qwtPlot.updateGeometry()
self.stackedWidget.addWidget(self.qwtPlot)
#Page 3 - matplotlib
self.mplLine = None #make sure to invalidate when layers change
if self.hasmpl:
# mpl stuff
# should make figure light gray
self.mplBackground = None #http://www.scipy.org/Cookbook/Matplotlib/Animations
self.mplFig = plt.Figure(facecolor='w', edgecolor='w')
self.mplFig.subplots_adjust(left=0.1, right=0.975, bottom=0.13, top=0.95)
self.mplPlt = self.mplFig.add_subplot(111)
self.mplPlt.tick_params(axis='both', which='major', labelsize=12)
self.mplPlt.tick_params(axis='both', which='minor', labelsize=10)
# qt stuff
self.pltCanvas = FigureCanvasQTAgg(self.mplFig)
self.pltCanvas.setParent(self.stackedWidget)
self.pltCanvas.setAutoFillBackground(False)
self.pltCanvas.setObjectName("mplPlot")
self.mplPlot = self.pltCanvas
self.mplPlot.setVisible(False)
else:
self.mplPlot = QtGui.QLabel("Need Qwt >= 5.0 or matplotlib >= 1.0 !")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.mplPlot.sizePolicy().hasHeightForWidth())
self.mplPlot.setSizePolicy(sizePolicy)
self.qwtPlot.setObjectName("qwtPlot")
self.mplPlot.updateGeometry()
self.stackedWidget.addWidget(self.mplPlot)
self.stackedWidget.setCurrentIndex(0)
def disconnect(self):
self.changeActive(False)
QObject.disconnect(self.canvas, SIGNAL( "keyPressed( QKeyEvent * )" ), self.pauseDisplay )
def pauseDisplay(self,e):
if ( e.modifiers() == Qt.ShiftModifier or e.modifiers() == Qt.MetaModifier ) and e.key() == Qt.Key_A:
self.cbxActive.toggle()
return True
return False
def keyPressEvent( self, e ):
if ( e.modifiers() == Qt.ControlModifier or e.modifiers() == Qt.MetaModifier ) and e.key() == Qt.Key_C:
items = QString()
for rec in range( self.tableWidget.rowCount() ):
items.append( '"' + self.tableWidget.item( rec, 0 ).text() + '",' + self.tableWidget.item( rec, 1 ).text() + "\n" )
if not items.isEmpty():
clipboard = QApplication.clipboard()
clipboard.setText( items )
elif (self.pauseDisplay(e)):
pass
else:
QWidget.keyPressEvent( self, e )
def changePage(self,state):
if (state==Qt.Checked):
self.plotSelector.setVisible( True )
self.cbxStats.setVisible( True )
self.graphControls.setVisible( True )
if (self.plotSelector.currentText()=='mpl'):
self.stackedWidget.setCurrentIndex(2)
else:
self.stackedWidget.setCurrentIndex(1)
else:
self.plotSelector.setVisible( False )
self.cbxStats.setVisible( False )
self.graphControls.setVisible( False )
self.stackedWidget.setCurrentIndex(0)
def changePlot(self):
self.changePage(self.cbxActive.checkState())
def changeActive(self,state):
if (state==Qt.Checked):
#QObject.connect(self.legend, SIGNAL( "itemAdded ( QModelIndex )" ), self.statsNeedChecked )
#QObject.connect(self.legend, SIGNAL( "itemRemoved ()" ), self.invalidatePlot )
QObject.connect(self.canvas, SIGNAL( "layersChanged ()" ), self.invalidatePlot )
if int(QGis.QGIS_VERSION[2]) > 2: # for QGIS >= 1.3
QObject.connect(self.canvas, SIGNAL("xyCoordinates(const QgsPoint &)"), self.printValue)
else:
QObject.connect(self.canvas, SIGNAL("xyCoordinates(QgsPoint &)"), self.printValue)
else:
QObject.disconnect(self.canvas, SIGNAL( "layersChanged ()" ), self.invalidatePlot )
if int(QGis.QGIS_VERSION[2]) > 2: # for QGIS >= 1.3
QObject.disconnect(self.canvas, SIGNAL("xyCoordinates(const QgsPoint &)"), self.printValue)
else:
QObject.disconnect(self.canvas, SIGNAL("xyCoordinates(QgsPoint &)"), self.printValue)
def printValue(self,position):
if self.canvas.layerCount() == 0:
self.values=[]
self.showValues()
return
needextremum = self.cbxGraph.isChecked() # if plot is checked
# count the number of requires rows and remember the raster layers
nrow=0
rasterlayers=[]
layersWOStatistics=[]
for i in range(self.canvas.layerCount()):
layer = self.canvas.layer(i)
if (layer!=None and layer.isValid() and layer.type()==QgsMapLayer.RasterLayer):
if QGis.QGIS_VERSION_INT >= 10900: # for QGIS >= 1.9
if not layer.dataProvider():
continue
if not layer.dataProvider().capabilities() & QgsRasterDataProvider.IdentifyValue:
continue
nrow+=layer.bandCount()
rasterlayers.append(layer)
else: # < 1.9
if layer.providerKey()=="wms":
continue
if layer.providerKey()=="grassraster":
nrow+=1
rasterlayers.append(layer)
else: # normal raster layer
nrow+=layer.bandCount()
rasterlayers.append(layer)
# check statistics for each band
if needextremum:
for i in range( 1,layer.bandCount()+1 ):
if int(QGis.QGIS_VERSION[2]) > 8: # for QGIS > 1.8
has_stats=layer.dataProvider().hasStatistics(i)
else:
has_stats=layer.hasStatistics(i)
if not layer.id() in self.layerMap and not has_stats\
and not layer in layersWOStatistics:
layersWOStatistics.append(layer)
if layersWOStatistics and not self.statsChecked:
self.calculateStatistics(layersWOStatistics)
# create the row if necessary
self.tableWidget.setRowCount(nrow)
irow=0
self.values=[]
self.ymin=1e38
self.ymax=-1e38
mapCanvasSrs = self.iface.mapCanvas().mapRenderer().destinationSrs()
# TODO - calculate the min/max values only once, instead of every time!!!
# keep them in a dict() with key=layer.id()
for layer in rasterlayers:
layername=unicode(layer.name())
layerSrs = layer.srs()
pos = position
# if given no position, get dummy values
if position is None:
pos = QgsPoint(0,0)
# transform points if needed
elif not mapCanvasSrs == layerSrs and self.iface.mapCanvas().hasCrsTransformEnabled():
srsTransform = QgsCoordinateTransform(mapCanvasSrs, layerSrs)
try:
pos = srsTransform.transform(position)
except QgsCsException, err:
# ignore transformation errors
continue
if QGis.QGIS_VERSION_INT >= 10900: # for QGIS >= 1.9
if not layer.dataProvider():
continue
ident = None
if position is not None:
canvas = self.iface.mapCanvas()
# we can only use context if layer is not projected
if canvas.hasCrsTransformEnabled() and layer.dataProvider().crs() != canvas.mapRenderer().destinationCrs():
ident = layer.dataProvider().identify(pos, QgsRasterDataProvider.IdentifyFormatValue )
else:
extent = canvas.extent()
width = round(extent.width() / canvas.mapUnitsPerPixel());
height = round(extent.height() / canvas.mapUnitsPerPixel());
extent = canvas.mapRenderer().mapToLayerCoordinates( layer, extent );
ident = layer.dataProvider().identify(pos, QgsRasterDataProvider.IdentifyFormatValue, canvas.extent(), width, height )
if not len( ident ) > 0:
continue
# if given no position, set values to 0
if position is None:
for key in ident.iterkeys():
ident[key] = layer.dataProvider().noDataValue(key)
for iband in range(1,layer.bandCount()+1): # loop over the bands
layernamewithband=layername
if len(ident)>1:
layernamewithband+=' '+layer.bandName(iband)
if not ident or not ident.has_key( iband ): # should not happen
bandvalue = "?"
else:
doubleValue = ident[iband].toDouble()[0]
if layer.dataProvider().isNoDataValue ( iband, doubleValue ):
bandvalue = "no data"
else:
bandvalue = QgsRasterBlock.printValue( doubleValue )
self.values.append((layernamewithband,bandvalue))
if needextremum:
has_stats=layer.hasStatistics(i)
if has_stats:
cstr=layer.bandStatistics(iband)
if has_stats:
self.ymin=min(self.ymin,cstr.minimumValue)
self.ymax=max(self.ymax,cstr.maximumValue)
else:
self.ymin=min(self.ymin,layer.minimumValue(i))
self.ymax=max(self.ymax,layer.maximumValue(i))
else: # QGIS < 1.9
isok,ident = layer.identify(pos)
if not isok:
continue
# if given no position, set values to 0
if position is None:
for key in ident.iterkeys():
ident[key] = 0
if layer.providerKey()=="grassraster":
if not ident.has_key(QString("value")):
continue
cstr = ident[QString("value")]
if cstr.isNull():
continue
value = cstr.toDouble()
if not value[1]:
# if this is not a double, it is probably a (GRASS string like
# 'out of extent' or 'null (no data)'. Let's just show that:
self.values.append((layername, cstr))
continue
self.values.append((layername,cstr))
if needextremum:
self.ymin = min(self.ymin,value[0])
self.ymax = max(self.ymax,value[0])
else:
for iband in range(1,layer.bandCount()+1): # loop over the bands
bandvalue=ident[layer.bandName(iband)]
layernamewithband=layername
if len(ident)>1:
layernamewithband+=' '+layer.bandName(iband)
self.values.append((layernamewithband,bandvalue))
if needextremum:
if int(QGis.QGIS_VERSION[2]) > 8: # for QGIS > 1.8
has_stats=layer.dataProvider().hasStatistics(i)
if has_stats:
cstr=layer.dataProvider().bandStatistics(iband)
else:
has_stats=layer.hasStatistics(i)
if has_stats:
cstr=layer.bandStatistics(iband)
if has_stats:
self.ymin=min(self.ymin,cstr.minimumValue)
self.ymax=max(self.ymax,cstr.maximumValue)
else:
self.ymin=min(self.ymin,layer.minimumValue(i))
self.ymax=max(self.ymax,layer.maximumValue(i))
self.showValues()
def showValues(self):
if self.cbxGraph.isChecked():
#TODO don't plot if there is no data to plot...
self.plot()
else:
self.printInTable()
def calculateStatistics(self,layersWOStatistics):
self.invalidatePlot(False)
self.statsChecked = True
layerNames = QStringList()
for layer in layersWOStatistics:
if not layer.id() in self.layerMap:
layerNames << layer.name()
if ( len(layerNames) != 0 ):
# res = QMessageBox.warning( self, self.tr( 'Warning' ),
# self.tr( 'There are no statistics in the following rasters:\n%1\n\nCalculate?' ).arg(layerNames.join('\n')),
# QMessageBox.Yes | QMessageBox.No )
# if res != QMessageBox.Yes:
if not self.cbxStats.isChecked():
#self.cbxActive.setCheckState(Qt.Unchecked)
for layer in layersWOStatistics:
self.layerMap[layer.id()] = True
return
else:
print('ERROR, no layers to get stats for')
save_state=self.cbxActive.isChecked()
self.changeActive(Qt.Unchecked) # deactivate
# calculate statistics
for layer in layersWOStatistics:
if not layer.id() in self.layerMap:
self.layerMap[layer.id()] = True
for i in range( 1,layer.bandCount()+1 ):
if int(QGis.QGIS_VERSION[2]) > 8: # for QGIS > 1.8
stat = layer.dataProvider().bandStatistics(i)
else:
stat = layer.bandStatistics(i)
if save_state:
self.changeActive(Qt.Checked) # activate if necessary
def printInTable(self):
irow=0
for row in self.values:
layername,value=row
if (self.tableWidget.item(irow,0)==None):
# create the item
self.tableWidget.setItem(irow,0,QTableWidgetItem())
self.tableWidget.setItem(irow,1,QTableWidgetItem())
self.tableWidget.item(irow,0).setText(layername)
self.tableWidget.item(irow,1).setText(value)
irow+=1
def plot(self):
numvalues=[]
if ( self.hasqwt or self.hasmpl ):
for row in self.values:
layername,value=row
try:
numvalues.append(float(value))
except:
numvalues.append(0)
ymin = self.ymin
ymax = self.ymax
if self.leYMin.text() != '' and self.leYMax.text() != '':
ymin = float(self.leYMin.text())
ymax = float(self.leYMax.text())
if ( self.hasqwt and (self.plotSelector.currentText()=='Qwt') ):
self.qwtPlot.setAxisMaxMinor(QwtPlot.xBottom,0)
#self.qwtPlot.setAxisMaxMajor(QwtPlot.xBottom,0)
self.qwtPlot.setAxisScale(QwtPlot.xBottom,1,len(self.values))
#self.qwtPlot.setAxisScale(QwtPlot.yLeft,self.ymin,self.ymax)
self.qwtPlot.setAxisScale(QwtPlot.yLeft,ymin,ymax)
self.curve.setData(range(1,len(numvalues)+1), numvalues)
self.qwtPlot.replot()
self.qwtPlot.setVisible(len(numvalues)>0)
elif ( self.hasmpl and (self.plotSelector.currentText()=='mpl') ):
self.mplPlt.clear()
self.mplPlt.plot(range(1,len(numvalues)+1), numvalues, marker='o', color='k', mfc='b', mec='b')
self.mplPlt.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
self.mplPlt.yaxis.set_minor_locator(ticker.AutoMinorLocator())
self.mplPlt.set_xlim( (1-0.25,len(self.values)+0.25 ) )
self.mplPlt.set_ylim( (ymin, ymax) )
self.mplFig.canvas.draw()
#disable optimizations - too many bugs, depending on mpl version!
# if self.mplLine is None:
# self.mplPlt.clear()
# self.mplLine, = self.mplPlt.plot(range(1,len(numvalues)+1), numvalues, marker='o', color='k', mfc='b', mec='b', animated=True)
# self.mplPlt.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
# self.mplPlt.yaxis.set_minor_locator(ticker.AutoMinorLocator())
# self.mplPlt.set_xlim( (1-0.25,len(self.values)+0.25 ) )
# self.mplPlt.set_ylim( (self.ymin, self.ymax) )
# self.mplFig.canvas.draw()
# self.mplBackground = self.mplFig.canvas.copy_from_bbox(self.mplFig.bbox)
# else:
# # restore the clean slate background
# self.mplFig.canvas.restore_region(self.mplBackground)
# # update the data
# self.mplLine.set_xdata(range(1,len(numvalues)+1))
# self.mplLine.set_ydata(numvalues)
# self.mplPlt.draw_artist(self.mplLine)
# # just redraw the axes rectangle
# self.mplFig.canvas.blit(self.mplFig.bbox)
# self.mplPlot.setVisible(len(numvalues)>0)
#try:
# attr = float(ident[j])
#except:
# attr = 0
# print "Null cell value catched as zero!" # For none values, profile height = 0. It's not elegant...
#nr = rastLayer.getRasterBandNumber(self.rastItems[field[1]][field[2]][0])
#print ident
#for j in ident:
#print j
#if j.right(1) == str(nr):
#attr = int(ident[j])
#attr = float(ident[j]) ##### I MUST IMPLEMENT RASTER TYPE HANDLING!!!!
#outFeat.addAttribute(i, QVariant(attr))
def statsNeedChecked(self, indx):
#self.statsChecked = False
self.invalidatePlot()
def invalidatePlot(self,replot=True):
self.statsChecked = False
if self.mplLine is not None:
del self.mplLine
self.mplLine = None
#update empty plot
if replot and self.cbxGraph.isChecked():
#self.values=[]
self.printValue( None )
def resizeEvent(self, event):
self.invalidatePlot()