Пример #1
0
    def testLoadWriteRenderingScaleVisibility(self):
        """Test write and load scale visibility, see GH #33840"""

        vl = QgsVectorLayer("LineString?crs=epsg:4326", "result", "memory")
        vl.setScaleBasedVisibility(True)
        vl.setMinimumScale(125.0)
        vl.setMaximumScale(1.25)
        style = QDomDocument()
        style.setContent("<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'><qgis></qgis>")
        node = style.firstChild()
        self.assertTrue(vl.writeStyle(node, style, "Error writing style", QgsReadWriteContext(), QgsMapLayer.Rendering))

        style_content = style.toString()
        del(vl)

        # Read
        vl2 = QgsVectorLayer("LineString?crs=epsg:4326", "result", "memory")
        self.assertFalse(vl2.hasScaleBasedVisibility())
        style2 = QDomDocument()
        style2.setContent(style_content)
        self.assertTrue(vl2.readStyle(style.namedItem('qgis'), "Error reading style", QgsReadWriteContext(), QgsMapLayer.Rendering))
        self.assertTrue(vl2.hasScaleBasedVisibility())
        self.assertEqual(vl2.minimumScale(), 125.0)
        self.assertEqual(vl2.maximumScale(), 1.25)
Пример #2
0
def override_component_template(component, template_path):
    """Override a default component with a new component with given template.

    :param component: Component as dictionary.
    :type component: dict

    :param template_path: Custom template path that will be used.
    :type template_path: str

    :returns: New report component.
    :rtype: dict
    """
    copy_component = deepcopy(component)
    template_directory, template_filename = split(template_path)
    file_name, file_format = splitext(template_filename)
    if file_format[1:] != (
            QgisComposerComponentsMetadata.OutputFormat.QPT) or (
                not exists(template_path)):
        return copy_component

    # we do the import here to avoid circular import when starting
    # up the plugin
    from safe.definitions.reports.components import (
        map_report_component_boilerplate)
    custom_template_component = deepcopy(
        map_report_component_boilerplate)

    # we need to update several items in this component
    pdf_output_file = '{file_name}.pdf'.format(file_name=file_name)
    qpt_output_file = '{file_name}.qpt'.format(file_name=file_name)

    custom_template_component['key'] = file_name
    custom_template_component['template'] = template_path
    custom_template_component['output_path']['template'] = qpt_output_file
    custom_template_component['output_path']['map'] = pdf_output_file

    # we need to update the orientation of the custom template
    with open(custom_template_component['template']) as (
            template_file):
        template_content = template_file.read()
    document = QDomDocument()
    document.setContent(template_content)
    root_element = document.namedItem('Composer')
    composition_element = root_element.namedItem('Composition')
    all_orientations = [
        landscape_map_report_description,
        portrait_map_report_description
    ]
    orientation = None
    if isinstance(root_element, QDomNode):
        paper_width = composition_element.attributes().namedItem(
            'paperWidth').nodeValue()
        paper_height = composition_element.attributes().namedItem(
            'paperHeight').nodeValue()
        for _orientation in all_orientations:
            if _orientation['width'] == int(paper_width) and (
                    _orientation['height'] == int(paper_height)):
                orientation = _orientation['orientation']
                break

    # By default, the component is landscape oriented, So if we found that
    # the custom template is portrait, we need to delete the information about
    # orientation in the component because in the report metadata, if there is
    # no specification about the orientation, then they will set it
    # to portrait.
    if orientation == portrait_map_report_description['orientation']:
        custom_template_component['orientation'] = orientation
        del custom_template_component['page_width']
        del custom_template_component['page_height']

    copy_component['components'] = [custom_template_component]

    return copy_component
Пример #3
0
def override_component_template(component, template_path):
    """Override a default component with a new component with given template.

    :param component: Component as dictionary.
    :type component: dict

    :param template_path: Custom template path that will be used.
    :type template_path: str

    :returns: New report component.
    :rtype: dict
    """
    copy_component = deepcopy(component)
    template_directory, template_filename = split(template_path)
    file_name, file_format = splitext(template_filename)
    if file_format[1:] != (
            QgisComposerComponentsMetadata.OutputFormat.QPT) or (
                not exists(template_path)):
        return copy_component

    # we do the import here to avoid circular import when starting
    # up the plugin
    from safe.definitions.reports.components import (
        map_report_component_boilerplate)
    custom_template_component = deepcopy(
        map_report_component_boilerplate)

    # we need to update several items in this component
    pdf_output_file = '{file_name}.pdf'.format(file_name=file_name)
    qpt_output_file = '{file_name}.qpt'.format(file_name=file_name)

    custom_template_component['key'] = file_name
    custom_template_component['template'] = template_path
    custom_template_component['output_path']['template'] = qpt_output_file
    custom_template_component['output_path']['map'] = pdf_output_file

    # we need to update the orientation of the custom template
    with open(custom_template_component['template']) as (
            template_file):
        template_content = template_file.read()
    document = QDomDocument()
    document.setContent(template_content)
    root_element = document.namedItem('Composer')
    composition_element = root_element.namedItem('Composition')
    all_orientations = [
        landscape_map_report_description,
        portrait_map_report_description
    ]
    orientation = None
    if isinstance(root_element, QDomNode):
        paper_width = composition_element.attributes().namedItem(
            'paperWidth').nodeValue()
        paper_height = composition_element.attributes().namedItem(
            'paperHeight').nodeValue()
        for _orientation in all_orientations:
            if _orientation['width'] == int(paper_width) and (
                    _orientation['height'] == int(paper_height)):
                orientation = _orientation['orientation']
                break

    # By default, the component is landscape oriented, So if we found that
    # the custom template is portrait, we need to delete the information about
    # orientation in the component because in the report metadata, if there is
    # no specification about the orientation, then they will set it
    # to portrait.
    if orientation == portrait_map_report_description['orientation']:
        custom_template_component['orientation'] = orientation
        del custom_template_component['page_width']
        del custom_template_component['page_height']

    copy_component['components'] = [custom_template_component]

    return copy_component