Skip to content

Commit

Permalink
Merge pull request #17 from Riverscapes/dev
Browse files Browse the repository at this point in the history
Version 0.4.0
  • Loading branch information
MattReimer authored May 13, 2021
2 parents 1707465 + 904e00c commit 926e6b9
Show file tree
Hide file tree
Showing 7 changed files with 1,001 additions and 944 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
"--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",
// Relative paths have a problem in pylint. We'll try not to use them but.....
"--disable=E0402"
],
"python.analysis.extraPaths": [
"${workspaceRoot}"
]
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.4.0 ***(May 13, 2021)***

* [Fixed a blocking bug that prevented working on QGIS Versions less than 3.18](https://github.com/Riverscapes/QRAVEPlugin/issues/16)
* [Fixed an issue where layers with the same name in different projects cannot be added to the map](https://github.com/Riverscapes/QRAVEPlugin/issues/14)
* [Fixed: Project-level "Add all to map" broken #13](https://github.com/Riverscapes/QRAVEPlugin/issues/13)
* [Regression: Transparency issue fixed and Vectors now supported](https://github.com/Riverscapes/QRAVEPlugin/issues/2)
* Misc problems and code cleanups.

## 0.3.0 ***(May 10, 2021)***

* [Fixed the about screen regression issue](https://github.com/Riverscapes/QRAVEPlugin/issues/11)
Expand Down
2 changes: 1 addition & 1 deletion __version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.0"
__version__ = "0.4.0"
141 changes: 94 additions & 47 deletions src/classes/qrave_map_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,68 +129,115 @@ def add_layer_to_map(item: QStandardItem):
# Loop over all the parent group layers for this raster
# ensuring they are in the tree in correct, nested order

transparency = 0
try:
if 'transparency' in map_layer.bl_attr:
transparency = int(map_layer.bl_attr['transparency'])
except Exception as e:
settings.log('Error deriving transparency from layer: {}'.format(e))

# Only add the layer if it's not already in the registry
if not QgsProject.instance().mapLayersByName(map_layer.label):
exists = False
existing_layers = QgsProject.instance().mapLayersByName(map_layer.label)
layers_ancestry = [QRaveMapLayer.get_layer_ancestry(lyr) for lyr in existing_layers]

# Now we compare the ancestry group labels to the business logic ancestry branch names
# to see if this layer is already in the map
for lyr in layers_ancestry:
if len(lyr) == len(ancestry) \
and all(iter([ancestry[x][0] == lyr[x] for x in range(len(ancestry))])):
exists = True
break

if not exists:
layer_uri = map_layer.layer_uri
rOutput = None
# This might be a basemap
if map_layer.layer_type == QRaveMapLayer.LayerTypes.WMS:
rOutput = QgsRasterLayer(layer_uri, map_layer.label, 'wms')

elif map_layer.layer_type in [QRaveMapLayer.LayerTypes.LINE, QRaveMapLayer.LayerTypes.POLYGON, QRaveMapLayer.LayerTypes.POINT]:
if map_layer.layer_name is not None:
layer_uri += "|layername={}".format(map_layer.layer_name)
rOutput = QgsVectorLayer(layer_uri, map_layer.label, "ogr")
rOutput = QgsVectorLayer(layer_uri, map_layer.label, "ogr")

elif map_layer.layer_type == QRaveMapLayer.LayerTypes.RASTER:
# Raster
rOutput = QgsRasterLayer(layer_uri, map_layer.label)
if transparency > 0:
rOutput.setOpacity((100 - transparency) / 100)

##########################################
# Symbology
##########################################

symbology = map_layer.bl_attr['symbology'] if map_layer.bl_attr is not None and 'symbology' in map_layer.bl_attr else None
# If the business logic has symbology defined
if symbology is not None:
qml_fname = '{}.qml'.format(symbology)
os.path.abspath(os.path.join(project.project_dir, qml_fname))

# Here are the search paths for QML files in order of precedence
hierarchy = [
os.path.abspath(os.path.join(project.project_dir, qml_fname)),
# This is the default one
os.path.abspath(os.path.join(SYMBOLOGY_DIR, project.project_type, qml_fname)),
os.path.abspath(os.path.join(SYMBOLOGY_DIR, 'Shared', qml_fname))
]
# Find the first match


if rOutput is not None:
##########################################
# Symbology
##########################################

symbology = map_layer.bl_attr['symbology'] if map_layer.bl_attr is not None and 'symbology' in map_layer.bl_attr else None
# If the business logic has symbology defined
if symbology is not None:
qml_fname = '{}.qml'.format(symbology)
os.path.abspath(os.path.join(project.project_dir, qml_fname))

# Here are the search paths for QML files in order of precedence
hierarchy = [
os.path.abspath(os.path.join(project.project_dir, qml_fname)),
# This is the default one
os.path.abspath(os.path.join(SYMBOLOGY_DIR, project.project_type, qml_fname)),
os.path.abspath(os.path.join(SYMBOLOGY_DIR, 'Shared', qml_fname))
]
# Find the first match
try:
chosen_qml = next(iter([candidate for candidate in hierarchy if os.path.isfile(candidate)]))
# Report to the terminal if we couldn't find a qml file to use
if chosen_qml is None:
settings.msg_bar(
"Missing Symbology",
"Could not find a valid .qml symbology file for layer {}. Search paths: [{}]".format(layer_uri, ', '.join(hierarchy)),
level=Qgis.Warning
)
# Apply the QML file
else:
rOutput.loadNamedStyle(chosen_qml)

except StopIteration:
settings.log('Could not find valid symbology for layer at any of the following search paths: [ {} ]'.format(', '.join(hierarchy)), Qgis.Warning)

############################################################
# Transparency. A few notes:
# - QML transparency will prevail for rasters before 3.18
# - We set this here so that QML layer transparency will be
# overruled
############################################################
transparency = 0

try:
chosen_qml = next(iter([candidate for candidate in hierarchy if os.path.isfile(candidate)]))
# Report to the terminal if we couldn't find a qml file to use
if chosen_qml is None:
settings.msg_bar(
"Missing Symbology",
"Could not find a valid .qml symbology file for layer {}. Search paths: [{}]".format(layer_uri, ', '.join(hierarchy)),
level=Qgis.Warning
)
# Apply the QML file
else:
rOutput.loadNamedStyle(chosen_qml)

except StopIteration:
settings.log('Could not find valid symbology for layer at any of the following search paths: [ {} ]'.format(', '.join(hierarchy)), Qgis.Warning)

QgsProject.instance().addMapLayer(rOutput, False)
parentGroup.insertLayer(item.row(), rOutput)
if 'transparency' in map_layer.bl_attr:
transparency = int(map_layer.bl_attr['transparency'])
except Exception as e:
settings.log('Error deriving transparency from layer: {}'.format(e))

try:
if transparency > 0:
if rOutput.__class__ is QgsVectorLayer:
rOutput.setLayerTransparency(transparency)
# rOutput.triggerRepaint()
elif rOutput.__class__ is QgsRasterLayer:
renderer = rOutput.renderer()
renderer.setOpacity((100 - transparency) / 100.0)
# rOutput.triggerRepaint()
except Exception as e:
settings.log('Error deriving transparency from layer: {}'.format(e))

QgsProject.instance().addMapLayer(rOutput, False)
parentGroup.insertLayer(item.row(), rOutput)

# if the layer already exists trigger a refresh
else:
QgsProject.instance().mapLayersByName(map_layer.label)[0].triggerRepaint()

@staticmethod
def get_layer_ancestry(layer: list):
root = QgsProject.instance().layerTreeRoot()
tree_layer = root.findLayer(layer.id())

lyr_ancestry = []
if tree_layer:
layer_parent = tree_layer.parent()
while layer_parent is not None and layer_parent.name() != '' and len(lyr_ancestry) < 50:
lyr_ancestry.append(layer_parent.name())
layer_parent = layer_parent.parent()

lyr_ancestry.reverse()
return lyr_ancestry
4 changes: 2 additions & 2 deletions src/dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ def change_meta(self, item: QStandardItem, item_data: ProjectTreeData, show=Fals
QRaveTreeTypes.BASEMAP_SUPER_FOLDER,
QRaveTreeTypes.BASEMAP_SUB_FOLDER
]:
self.metaChange.emit(item.text(), MetaType.FOLDER, data, show)
self.metaChange.emit(item.text(), MetaType.FOLDER, data or {}, show)
elif isinstance(data, dict):
# this is just the generic case for any kind of metadata
self.metaChange.emit(item.text(), MetaType.NONE, data, show)
self.metaChange.emit(item.text(), MetaType.NONE, data or {}, show)
else:
# Do not update the metadata if we have nothing to show
self.metaChange.emit(item.text(), MetaType.NONE, {}, show)
Expand Down
Loading

0 comments on commit 926e6b9

Please sign in to comment.