コード例 #1
0
    def _get_style(self, symbol: QgsSymbol) -> Style:
        self.feedback.pushDebugInfo(str(type(symbol)))

        symbol_opacity: float = symbol.opacity()

        symbol_layer: QgsSymbolLayer = symbol.symbolLayers()[0]
        if symbol_layer.subSymbol() is not None:
            return self._get_style(symbol_layer.subSymbol())

        style: Style = self.style_type.get_style()
        sym_type = SymbolLayerType[symbol_layer.layerType()]
        sym = symbol_layer.properties()

        # Add data defined properties for the style
        if symbol_layer.hasDataDefinedProperties():
            data_defined_props: QgsPropertyCollection = (
                symbol_layer.dataDefinedProperties())
            for key in data_defined_props.propertyKeys():
                prop: QgsProperty = data_defined_props.property(key)
                if prop.field() != "":
                    style.add_data_defined_expression(prop.field(),
                                                      prop.asExpression())

        if isinstance(symbol, QgsFillSymbol):
            if sym_type == SymbolLayerType.SimpleLine:
                style.type = "line"
                style.fill = "#000000"
                style.fill_opacity = 0
                style.stroke = style.rgb_extract(sym["outline_color"])[0]
                style.stroke_opacity = (
                    symbol_opacity *
                    style.rgb_extract(sym["outline_color"])[1])
                style.stroke_width = style.convert_to_pixels(
                    float(sym["outline_width"]), sym["outline_width_unit"])
            if sym_type in [
                    SymbolLayerType.CentroidFill, SymbolLayerType.SimpleFill
            ]:
                if sym_type == SymbolLayerType.CentroidFill:
                    style.type = "circle"
                else:
                    style.type = "square"
                style.fill = style.rgb_extract(sym["color"])[0]
                style.fill_opacity = symbol_opacity * style.rgb_extract(
                    sym["color"])[1]
                style.stroke = style.rgb_extract(sym["outline_color"])[0]
                style.stroke_opacity = (
                    symbol_opacity *
                    style.rgb_extract(sym["outline_color"])[1])
                style.stroke_width = style.convert_to_pixels(
                    float(sym["outline_width"]), sym["outline_width_unit"])

        elif isinstance(symbol, QgsLineSymbol):
            if sym_type == SymbolLayerType.SimpleLine:
                self.feedback.pushDebugInfo(symbol_layer.properties())
                style.type = "line"
                style.fill = "transparent"
                style.fill_opacity = 0
                style.stroke = style.rgb_extract(sym["line_color"])[0]
                style.stroke_opacity = (
                    symbol_opacity * style.rgb_extract(sym["line_color"])[1])
                style.stroke_width = style.convert_to_pixels(
                    float(sym["line_width"]), sym["line_width_unit"])
        elif isinstance(symbol, QgsMarkerSymbol):
            if sym_type == SymbolLayerType.SimpleMarker:
                assert isinstance(style, PointStyle)
                style.type = "circle"
                style.fill = style.rgb_extract(sym["color"])[0]
                style.fill_opacity = symbol_opacity * style.rgb_extract(
                    sym["color"])[1]
                style.has_fill = style.fill_opacity > 0.0
                style.stroke = style.rgb_extract(sym["outline_color"])[0]
                style.stroke_opacity = (
                    symbol_opacity *
                    style.rgb_extract(sym["outline_color"])[1])
                style.stroke_width = style.convert_to_pixels(
                    float(sym["outline_width"]), sym["outline_width_unit"])
                style.has_stroke = style.stroke_opacity > 0.0
                style.radius = style.convert_to_pixels(float(sym["size"]),
                                                       sym["size_unit"])

        else:
            raise ValueError(f"Unkown symbol type: {symbol_layer.layerType()}")
        return style
コード例 #2
0
    def _extract_layer_style(self,
                             symbol: QgsSymbol) -> Tuple[List[int], VisConfig]:
        symbol_opacity: float = symbol.opacity()
        symbol_layer: QgsSymbolLayer = symbol.symbolLayers()[0]
        if symbol_layer.subSymbol() is not None:
            return self._extract_layer_style(symbol_layer.subSymbol())

        sym_type = SymbolLayerType[symbol_layer.layerType()]
        properties = symbol_layer.properties()

        # Default values
        radius = VisConfig.radius
        color_range = ColorRange.create_default()
        radius_range = VisConfig.radius_range

        if isinstance(symbol, QgsMarkerSymbol) or isinstance(
                symbol, QgsFillSymbol):
            fill_rgb, alpha = extract_color(properties['color'])
            opacity = round(symbol_opacity * alpha, 2)
            stroke_rgb, stroke_alpha = extract_color(
                properties['outline_color'])
            stroke_opacity = round(symbol_opacity * stroke_alpha, 2)
            thickness = self._convert_to_pixels(
                float(properties['outline_width']),
                properties['outline_width_unit'])
            outline = stroke_opacity > 0.0 and properties[
                'outline_style'] != 'no'
            stroke_opacity = stroke_opacity if outline else None
            stroke_color = stroke_rgb if outline else None
            filled = opacity > 0.0 and properties.get('style', 'solid') != 'no'

            if isinstance(symbol, QgsMarkerSymbol):
                size_range, height_range, elevation_scale, stroked, enable3_d, wireframe = [
                    None
                ] * 6

                # Fixed radius seems to always be False with point types
                fixed_radius = False

                radius = self._convert_to_pixels(float(properties['size']),
                                                 properties['size_unit'],
                                                 radius=True)
                thickness = thickness if thickness > 0.0 else 1.0  # Hairline in QGIS
            else:
                size_range = VisConfig.size_range
                height_range = VisConfig.height_range
                elevation_scale = VisConfig.elevation_scale
                if outline:
                    stroked = True
                else:
                    stroked = False
                    stroke_color = None
                wireframe, enable3_d = [False] * 2
                fixed_radius, outline = [None] * 2
        elif isinstance(symbol, QgsLineSymbol):
            fill_rgb, stroke_alpha = extract_color(properties['line_color'])
            opacity = round(symbol_opacity * stroke_alpha, 2)
            stroke_opacity = opacity
            thickness = self._convert_to_pixels(
                float(properties['line_width']), properties['line_width_unit'])

            size_range = VisConfig.size_range
            height_range = VisConfig.height_range
            elevation_scale = VisConfig.elevation_scale
            stroked = True
            wireframe, enable3_d, filled = [False] * 3
            stroke_color, fixed_radius, outline = [None] * 3
        else:
            raise QgsPluginNotImplementedException(tr(
                'Symbol type {} is not supported yet', symbol.type()),
                                                   bar_msg=bar_msg())

        thickness = thickness if thickness > 0.0 else VisConfig.thickness

        vis_config = VisConfig(opacity, stroke_opacity, thickness,
                               stroke_color, color_range, color_range, radius,
                               size_range, radius_range, height_range,
                               elevation_scale, stroked, filled, enable3_d,
                               wireframe, fixed_radius, outline)

        return fill_rgb, vis_config