def add_layer(layer: QgsMapLayer, name: str = None, index: int = -1, color: typing.Tuple[int, int, int] = None, size: float = None, file: str = None, parent: QgsLayerTreeNode = None, show_feature_count: bool = True) -> None: if name: layer.setName(name) if isinstance(layer, QgsVectorLayer): if color or size or file: update_symbology(layer, color=color, size=size, file=file) elif isinstance(layer, QgsRasterLayer): # TODO update symbology pass instance = QgsProject.instance() instance.addMapLayer(layer, False) layerTreeNode = QgsLayerTreeLayer(layer) layerTreeNode.setCustomProperty('showFeatureCount', show_feature_count) parent = parent if parent else instance.layerTreeRoot() parent.insertChildNode(index, layerTreeNode)
def move_tree_node(node: typing.Union[QgsMapLayer, QgsLayerTreeNode], index: int, parent: QgsLayerTreeNode = None) -> None: root = QgsProject.instance().layerTreeRoot() node = root.findLayer(node.id()) if isinstance(node, QgsMapLayer) else node if node is None: return layer_clone = node.clone() parent = parent if parent else node.parent() parent.insertChildNode(index, layer_clone) parent.removeChildNode(node)
def browse(path: List[str], node: QgsLayerTreeNode) -> None: if isinstance(node, QgsLayerTreeLayer): ogc_name = self.ogc_layer_name(node.layer()) elif isinstance(node, QgsLayerTreeGroup): ogc_name = node.customProperty( "wmsShortName") or node.name() else: ogc_name = node.name() nodes[ogc_name] = [ogc_name] for name in path: nodes[ogc_name].append(name) for layer in node.children(): browse(path + [ogc_name], layer)
def get_layers_and_visibility_from_node( root: QgsLayerTree, node: QgsLayerTreeNode) -> List[Tuple[QgsMapLayer, bool]]: layers = [] child: QgsLayerTreeNode for child in node.children(): if root.isGroup(child): # noinspection PyTypeChecker layers += LayerHandler.get_layers_and_visibility_from_node( root, child) else: layer = child.layer() visibility = child.itemVisibilityChecked( ) and node.itemVisibilityChecked() if layer: layers.append((layer, visibility)) return layers
def add_group(group: QgsLayerTreeNode, name: str = None, index: int = -1, parent: QgsLayerTreeNode = None) -> None: parent = parent if parent else QgsProject.instance().layerTreeRoot() parent.insertGroup(index, group)