-
Notifications
You must be signed in to change notification settings - Fork 5
/
ColorClassificationMap.py
51 lines (34 loc) · 1.43 KB
/
ColorClassificationMap.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
from PyQt5.QtCore import *
from PyQt5.QtGui import *
layer = iface.activeLayer()
renderer = layer.renderer()
provider = layer.dataProvider()
extent = layer.extent()
ver = provider.hasStatistics(1, QgsRasterBandStats.All)
stats = provider.bandStatistics(1, QgsRasterBandStats.All,extent, 0)
if ver is not False:
print("minimumValue = ", stats.minimumValue)
print("maximumValue = ", stats.maximumValue)
if (stats.minimumValue < 0):
min = 0
else:
min= stats.minimumValue
max = stats.maximumValue
range = max - min
add = range//2
interval = min + add
colDic = {'red':'#ff0000', 'yellow':'#ffff00','blue':'#0000ff'}
valueList =[min, interval, max]
lst = [ QgsColorRampShader.ColorRampItem(valueList[0], QColor(colDic['red'])),
QgsColorRampShader.ColorRampItem(valueList[1], QColor(colDic['yellow'])),
QgsColorRampShader.ColorRampItem(valueList[2], QColor(colDic['blue']))]
myRasterShader = QgsRasterShader()
myColorRamp = QgsColorRampShader()
myColorRamp.setColorRampItemList(lst)
myColorRamp.setClassificationMode(1) # this line doesn't work
myRasterShader.setRasterShaderFunction(myColorRamp)
myPseudoRenderer = QgsSingleBandPseudoColorRenderer(layer.dataProvider(),
layer.type(),
myRasterShader)
layer.setRenderer(myPseudoRenderer)
layer.triggerRepaint()