Commit 78f4a48b authored by Benjamin Jakimow's avatar Benjamin Jakimow
Browse files

qps update


next/previous label works on filered features
Signed-off-by: Benjamin Jakimow's avatarBenjamin Jakimow benjamin.jakimow@geo.hu-berlin.de <benjamin.jakimow@geo.hu-berlin.de>
parent 28b551e7
Pipeline #13923 failed with stage
in 6 seconds
......@@ -449,7 +449,12 @@ class TreeNode(QObject):
if isinstance(child_nodes, TreeNode):
child_nodes = [child_nodes]
assert isinstance(child_nodes, list)
child_nodes = [l for l in child_nodes if l not in self.mChildren]
unique = []
for n in child_nodes:
if not (n in unique or n in self.mChildren):
unique.append(n)
child_nodes = unique
l = len(child_nodes)
if l == 0:
......
......@@ -1559,17 +1559,15 @@ def parseWavelength(dataset) -> typing.Tuple[np.ndarray, str]:
:return: (wl, wl_u) or (None, None), if not existing
"""
wl = None
wlu = None
try:
dataset = gdalDataset(dataset)
except:
pass
def checkWavelengthUnit(key: str, value: str) -> str:
wlu = None
wlu: str = None
value = value.strip()
if re.search(r'wavelength[ _]?units?', key, re.I):
if re.search(r'^wavelength[ _]?units?', key, re.I):
# metric length units
wlu = UnitLookup.baseUnit(value)
......@@ -1595,9 +1593,9 @@ def parseWavelength(dataset) -> typing.Tuple[np.ndarray, str]:
wlu = None
return wlu
def checkWavelength(key: str, values: str):
wl = None
if re.search(r'wavelengths?$', key, re.I):
def checkWavelength(key: str, values: str, expected:int = 1) -> np.ndarray:
wl: np.ndarray = None
if re.search(r'^wavelengths?$', key, re.I):
# remove trailing / ending { } and whitespace
values = re.sub('[{}]', '', values).strip()
if ',' not in values:
......@@ -1605,7 +1603,7 @@ def parseWavelength(dataset) -> typing.Tuple[np.ndarray, str]:
else:
sep = ','
try:
wl = np.fromstring(values, count=dataset.RasterCount, sep=sep)
wl = np.fromstring(values, count=expected, sep=sep)
except ValueError as exV:
pass
except Exception as ex:
......@@ -1621,51 +1619,80 @@ def parseWavelength(dataset) -> typing.Tuple[np.ndarray, str]:
mdDict = dataset.GetMetadata_Dict(domain)
domainWLU: str = None
domainWL: np.ndarray = None
# search domain
for key, values in mdDict.items():
if wl is None:
wl = checkWavelength(key, values)
if wlu is None:
wlu = checkWavelengthUnit(key, values)
if wl is not None and wlu is not None:
# domain-specific check
if domain == 'FORCE' and wlu == 'DecimalYear':
# make decimal-year values leap-year sensitive
wl = convertDateUnit(datetime64(wl, dpy=365), 'DecimalYear')
break
if wl is not None and wlu is not None:
if len(wl) > dataset.RasterCount:
wl = wl[0:dataset.RasterCount]
return wl, wlu
# 2. check on band level
wl = []
if domainWL is None:
domainWL = checkWavelength(key, values, expected=dataset.RasterCount)
if domainWLU is None:
domainWLU = checkWavelengthUnit(key, values)
if isinstance(domainWL, np.ndarray) and isinstance(domainWLU, str):
if domain == 'FORCE' and domainWLU == 'DecimalYear':
# make decimal-year values leap-year sensitive
domainWL = convertDateUnit(datetime64(domainWL, dpy=365), 'DecimalYear')
if len(domainWL) > dataset.RasterCount:
domainWL = domainWL[0:dataset.RasterCount]
return domainWL, domainWLU
# 2. check on band level. collect wl from each single band
# first domain that defines wl and wlu is prototyp domain for all other bands
wl = [] # list of wavelength values
wlu: str = None # wavelength unit string
wlDomain: str = None # the domain in which the WL and WLU are defined
wlKey: str = None # key that stores the wavelength value
wluKey: str = None # key that stores the wavelength unit
for b in range(dataset.RasterCount):
band: gdal.Band = dataset.GetRasterBand(b + 1)
domains = band.GetMetadataDomainList()
if isinstance(domains, list):
for domain in domains:
# see http://www.harrisgeospatial.com/docs/ENVIHeaderFiles.html for supported wavelength units
mdDict = band.GetMetadata_Dict(domain)
_wl = None
for key, values in mdDict.items():
if wlu is None:
wlu = checkWavelengthUnit(key, values)
if _wl is None:
_wl = checkWavelength(key, values)
if wlu is not None:
s = ""
if wlu is not None and _wl is not None:
wl.append(_wl[0])
break
if b == 0:
domains = band.GetMetadataDomainList()
if isinstance(domains, list):
for domain in domains:
# see http://www.harrisgeospatial.com/docs/ENVIHeaderFiles.html for supported wavelength units
domainWL = domainWLU = None
mdDict = band.GetMetadata_Dict(domain)
for key, values in mdDict.items():
if domainWLU is None:
domainWLU = checkWavelengthUnit(key, values)
if domainWLU:
wluKey = key
if domainWL is None:
domainWL = checkWavelength(key, values, expected=1)
if isinstance(domainWL, np.ndarray):
wlKey = key
if isinstance(domainWL, np.ndarray) and isinstance(domainWLU, str):
wlDomain = domain
wlu = domainWLU
wl.append(domainWL[0])
if len(wl) == 0:
# we did not found a WL + WLU for the 1st band. stop searching and return
return None, None
else:
bandWLU = checkWavelengthUnit(wluKey, band.GetMetadataItem(wluKey, wlDomain))
bandWL = checkWavelength(wlKey, band.GetMetadataItem(wlKey, wlDomain), expected=1)
if bandWLU != wlu or bandWL is None:
print(f'{dataset.GetDescription()}: inconsistent use of metadata key {wluKey} per band')
return None, None
wl.append(bandWL[0])
if len(wl) == 0:
wl = None
else:
wl = np.asarray(wl)
if domain == 'FORCE' and wlu == 'DecimalYear':
# make decimal-year values leap-year sensitive
wl = UnitLookup.convertDateUnit(datetime64(wl, dpy=365), 'DecimalYear')
return None, None
wl = np.asarray(wl)
if domain == 'FORCE' and wlu == 'DecimalYear':
# make decimal-year values leap-year sensitive
wl = UnitLookup.convertDateUnit(datetime64(wl, dpy=365), 'DecimalYear')
return wl, wlu
......
......@@ -284,8 +284,6 @@ def setQuickTSDLabels(vectorLayer: QgsVectorLayer,
conf = setup.config()
labelType: LabelShortcutType = LabelShortcutType.fromConfValue(conf.get(LabelConfigurationKey.LabelType))
value = quickLabelValue(fieldType, labelType, tsd, tss)
if value is not None:
......@@ -641,37 +639,65 @@ class LabelAttributeTypeWidgetDelegate(QStyledItemDelegate):
model.setData(index, w.currentData(Qt.UserRole), Qt.EditRole)
def gotoNextFeature(layer: QgsVectorLayer, tools: QgsVectorLayerTools):
def gotoNextFeature(layer: QgsVectorLayer,
tools: QgsVectorLayerTools,
visible_features: typing.List[int] = None) -> int:
assert isinstance(tools, QgsVectorLayerTools)
if isinstance(layer, QgsVectorLayer) and layer.hasFeatures():
if not isinstance(visible_features, list):
visible_features = layer.allFeatureIds()
if len(visible_features) == 0:
return None
visible_features = sorted(visible_features)
selected_fids = layer.selectedFeatureIds()
selected_fids = [f for f in visible_features if f in selected_fids]
allIDs = sorted(layer.allFeatureIds())
fids = layer.selectedFeatureIds()
if len(fids) == 0:
nextFID = allIDs[0]
if len(selected_fids) == 0:
nextFID = visible_features[0]
else:
i = min(allIDs.index(max(fids)) + 1, len(allIDs) - 1)
nextFID = allIDs[i]
next_index = visible_features.index(max(selected_fids)) + 1
if next_index >= len(visible_features):
nextFID = visible_features[-1]
else:
nextFID = visible_features[next_index]
layer.selectByIds([nextFID])
tools.panToSelected(layer)
return nextFID
return None
def gotoPreviousFeature(layer: QgsVectorLayer, tools: QgsVectorLayerTools):
def gotoPreviousFeature(layer: QgsVectorLayer,
tools: QgsVectorLayerTools,
visible_features: typing.List[int] = None):
assert isinstance(tools, QgsVectorLayerTools)
if isinstance(layer, QgsVectorLayer) and layer.hasFeatures():
allIDs = sorted(layer.allFeatureIds())
fids = layer.selectedFeatureIds()
if len(fids) == 0:
nextFID = allIDs[0]
if not isinstance(visible_features, list):
visible_features = layer.allFeatureIds()
if len(visible_features) == 0:
return None
visible_features = sorted(visible_features)
selected_fids = layer.selectedFeatureIds()
selected_fids = [f for f in visible_features if f in selected_fids]
if len(selected_fids) == 0:
nextFID = visible_features[0]
else:
i = max(allIDs.index(min(fids)) - 1, 0)
nextFID = allIDs[i]
next_index = visible_features.index(max(selected_fids)) - 1
if next_index < 0:
nextFID = visible_features[0]
else:
nextFID = visible_features[next_index]
layer.selectByIds([nextFID])
tools.panToSelected(layer)
class LabelWidget(AttributeTableWidget):
sigMoveTo = pyqtSignal([QDateTime],
[QDateTime, object])
......@@ -682,13 +708,21 @@ class LabelWidget(AttributeTableWidget):
self.mActionNextFeature = QAction('Next Feature', parent=self)
self.mActionNextFeature.setIcon(QIcon(':/images/themes/default/mActionAtlasNext.svg'))
self.mActionNextFeature.triggered.connect(
lambda *args, lyr=self.mLayer, vlt=self.vectorLayerTools(): gotoNextFeature(lyr, vlt)
lambda *args,
lyr=self.mLayer,
vlt=self.vectorLayerTools(),
mv=self.mMainView
: gotoNextFeature(lyr, vlt, visible_features=mv.filteredFeatures())
)
self.mActionPreviousFeature = QAction('Previous Feature', parent=self)
self.mActionPreviousFeature.setIcon(QIcon(':/images/themes/default/mActionAtlasPrev.svg'))
self.mActionPreviousFeature.triggered.connect(
lambda *args, lyr=self.mLayer, vlt=self.vectorLayerTools(): gotoPreviousFeature(lyr, vlt))
lambda *args,
lyr=self.mLayer,
vlt=self.vectorLayerTools(),
mv=self.mMainView
: gotoPreviousFeature(lyr, vlt, visible_features=mv.filteredFeatures()))
self.mOptionSelectBehaviour = QAction('Selection behaviour')
self.mOptionSelectBehaviour.setCheckable(True)
......@@ -721,7 +755,7 @@ class LabelWidget(AttributeTableWidget):
self.mOptionSelectionAddToSelection.trigger()
# show selected feature on top by default
self.mActionSelectedToTop.setChecked(True)
# self.mActionSelectedToTop.setChecked(True)
self.mToolbar: QToolBar
self.mToolbar.insertActions(self.mActionToggleEditing,
......@@ -779,9 +813,6 @@ class LabelWidget(AttributeTableWidget):
# add temporal options
if isinstance(datetime, QDateTime):
date_string = datetime.toString(Qt.ISODate)
a1 = QAction(f'Move time to', menu)
a1.setToolTip(f'Moves the current date to {date_string}')
......@@ -798,12 +829,9 @@ class LabelWidget(AttributeTableWidget):
a3.triggered.connect(lambda *args, f=feature, d=datetime:
self.sigMoveTo[QDateTime, object].emit(d, extent))
menu.insertActions(menu.actions()[0], [a1, a2, a3])
menu.insertSeparator(menu.actions()[3])
def selectBehaviour(self) -> QgsVectorLayer.SelectBehavior:
if self.mOptionSelectionSetSelection.isChecked():
return QgsVectorLayer.SetSelection
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment