Newer
Older
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
import os
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as np
from timeseriesviewer import *
from timeseriesviewer.utils import *
from timeseriesviewer.ui.widgets import loadUIFormClass
load = lambda p : loadUIFormClass(jp(DIR_UI,p))
class ClassInfo(QObject):
def __init__(self, name=None, color=None):
self.mName = ''
self.mColor = QColor('black')
if name:
self.setName(name)
if color:
self.setColor(color)
def setColor(self, color):
assert isinstance(color, QColor)
self.mColor = color
def setName(self, name):
assert isinstance(name, str)
self.mName = name
def clone(self):
return ClassInfo(name=self.mName, color=self.mColor)
class ClassificationScheme(QObject):
@staticmethod
def fromRasterImage(path, bandIndex=None):
ds = gdal.Open(path)
assert ds is not None
if bandIndex is None:
for b in range(ds.RasterCount):
band = ds.GetRasterBand(b + 1)
cat = band.GetCategoryNames()
if cat != None:
bandIndex = b
break
s = ""
assert bandIndex >= 0 and bandIndex < ds.RasterCount
band = ds.GetRasterBand(bandIndex + 1)
cat = band.GetCategoryNames()
ct = band.GetColorTable()
if len(cat) == 0:
return None
scheme = ClassificationScheme()
for i, catName in enumerate(cat):
cli = ClassInfo(name=catName)
if ct is not None:
cli.setColor(QColor(*ct.GetColorEntry(i)))
scheme.addClass(cli)
return scheme
@staticmethod
def fromVectorFile(self, path, fieldClassName='classname', fieldClassColor='classColor'):
pass
def clear(self):
del self.classes[:]
def __init__(self):
super(ClassificationScheme, self).__init__()
self.classes = []
def __len__(self):
return len(self.classes)
def __iter__(self):
return self.classes.__iter__()
def removeClass(self, c):
assert c in self.classes
def addClass(self, c, index=None):
assert isinstance(c, ClassInfo)
if index is None:
index = len(self.classes)
self.classes.insert(index, c)
class ClassificationSchemeTableModel(QAbstractTableModel):
columnNames = ['label', 'name', 'color']
def __init__(self, parent=None):
super(ClassificationSchemeTableModel, self).__init__(parent)
self.scheme = ClassificationScheme()
def loadClassesFromImage(self, path, append=True):
if not append:
for c in self.classes:
self.removeClass(c)
def rowCount(self, QModelIndex_parent=None, *args, **kwargs):
return len(self.scheme)
def columnCount(self, parent = QModelIndex()):
return len(self.columNames)
def getIndexFromClassInfo(self, classInfo):
return self.createIndex(self.scheme.index(classInfo),0)
def getClassInfoFromIndex(self, index):
if index.isValid():
return self.scheme[index.row()]
return None
def data(self, index, role=Qt.DisplayRole):
if role is None or not index.isValid():
return None
columnName = self.columnames[index.column()]
classInfo = self.getClassInfoFromIndex(index)
assert isinstance(classInfo, ClassInfo)
value = None
if role == Qt.DisplayRole:
if columnName == 'id':
value = index.row()
if columnName == 'name':
value = classInfo.mName
elif columnName == 'color':
value = str(classInfo.mColor)
return value
def setData(self, index, value, role=None):
if role is None or not index.isValid():
return None
columnName = self.columnames[index.column()]
classInfo = self.getClassInfoFromIndex(index)
assert isinstance(classInfo, ClassInfo)
if role == Qt.EditRole and columnName == 'name':
if len(value) == 0: # do not accept empty strings
return False
classInfo.setName(str(value))
return True
return False
def flags(self, index):
if index.isValid():
columnName = self.columnames[index.column()]
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
if columnName in ['name']: # allow check state
flags = flags | Qt.ItemIsUserCheckable | Qt.ItemIsEditable
return flags
# return item.qt_flags(index.column())
return None
def headerData(self, col, orientation, role):
if Qt is None:
return None
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.columnames[col]
elif orientation == Qt.Vertical and role == Qt.DisplayRole:
return col
return None
class ClassificationSchemeWidget(QWidget, load('classificationscheme.ui')):
def __init__(self, parent=None, classificationScheme=None):
super(ClassificationSchemeWidget, self).__init__(parent)
self.setupUi(self)
self.mScheme = ClassificationScheme()
if classificationScheme is not None:
self.setClassificationScheme(classificationScheme)
self.tableViewModel = ClassificationSchemeTableModel(self)
self.tableClassificationScheme.setModel(self.tableViewModel)
self.btnLoadClasses.clicked.connect(self.loadClasses)
self.btnRemoveClasses.clicked.connect(self.removeSelectedClasses)
self.btnAddClasses.clicked.connect(self.addClasses)
def addClasses(self, n):
for i in range(n):
c = ClassInfo(name = '<empty>', color = QColor('red'))
self.mScheme.addClass(c)
def loadClasses(self, *args):
path = QFileDialog.getOpenFileName(self, 'Select Raster File', '')
if os.path.exists(path):
scheme = ClassificationScheme.fromRasterImage(path)
if scheme is not None:
self.appendClassificationScheme(scheme)
def appendClassificationScheme(self, classificationScheme):
assert isinstance(classificationScheme, ClassificationScheme)
for c in classificationScheme:
self.mScheme.addClass(c)
def setClassificationScheme(self, classificationScheme):
assert isinstance(classificationScheme, ClassificationScheme)
self.mScheme.classes[:]
self.appendClassificationScheme(classificationScheme)
class ClassificationSchemeDialog(QgsDialog):
@staticmethod
def getClassificationScheme(*args, **kwds):
"""
Opens a CrosshairDialog.
:param args:
:param kwds:
:return: specified CrosshairStyle if accepted, else None
"""
d = ClassificationSchemeDialog(*args, **kwds)
d.exec_()
if d.result() == QDialog.Accepted:
return d.classificationSheme()
else:
return None
def __init__(self, parent=None, classificationScheme=None, title='Specify Classification Scheme'):
super(ClassificationSchemeDialog, self).__init__(parent=parent , \
buttons=QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.w = ClassificationSchemeWidget(parent=self, classificationScheme=classificationScheme)
self.setWindowTitle(title)
self.btOk = QPushButton('Ok')
self.btCancel = QPushButton('Cancel')
buttonBar = QHBoxLayout()
#buttonBar.addWidget(self.btCancel)
#buttonBar.addWidget(self.btOk)
l = self.layout()
l.addWidget(self.w)
l.addLayout(buttonBar)
#self.setLayout(l)
if isinstance(classificationScheme, ClassificationScheme):
self.setClassificationSheme(classificationScheme)
s = ""
def classificationScheme(self):
return self.w.crosshairStyle()
def setClassificationScheme(self, classificationScheme):
assert isinstance(classificationScheme, ClassificationScheme)
self.w.setClassificationScheme(classificationScheme)
if __name__ == '__main__':
import site, sys
#add site-packages to sys.path as done by enmapboxplugin.py
from timeseriesviewer import sandbox
qgsApp = sandbox.initQgisEnvironment()
pathClassImg = r'D:\Repositories\QGIS_Plugins\enmap-box\enmapbox\testdata\HymapBerlinA\HymapBerlinA_test.img'
pathShp = r''
w = ClassificationSchemeWidget()
w.setClassificationScheme(ClassificationScheme.fromRasterImage(pathClassImg))
w.show()
qgsApp.exec_()
qgsApp.exitQgis()