def format_impact_summary(self):
        """The impact summary as per category

        :returns: The impact summary.
        :rtype: safe.message.Message
        """
        flat_table = FlatTable().from_dict(
            groups=self.impact_table['groups'],
            data=self.impact_table['data'],
        )

        LOGGER.debug(self.impact_table['groups'])
        LOGGER.debug(self.impact_table['data'])

        LOGGER.debug(flat_table.groups)
        LOGGER.debug(flat_table.data)

        pivot_table = PivotTable(
            flat_table,
            row_field='landcover',
            column_field='hazard',
            columns=self.ordered_columns,
            affected_columns=self.affected_columns)

        report = {'impacted': pivot_table}

        # breakdown by zones
        if self.zone_field is not None:
            report['impacted_zones'] = {}
            for zone in flat_table.group_values('zone'):
                table = PivotTable(
                    flat_table,
                    row_field="landcover",
                    column_field='hazard',
                    columns=self.ordered_columns,
                    affected_columns=self.affected_columns,
                    filter_field="zone",
                    filter_value=zone)
                report['impacted_zones'][zone] = table

        message = m.Message(style_class='container')
        affected_text = tr('Affected Area (ha)')

        show_affected = True if len(self.affected_columns) else False
        if show_affected:
            msg = tr(
                '* Percentage of affected area compared to the total area for '
                'the land cover type.')
            self.notes['fields'].append(msg)

        table = format_pivot_table(
            report['impacted'],
            header_text=affected_text,
            total_columns=True,
            total_affected=show_affected,
            total_percent_affected=show_affected,
            bar_chart=False)
        message.add(table)

        if 'impacted_zones' in report:
            message.add(m.Heading(
                tr('Analysis Results by Aggregation Area'), **INFO_STYLE))
            for zone, table in report['impacted_zones'].items():
                message.add(m.Heading(zone.lower().title(), **SUB_INFO_STYLE))
                m_table = format_pivot_table(
                    table,
                    header_text=affected_text,
                    total_columns=True,
                    total_affected=show_affected,
                    total_percent_affected=show_affected,
                    bar_chart=False)
                message.add(m_table)

        return message.to_html(suppress_newlines=True)
Exemple #2
0
    def format_impact_summary(self):
        """The impact summary as per category

        :returns: The impact summary.
        :rtype: safe.message.Message
        """
        flat_table = FlatTable().from_dict(
            groups=self.impact_table['groups'],
            data=self.impact_table['data'],
        )

        LOGGER.debug(self.impact_table['groups'])
        LOGGER.debug(self.impact_table['data'])

        LOGGER.debug(flat_table.groups)
        LOGGER.debug(flat_table.data)

        pivot_table = PivotTable(flat_table,
                                 row_field='landcover',
                                 column_field='hazard',
                                 columns=self.ordered_columns,
                                 affected_columns=self.affected_columns)

        report = {'impacted': pivot_table}

        # breakdown by zones
        if self.zone_field is not None:
            report['impacted_zones'] = {}
            for zone in flat_table.group_values('zone'):
                table = PivotTable(flat_table,
                                   row_field="landcover",
                                   column_field='hazard',
                                   columns=self.ordered_columns,
                                   affected_columns=self.affected_columns,
                                   filter_field="zone",
                                   filter_value=zone)
                report['impacted_zones'][zone] = table

        message = m.Message(style_class='container')
        affected_text = tr('Affected Area (ha)')

        show_affected = True if len(self.affected_columns) else False
        if show_affected:
            msg = tr(
                '* Percentage of affected area compared to the total area for '
                'the land cover type.')
            self.notes['fields'].append(msg)

        table = format_pivot_table(report['impacted'],
                                   header_text=affected_text,
                                   total_columns=True,
                                   total_affected=show_affected,
                                   total_percent_affected=show_affected,
                                   bar_chart=False)
        message.add(table)

        if 'impacted_zones' in report:
            message.add(
                m.Heading(tr('Analysis Results by Aggregation Area'),
                          **INFO_STYLE))
            for zone, table in report['impacted_zones'].items():
                message.add(m.Heading(zone.lower().title(), **SUB_INFO_STYLE))
                m_table = format_pivot_table(
                    table,
                    header_text=affected_text,
                    total_columns=True,
                    total_affected=show_affected,
                    total_percent_affected=show_affected,
                    bar_chart=False)
                message.add(m_table)

        return message.to_html(suppress_newlines=True)