def MultiPartColorRamp_to_QgsColorRamp(ramp: MultiPartColorRamp): # pylint: disable=too-many-branches """ Converts a MultiPartColorRamp to a QgsColorRamp """ total_length = 0 start_color = None end_color = None for i, p in enumerate(ramp.parts): if not isinstance(p, (AlgorithmicColorRamp, PresetColorRamp)): raise NotImplementedException( 'Converting MultiPartColorRamp with a {} part is not supported' .format(p.__class__.__name__)) if len(ramp.part_lengths) > i: total_length += ramp.part_lengths[i] else: total_length += 1 if not start_color: if isinstance(p, AlgorithmicColorRamp): start_color = ColorConverter.color_to_qcolor(p.color1) elif isinstance(p, PresetColorRamp): start_color = ColorConverter.color_to_qcolor(p.colors[0]) if isinstance(p, AlgorithmicColorRamp): end_color = ColorConverter.color_to_qcolor(p.color2) elif isinstance(p, PresetColorRamp): end_color = ColorConverter.color_to_qcolor(p.colors[-1]) out = QgsGradientColorRamp(start_color, end_color) stops = [] current_length = 0 for i, p in enumerate(ramp.parts[:-1]): if len(ramp.part_lengths) > i: this_length = ramp.part_lengths[i] else: this_length = 1 if isinstance(p, PresetColorRamp): color_length = this_length / len(p.colors) / total_length for j, c in enumerate(p.colors): stops.append( QgsGradientStop( current_length / total_length + color_length * j, ColorConverter.color_to_qcolor(c))) stops.append( QgsGradientStop( (current_length / total_length + color_length * (j + 1)) * 0.999999, ColorConverter.color_to_qcolor(c))) current_length += this_length current_offset = current_length / total_length if isinstance(p, AlgorithmicColorRamp): stops.append( QgsGradientStop(current_offset, ColorConverter.color_to_qcolor(p.color2))) out.setStops(stops) return out
def convert_gradient_type(gradient_type: int) -> str: """ Converts a gradient type to string """ if gradient_type == GradientFillSymbol.LINEAR: return 'linear' elif gradient_type == GradientFillSymbol.CIRCULAR: return 'circular' elif gradient_type == GradientFillSymbol.BUFFERED: return 'buffered' elif gradient_type == GradientFillSymbol.RECTANGULAR: return 'rectangular' raise NotImplementedException( 'Gradient type {} not implemented yet'.format(gradient_type))
def ColorRamp_to_QgsColorRamp(ramp: ColorRamp): """ Converts a ColorRamp to a QgsColorRamp """ if isinstance(ramp, PresetColorRamp): return ColorRampConverter.PresetColorRamp_to_QgsColorRamp(ramp) elif isinstance(ramp, RandomColorRamp): return ColorRampConverter.RandomColorRamp_to_QgsColorRamp(ramp) elif isinstance(ramp, AlgorithmicColorRamp): return ColorRampConverter.AlgorithmicColorRamp_to_QgsColorRamp( ramp) elif isinstance(ramp, MultiPartColorRamp): return ColorRampConverter.MultiPartColorRamp_to_QgsColorRamp(ramp) else: raise NotImplementedException( 'Converting {} not implemented yet'.format( ramp.__class__.__name__))
def to_dict(self): raise NotImplementedException('{} not implemented yet'.format( self.__class__))
def wrapper(*args, **kwargs): # pylint: disable=unused-argument """ Wrapper which raises a NotImplementedException on function call """ raise NotImplementedException( '{} objects are not yet supported'.format(cls.__name__))
def to_dict(self) -> Optional[dict]: # pylint: disable=method-hidden """ Converts the object to a dictionary """ raise NotImplementedException( '{} objects are not yet supported'.format(self.__class__.__name__))
def to_dict(self): # pylint: disable=method-hidden raise NotImplementedException('{} not implemented yet'.format(self.__class__))
def append_decoration(symbol, decoration: SimpleLineDecorationElement, context: Context, enabled: bool, locked: bool): """ Appends decorations to the given symbol """ positions = decoration.marker_positions[:] from slyr_community.converters.symbols import SymbolConverter marker = SymbolConverter.Symbol_to_QgsSymbol(decoration.marker, context) if decoration.flip_all: for l in range(marker.symbolLayerCount()): layer = marker.symbolLayer(l) layer.setAngle(layer.angle() + 180) if 0 in positions: # start marker line = QgsMarkerLineSymbolLayer(not decoration.fixed_angle) start_marker = marker.clone() if decoration.flip_first: for l in range(marker.symbolLayerCount()): layer = start_marker.symbolLayer(l) layer.setAngle(layer.angle() + 180) for l in range(start_marker.symbolLayerCount()): layer = start_marker.symbolLayer(l) if layer.offset().x() or layer.offset().y(): # adjust marker offset to account for rotation of line markers offset = ConversionUtils.adjust_offset_for_rotation( layer.offset(), layer.angle()) if decoration.flip_first or decoration.flip_all: offset.setY(-offset.y()) layer.setOffset(offset) line.setSubSymbol(start_marker) line.setEnabled(enabled) line.setLocked(locked) # TODO - maybe need to offset this by marker width / 4? seems a better match to ESRI line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex) symbol.appendSymbolLayer(line) if 1 in positions: # end marker line = QgsMarkerLineSymbolLayer(not decoration.fixed_angle) end_marker = marker.clone() for l in range(end_marker.symbolLayerCount()): layer = end_marker.symbolLayer(l) if layer.offset().x() or layer.offset().y(): # adjust marker offset to account for rotation of line markers offset = ConversionUtils.adjust_offset_for_rotation( layer.offset(), layer.angle()) if decoration.flip_all: offset.setY(-offset.y()) layer.setOffset(offset) line.setSubSymbol(end_marker) line.setPlacement(QgsMarkerLineSymbolLayer.LastVertex) line.setEnabled(enabled) line.setLocked(locked) # TODO - maybe need to offset this by marker width / 4? seems a better match to ESRI symbol.appendSymbolLayer(line) if 0.5 in positions: # mid marker line = QgsMarkerLineSymbolLayer(not decoration.fixed_angle) end_marker = marker.clone() for l in range(end_marker.symbolLayerCount()): layer = end_marker.symbolLayer(l) if layer.offset().x() or layer.offset().y(): # adjust marker offset to account for rotation of line markers offset = ConversionUtils.adjust_offset_for_rotation( layer.offset(), layer.angle()) if decoration.flip_all: offset.setY(-offset.y()) layer.setOffset(offset) line.setSubSymbol(end_marker) line.setPlacement(QgsMarkerLineSymbolLayer.CentralPoint) line.setEnabled(enabled) line.setLocked(locked) symbol.appendSymbolLayer(line) # TODO other positions other_positions = [p for p in positions if p not in (0, 0.5, 1)] if other_positions: # Would need to use data defined marker placement distance, e.g. $length/3 # and offset first marker by $length/3 to avoid placing a marker at the start # of the line raise NotImplementedException( 'Non start/end decoration positions are not implemented (need {})' .format(other_positions))