def icon_per_geometry_type(geometry_type: str): """Return the icon for a geometry type. If not found, it will return the default icon. :param geometry_type: The geometry as a string. :type geometry_type: basestring :return: The icon. :rtype: QIcon """ geometry_type = geometry_type.lower() if geometry_type == "raster": return QgsLayerItem.iconRaster() elif geometry_type == "mesh": return QgsLayerItem.iconMesh() elif geometry_type == "point": return QgsLayerItem.iconPoint() elif geometry_type == "line": return QgsLayerItem.iconLine() elif geometry_type == "polygon": return QgsLayerItem.iconPolygon() elif geometry_type == "no geometry": return QgsLayerItem.iconTable() else: return QgsLayerItem.iconDefault()
def __init__(self, parent, name, path, lyr_object=None, layer_path=''): super().__init__(QgsDataItem.Custom, parent, name, path) self.setCapabilities(QgsDataItem.Fertile | QgsDataItem.Collapse) self.layer_path = layer_path or path self.object = lyr_object if path: self.setIcon(GuiUtils.get_icon('icon.svg')) elif isinstance(self.object, (GroupLayer, BaseMapLayer)): self.setIcon(QgsDataCollectionItem.iconDir()) elif self.object.__class__.__name__ in ('WmsMapLayer', 'RasterLayer'): self.setIcon(QgsLayerItem.iconRaster()) else: wkb_type = VectorLayerConverter.layer_to_wkb_type(self.object) geometry_type = QgsWkbTypes.geometryType(wkb_type) if geometry_type == QgsWkbTypes.PointGeometry: self.setIcon(QgsLayerItem.iconPoint()) elif geometry_type == QgsWkbTypes.LineGeometry: self.setIcon(QgsLayerItem.iconLine()) elif geometry_type == QgsWkbTypes.PolygonGeometry: self.setIcon(QgsLayerItem.iconPolygon()) else: self.setIcon(QgsLayerItem.iconDefault()) if self.object: if isinstance(self.object, (GroupLayer, BaseMapLayer)): self.add_children() self.setState(QgsDataItem.Populated) self.setToolTip( QDir.toNativeSeparators(path) if not lyr_object else lyr_object. name) self.child_items = []
def addQgsLayer(self, iface): currentCrs = iface.mapCanvas().mapSettings().destinationCrs() if currentCrs.isValid(): epsg = currentCrs.authid() else: epsg = 'EPSG:3857' # layerutils layer = createLayer(self, epsg) if not layer: return qgisLayerItem = QgisLayerItem(layer, self, self.ressource) self.qgisLayers.append(qgisLayerItem) self.addChild(qgisLayerItem) self.setExpanded(True) #for an odd reason there appears a warning below the layer if it is a #wms layer from shogun - workaround to hide this: if layer.dataProvider().name( ) == 'wms' and self.source['url'].startswith('/shogun2-webapp/'): root = QgsProject.instance().layerTreeRoot() layerNode = root.findLayer(layer.id()) layerNode.setExpanded(False) # when the layer has loaded for the first time and it's icon # is still on iconDefault, update the icon with the correct geometry if not self.datatype == 'Raster': if layer.type() == QgsMapLayer.VectorLayer: geom = layer.geometryType() if PYTHON_VERSION >= 3: if geom == QgsWkbTypes.PointGeometry: self.icon = QgsLayerItem.iconPoint() elif geom == QgsWkbTypes.LineGeometry: self.icon = QgsLayerItem.iconLine() elif geom == QgsWkbTypes.PolygonGeometry: self.icon = QgsLayerItem.iconPolygon() else: if geom == QGis.Point: self.icon = QgsLayerItem.iconPoint() elif geom == QGis.Line: self.icon = QgsLayerItem.iconLine() elif geom == QGis.Polygon: self.icon = QgsLayerItem.iconPolygon() else: self.icon = QgsLayerItem.iconRaster() self.setIcon(0, self.icon)
def icon_for_geometry_type(geometry_type: str) -> QIcon(): """ Return the correct icon according to the geometry type. """ if geometry_type == NULL: return QgsLayerItem.iconTable() elif geometry_type == 'POINT': return QgsLayerItem.iconPoint() elif geometry_type == 'LINESTRING': return QgsLayerItem.iconLine() elif geometry_type == 'MULTIPOLYGON': return QgsLayerItem.iconPolygon() # Default icon return QIcon(resources_path('icons', 'icon.png'))
def layer_icon(layer): """Helper to get the layer icon. :param layer: A layer. :type layer: QgsMapLayer :returns: The icon for the given layer. :rtype: QIcon """ if is_raster_layer(layer): return QgsLayerItem.iconRaster() elif is_point_layer(layer): return QgsLayerItem.iconPoint() elif is_line_layer(layer): return QgsLayerItem.iconLine() elif is_polygon_layer(layer): return QgsLayerItem.iconPolygon() else: return QgsLayerItem.iconDefault()