def __init__(self, size=40, field='', context=None): QgsFeatureRenderer.__init__(self, 'MilStd2525Renderer') self.field = field self.size = int(size) self._defaultSymbol = getDefaultSymbol(int(size)) self.cached = {} self.context = context
def __init__(self, width, height, column_mapping=None, style_file=None, symbology=None, parent=None): """ Parameters ---------- width: int Width in pixels height: int Height in pixels column_mapping: dict Layer column mapping with the following keys: - rock_code_column : name of the column which holds the rock code - formation_code_column : name of the column which holds the formation code - rock_description_column - formation_description_column style_file: str File name of the QGIS style file to load symbology: QDomDocument QGIS style, as XML document parent: QObject: Qt parent object """ LogItem.__init__(self, parent) self.__width = width self.__height = height self.__min_z = 0 self.__max_z = 100 self.__data = None self.__layer = None self.__column_mapping = column_mapping if column_mapping is not None else {} # change current directory, so that relative paths to SVG get correctly resolved os.chdir(os.path.dirname(__file__)) if style_file: doc = QDomDocument() doc.setContent(open(style_file, "r").read()) self.__renderer = QgsFeatureRenderer.load(doc.documentElement(), QgsReadWriteContext()) elif symbology: self.__renderer = QgsFeatureRenderer.load( symbology.documentElement(), QgsReadWriteContext()) else: self.__renderer = QgsFeatureRenderer.defaultRenderer( POLYGON_RENDERER)
def get_style_from_qml(self, qml_name): renderer = None labeling = None style_path = os.path.join(STYLES_DIR, qml_name + '.qml') file = QFile(style_path) if not file.open(QIODevice.ReadOnly | QIODevice.Text): self.logger.warning(__name__, "Unable to read style file from {}".format(style_path)) doc = QDomDocument() doc.setContent(file) file.close() doc_elem = doc.documentElement() nodes = doc_elem.elementsByTagName("renderer-v2") if nodes.count(): renderer_elem = nodes.at(0).toElement() renderer = QgsFeatureRenderer.load(renderer_elem, QgsReadWriteContext()) nodes = doc_elem.elementsByTagName("labeling") if nodes.count(): labeling_elem = nodes.at(0).toElement() labeling = QgsAbstractVectorLayerLabeling.create(labeling_elem, QgsReadWriteContext()) return (renderer, labeling)
def on_load_style(self): fn, _ = QFileDialog.getOpenFileName(self, "Fichier style à charger", filter = "*.xml") if fn: doc = QDomDocument() doc.setContent(open(fn, "r").read()) self.__renderer = QgsFeatureRenderer.load(doc.documentElement(), QgsReadWriteContext()) for i, c in enumerate(self.__classes): _, cls, wcls = c if self.__renderer.__class__ == cls: new_widget = wcls.create(self.__layer, self.__styles[i], self.__renderer) idx = i break # replace old widget self.__sw.removeWidget(self.__sw.widget(idx)) self.__sw.insertWidget(idx, new_widget) self.__sw.setCurrentIndex(idx) self.__combo.setCurrentIndex(idx)
def __init__(self, size=QSizeF(400, 200), render_type=POINT_RENDERER, x_orientation=ORIENTATION_LEFT_TO_RIGHT, y_orientation=ORIENTATION_UPWARD, allow_mouse_translation=False, allow_wheel_zoom=False, symbology=None, parent=None): """ Parameters ---------- size: QSize Size of the item render_type: Literal[POINT_RENDERER, LINE_RENDERER, POLYGON_RENDERER] Type of renderer x_orientation: Literal[ORIENTATION_LEFT_TO_RIGHT, ORIENTATION_RIGHT_TO_LEFT] y_orientation: Literal[ORIENTATION_UPWARD, ORIENTATION_DOWNWARD] allow_mouse_translation: bool Allow the user to translate the item with the mouse (??) allow_wheel_zoom: bool Allow the user to zoom with the mouse wheel symbology: QDomDocument QGIS symbology to use for the renderer parent: QObject """ LogItem.__init__(self, parent) self.__item_size = size self.__data_rect = None self.__data = None self.__delta = None self.__x_orientation = x_orientation self.__y_orientation = y_orientation # origin point of the graph translation, if any self.__translation_orig = None self.__render_type = render_type # type: Literal[POINT_RENDERER, LINE_RENDERER, POLYGON_RENDERER] self.__allow_mouse_translation = allow_mouse_translation self.__allow_wheel_zoom = allow_wheel_zoom self.__layer = None self.__default_renderers = [QgsFeatureRenderer.defaultRenderer(POINT_RENDERER), QgsFeatureRenderer.defaultRenderer(LINE_RENDERER), QgsFeatureRenderer.defaultRenderer(POLYGON_RENDERER)] symbol = self.__default_renderers[1].symbol() symbol.setWidth(1.0) symbol = self.__default_renderers[0].symbol() symbol.setSize(5.0) symbol = self.__default_renderers[2].symbol() symbol.symbolLayers()[0].setStrokeWidth(1.0) if not symbology: self.__renderer = self.__default_renderers[self.__render_type] else: self.__renderer = QgsFeatureRenderer.load(symbology.documentElement(), QgsReadWriteContext()) # index of the current point to label self.__old_point_to_label = None self.__point_to_label = None
def __init__(self, layer, column_mapping, filter_expression=None, size=QSizeF(400, 200), render_type=POLYGON_RENDERER, x_orientation=ORIENTATION_LEFT_TO_RIGHT, y_orientation=ORIENTATION_UPWARD, symbology=None, parent=None): """ Parameters ---------- layer: QgsVectorLayer Input vector layer column_mapping: dict Layer column mapping with the following keys: - min_event_column : name of the column which holds the minimum x value - max_event_column : name of the column which holds the maximum x value - value_column : name of the column which holds the value filter_expression: str Filter expression to apply to the input layer size: QSize Size of the item render_type: Literal[LINE_RENDERER, POLYGON_RENDERER] Type of renderer x_orientation: Literal[ORIENTATION_LEFT_TO_RIGHT, ORIENTATION_RIGHT_TO_LEFT] y_orientation: Literal[ORIENTATION_UPWARD, ORIENTATION_DOWNWARD] symbology: QDomDocument QGIS symbology to use for the renderer parent: QObject """ LogItem.__init__(self, parent) self.__item_size = size self.__data_rect = None self.__data = None self.__x_orientation = x_orientation self.__y_orientation = y_orientation if render_type not in (LINE_RENDERER, POLYGON_RENDERER): raise RuntimeError( "Render type not supported: {}".format(render_type)) self.__render_type = render_type # type: Literal[POINT_RENDERER, LINE_RENDERER, POLYGON_RENDERER] self.__layer = layer self.__column_mapping = column_mapping self.__filter_expression = filter_expression self.__default_renderers = [ None, QgsFeatureRenderer.defaultRenderer(LINE_RENDERER), QgsFeatureRenderer.defaultRenderer(POLYGON_RENDERER) ] symbol = self.__default_renderers[1].symbol() symbol.setWidth(1.0) symbol = self.__default_renderers[2].symbol() symbol.symbolLayers()[0].setStrokeWidth(1.0) if not symbology: self.__renderer = self.__default_renderers[self.__render_type] else: self.__renderer = QgsFeatureRenderer.load( symbology.documentElement(), QgsReadWriteContext()) # values cache self.__min_x_values = [] self.__max_x_values = [] self.__y_values = [] # index of the current point to label self.__old_point_to_label = None self.__point_to_label = None # determine data window on loading # FIXME we should use the database when possible self.__min_x_field = column_mapping["min_event_column"] self.__max_x_field = column_mapping["max_event_column"] self.__y_field = column_mapping["value_column"] req = QgsFeatureRequest() req.setFilterExpression(filter_expression) req.setSubsetOfAttributes( [self.__min_x_field, self.__max_x_field, self.__y_field], layer.fields()) min_x, max_x = None, None min_y, max_y = None, None for f in layer.getFeatures(req): if min_x is None or f[self.__min_x_field] < min_x: min_x = f[self.__min_x_field] if max_x is None or f[self.__max_x_field] > max_x: max_x = f[self.__max_x_field] if min_y is None or f[self.__y_field] < min_y: min_y = f[self.__y_field] if max_y is None or f[self.__y_field] > max_y: max_y = f[self.__y_field] if min_x is None: return # Add a 10% buffer above max h = max_y - min_y if h == 0.0: max_y = min_y + 1.0 h = 1.0 max_y += h * 0.1 self.set_data_window(QRectF(min_x, min_y, max_x - min_x, max_y - min_y))