Newer
Older
D.btn_showPxCoordinate.clicked.connect(lambda: self.ua_showPxCoordinate_start())
D.btn_selectByCoordinate.clicked.connect(self.ua_selectByCoordinate)
D.btn_selectByRectangle.clicked.connect(self.ua_selectByRectangle)
D.btn_addBandView.clicked.connect(lambda :self.ua_addView())
D.btn_addTSImages.clicked.connect(lambda :self.ua_addTSImages())
D.btn_addTSMasks.clicked.connect(lambda :self.ua_addTSMasks())
D.btn_removeTSD.clicked.connect(lambda : self.ua_removeTSD(None))
D.spinBox_ncpu.setRange(0, multiprocessing.cpu_count())
# Declare instance attributes
self.actions = []
#self.menu = self.tr(u'&EnMAP-Box')
# TODO: We are going to let the user set this up in a future iteration
self.RectangleMapTool = None
self.PointMapTool = None
print('Init QGIS Interaction')
self.canvas = self.iface.mapCanvas()
self.menu = self.tr(u'&SenseCarbon TSV')
self.toolbar = self.iface.addToolBar(u'SenseCarbon TSV')
self.toolbar.setObjectName(u'SenseCarbon TSV')
self.RectangleMapTool = RectangleMapTool(self.canvas)
self.RectangleMapTool.rectangleDrawed.connect(self.ua_selectBy_Response)
self.PointMapTool = PointMapTool(self.canvas)
self.PointMapTool.coordinateSelected.connect(self.ua_selectBy_Response)
#self.RectangleMapTool..connect(self.ua_selectByRectangle_Done)
self.CPV = self.dlg.scrollAreaWidgetContents.layout()
self.check_enabled()
s = ""
def ua_selectByRectangle(self):
if self.RectangleMapTool is not None:
self.canvas.setMapTool(self.RectangleMapTool)
if self.PointMapTool is not None:
self.canvas.setMapTool(self.PointMapTool)
def ua_selectBy_Response(self, geometry, authid):
D = self.dlg
x = D.spinBox_coordinate_x.value()
y = D.spinBox_coordinate_x.value()
dx = D.doubleSpinBox_subset_size_x.value()
dy = D.doubleSpinBox_subset_size_y.value()
canvas_srs = osr.SpatialReference()
print(authid)
print(type(authid))
wkt = osr.GetUserInputAsWKT(str(authid))
print(wkt)
canvas_srs.ImportFromWkt(wkt)
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
if type(geometry) is QgsRectangle:
center = geometry.center()
x = center.x()
y = center.y()
dx = geometry.xMaximum() - geometry.xMinimum()
dy = geometry.yMaximum() - geometry.yMinimum()
if type(geometry) is QgsPoint:
x = geometry.x()
y = geometry.y()
if self.TS.srs is not None and not self.TS.srs.IsSame(canvas_srs):
print('Convert canvas coordinates to time series SRS')
g = ogr.Geometry(ogr.wkbPoint)
g.AddPoint(x,y)
g.AssignSpatialReference(canvas_srs)
g.TransformTo(self.TS.srs)
x = g.GetX()
y = g.GetY()
D.doubleSpinBox_subset_size_x.setValue(dx)
D.doubleSpinBox_subset_size_y.setValue(dy)
D.spinBox_coordinate_x.setValue(x)
D.spinBox_coordinate_y.setValue(y)
def qgs_handleMouseDown(self, pt, btn):
print('MOUSE DOWN')
print(pt)
print(btn)
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
def ua_TSprogress(self, v_min, v, v_max):
assert v_min <= v and v <= v_max
P = self.dlg.progressBar
if P.minimum() != v_min or P.maximum() != v_max:
P.setRange(v_min, v_max)
P.setValue(v)
def ua_datumAdded(self):
cb_centerdate = self.dlg.cb_centerdate
cb_centerdate.clear()
if len(self.TS) > 0:
if self.dlg.spinBox_coordinate_x.value() == 0.0 and \
self.dlg.spinBox_coordinate_y.value() == 0.0:
xmin, ymin, xmax, ymax = self.TS.getMaxExtent()
self.dlg.spinBox_coordinate_x.setRange(xmin, xmax)
self.dlg.spinBox_coordinate_y.setRange(ymin, ymax)
self.dlg.spinBox_coordinate_x.setValue(0.5*(xmin+xmax))
self.dlg.spinBox_coordinate_y.setValue(0.5*(ymin+ymax))
s =""
for date in self.TS.getDates():
cb_centerdate.addItem(date.astype('str'), date)
s = ""
self.dlg.tableView_TimeSeries.resizeColumnsToContents()
def check_enabled(self):
D = self.dlg
hasTSV = len(self.VIEWS) > 0
hasQGIS = qgis_available
D.tabWidget_viewsettings.setEnabled(hasTS)
D.btn_showPxCoordinate.setEnabled(hasTS and hasTSV)
D.btn_selectByCoordinate.setEnabled(hasQGIS)
D.btn_selectByRectangle.setEnabled(hasQGIS)
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('EnMAPBox', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
callback=self.run,
parent=self.iface.mainWindow())
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
def run(self):
"""Run method that performs all the real work"""
#self.dlg.setWindowIcon(QIcon(self.icon_path))
# show the GUI
self.dlg.show()
if DEBUG:
pass
def ua_showPxCoordinate_start(self):
dx = D.doubleSpinBox_subset_size_x.value() * 0.5
dy = D.doubleSpinBox_subset_size_x.value() * 0.5
cx = D.spinBox_coordinate_x.value()
cy = D.spinBox_coordinate_y.value()
ring.AddPoint(cx - dx, cy + dy)
ring.AddPoint(cx + dx, cy + dy)
ring.AddPoint(cx + dx, cy - dy)
ring.AddPoint(cx - dx, cy - dy)
if self.srs:
bb.AssignSpatialReference(self.srs)
else:
bb.AssignSpatialReference(self.TS.srs)
bb.AddGeometry(ring)
bbWkt = bb.ExportToWkt()
srsWkt = bb.GetSpatialReference().ExportToWkt()
self.ImageChipBuffer.setBoundingBox(bb)
D = self.dlg
size_x = D.spinBox_chipsize_x.value()
size_y = D.spinBox_chipsize_y.value()
ScrollArea = D.scrollArea
#S.setWidgetResizable(False)
#remove widgets
#get the dates of interes
dates_of_interest = list()
if D.rb_showEntireTS.isChecked():
dates_of_interest = self.TS.getDates()
elif D.rb_showSelectedDates.isChecked():
dates_of_interest = self.getSelectedDates()
elif D.rb_showTimeWindow.isChecked():
TSD = D.cb_timeWindow_doi.itemData(D.cb_timeWindow_doi.currentIndex())
s = ""
allDates = self.TS.getDates()
i_doi = allDates.index(TSD)
i0 = max([0, i_doi-D.sb_ndates_before.value()])
ie = min([i_doi + D.sb_ndates_after.value(), len(allDates)])
dates_of_interest = allDates[i0:ie]
if self.CPV is None:
ScrollArea.setLayout(QHBoxLayout())
self.CPV = ScrollArea.layout()
else:
diff = diff.symmetric_difference(self.CHIPWIDGETS.keys())
if len(diff) != 0:
self.clearLayoutWidgets(self.CPV)
else:
for date, viewList in self.CHIPWIDGETS.items():
for imageLabel in viewList:
imageLabel.clear()
#initialize image labels
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
#LV = QVBoxLayout()
#LV.setSizeConstraint(QLayout.SetNoConstraint)
TSD = self.TS.data[date]
textLabel = QLabel('{}'.format(date.astype(str)))
textLabel.setToolTip(str(TSD))
#textLabel.setMinimumWidth(size_x)
#textLabel.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
self.CPV.addWidget(textLabel, 0, i)
viewList = list()
for j, view in enumerate(self.VIEWS):
imageLabel=QLabel()
imageLabel.setFrameShape(QFrame.StyledPanel)
imageLabel.setMinimumSize(size_x, size_y)
imageLabel.setMaximumSize(size_x, size_y)
imageLabel.setSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed)
viewList.append(imageLabel)
self.CPV.addWidget(imageLabel,j+1, i)
self.CHIPWIDGETS[date] = viewList
#LH.addSpacerItem(Spacer(size_x, size_y))
ScrollArea.show()
#fill image labels
missing_dates = set()
missing_bands = set()
for i, date in enumerate(self.TS.getDates()):
required_bands = set()
for j, view in enumerate(self.VIEWS):
required_bands = required_bands.union(set(view.getBands()))
missing = self.ImageChipBuffer.getMissingBands(date, required_bands)
if len(missing) == 0:
self.ua_showPxCoordinate_addChips(None, date=date)
else:
missing_dates.add(date)
missing_bands = missing_bands.union(missing)
if len(missing_dates) > 0:
self.TS.getSpatialChips_parallel(bbWkt, srsWkt, dates=list(missing_dates), bands=list(missing_bands))
def ua_showPxCoordinate_addChips(self, results, date=None):
if results is not None:
date, chipData = results
self.ImageChipBuffer.addDataCube(date, chipData)
viewList = self.CHIPWIDGETS.get(date)
if viewList:
for j, view in enumerate(self.VIEWS):
imageLabel = viewList[j]
img = self.ImageChipBuffer.getChipImage(date, view)
pxMap = QPixmap.fromImage(img)
pxMap = pxMap.scaled(imageLabel.size(), Qt.KeepAspectRatio)
imageLabel.setPixmap(pxMap)
s = ""
pass
s = ""
pass
def clearLayoutWidgets(self, L):
if L is not None:
while L.count():
w = L.takeAt(0)
w.widget().deleteLater()
#if w is not None:
# w.widget().deleteLater()
def ua_addTSImages(self, files=None):
if files is None:
files = QFileDialog.getOpenFileNames()
if files:
M = self.dlg.tableView_TimeSeries.model()
M.beginResetModel()
self.TS.addFiles(files)
M.endResetModel()
nb = self.TS.nb
if len(self.VIEWS) == 0 and nb > 0:
if nb < 3:
bands = [1,1,1]
else:
self.ua_addView([3,2,1])
if nb >= 5:
self.ua_addView([4,5,3])
self.check_enabled()
def ua_addTSMasks(self, files=None):
if files is None:
files = QFileDialog.getOpenFileNames()
l = len(files)
if l > 0:
M = self.dlg.tableView_TimeSeries.model()
M.beginResetModel()
M.endResetModel()
self.check_enabled()
def setViewNames(self):
for i, w in enumerate(self.VIEWS):
w.setTitle('View {}'.format(i+1))
self.check_enabled()
def ua_addView(self, bands = [3,2,1]):
import imagechipviewsettings_widget
if len(self.TS.bandnames) > 0:
w = imagechipviewsettings_widget.ImageChipViewSettings(self.TS, parent=self.dlg)
w.setMaximumSize(w.size())
#w.setMinimumSize(w.size())
w.setSizePolicy(QSizePolicy.Fixed,QSizePolicy.MinimumExpanding)
w.setBands(bands)
w.removeView.connect(lambda : self.ua_removeView(w))
L = self.dlg.scrollArea_viewsWidget.layout()
L.addWidget(w)
self.dlg.scrollArea_views.show()
def ua_removeTS(self):
#remove views
M = self.dlg.tableView_TimeSeries.model()
M.beginResetModel()
self.TS.clear()
M.endResetModel()
self.check_enabled()
def getSelectedDates(self):
TV = self.dlg.tableView_TimeSeries
TVM = TV.model()
return [TVM.getTimeSeriesDatumFromIndex(idx).getDate() for idx in TV.selectionModel().selectedRows()]
M = self.dlg.tableView_TimeSeries.model()
M.beginResetModel()
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
def ua_removeView(self,w):
self.VIEWS.remove(w)
L = self.dlg.scrollArea_viewsWidget.layout()
L.removeWidget(w)
w.deleteLater()
self.setViewNames()
def run_tests():
if False:
pathImg = r'O:\SenseCarbonProcessing\BJ_NOC\01_RasterData\00_VRTs\02_Cutted\2014-07-26_LC82270652014207LGN00_BOA.vrt'
pathMsk = r'O:\SenseCarbonProcessing\BJ_NOC\01_RasterData\00_VRTs\02_Cutted\2014-07-26_LC82270652014207LGN00_Msk.vrt'
TSD = TimeSeriesDatum(pathImg)
TSD.setMask(pathMsk)
print(TSD)
c = [670949.883,-786288.771]
w_x = w_y = 1000 #1km box
srs = TSD.getSpatialReference()
ring = ogr.Geometry(ogr.wkbLinearRing)
import itertools
for x,y in itertools.product([1000, -1000], repeat=2):
ring.AddPoint(c[0]+x, c[1]+y)
ring.AssignSpatialReference(srs)
bb = ogr.Geometry(ogr.wkbPolygon)
bb.AddGeometry(ring)
bb.AssignSpatialReference(srs)
def getChip3d_OLD(chips, r,g,b, range_r, range_g, range_b):
nl, ns = chips[r].shape
a3d = np.ndarray((3,nl,ns), dtype='float')
rgb_idx = [r,g,b]
ranges = [range_r, range_g, range_b]
for i, rgb_i in enumerate(rgb_idx):
range = ranges[i]
data = chips[rgb_i].astype('float')
data -= range[0]
data *= 255./range[1]
a3d[i,:] = data
np.clip(a3d, 0, 255, out=a3d)
return a3d.astype('uint8')
app=QApplication([])
main=PictureTest()
main.show()
range_r = [0,500]
range_g = [0,500]
range_b = [0,500]
bands = [3,2,1]
chipData = TSD.readSpatialChip(bb,bands=bands )
main.addNumpy(getChip3d(chipData, bands, (range_r, range_g, range_b)))
app.exec_()
exit(0)
if False:
dirSrc = r'O:\SenseCarbonProcessing\BJ_NOC\01_RasterData\00_VRTs\02_Cutted'
filesImg = file_search(dirSrc, '2014*_BOA.vrt')
filesMsk = file_search(dirSrc, '2014*_Msk.vrt')
TS = TimeSeries(imageFiles=filesImg, maskFiles=filesMsk)
print(TS)
exit(0)
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
S = SenseCarbon_TSV(a)
S.run()
if True:
dirSrc = r'O:\SenseCarbonProcessing\BJ_NOC\01_RasterData\00_VRTs\02_Cutted'
filesImg = file_search(dirSrc, '2014*_BOA.vrt')
filesMsk = file_search(dirSrc, '2014*_Msk.vrt')
S.ua_addTSImages(files=filesImg)
#S.ua_addTSMasks(files=filesMsk)
#S.ua_addView(bands=[4,5,3])
a.exec_()
if False:
import qgis.core
# supply path to where is your qgis installed
QgsApplication.setPrefixPath("/Applications/QGIS_2.12.app/Contents/MacOS/QGIS", True)
# load providers
QgsApplication.initQgis()
a = QgsApplication([], True)
dirSrc = r'O:\SenseCarbonProcessing\BJ_NOC\01_RasterData\00_VRTs\02_Cutted'
filesImg = file_search(dirSrc, '2014*_BOA.vrt')
filesMsk = file_search(dirSrc, '2014*_Msk.vrt')
S.ua_addTSImages(files=filesImg)