示例#1
0
    def html(self, identifiers=None, layout_options=None):
        """
        Popup with graph - table - bargraph.
        """
        add_snippet = layout_options.get('add_snippet', False)

        parameter_name = self.parameter_name

        # Make table with given identifiers.
        # Layer options contain request - not the best way but it works.
        start_date, end_date = current_start_end_dates(
            layout_options['request'])

        # Convert start and end dates to utc.
        start_date_utc, end_date_utc = self._to_utc(start_date, end_date)

        td_windows = [datetime.timedelta(days=2),
                      datetime.timedelta(days=1),
                      datetime.timedelta(hours=3),
                      datetime.timedelta(hours=1)]

        info = []

        symbol_url = self.symbol_url()

        for identifier in identifiers:
            image_graph_url = self.workspace_mixin_item.url(
                "lizard_map_adapter_image", (identifier,))
            flot_graph_data_url = self.workspace_mixin_item.url(
                "lizard_map_adapter_flot_graph_data", (identifier,))

            values = self._cached_values(identifier,
                                         start_date_utc,
                                         end_date_utc)

            area_m2 = GeoObject.objects.get(
                municipality_id=identifier['location'],
                config=self.rainapp_config).geometry.area
            area_km2 = meter_square_to_km_square(area_m2)

            period_summary_row = {
                'max': sum([v['value'] for v in values]),
                'start': start_date,
                'end': end_date,
                'delta': (end_date - start_date).days,
                't': t_to_string(None),
            }
            infoname = '%s, %s' % (
                self._get_location_name(identifier), parameter_name)
            info.append({
                'identifier': identifier,
                'identifier_json': json.dumps(identifier).replace('"', '%22'),
                'shortname': infoname,
                'name': infoname,
                'location': self._get_location_name(identifier),
                'period_summary_row': period_summary_row,
                'table': [rain_stats(values,
                                     area_km2,
                                     td_window,
                                     start_date_utc,
                                     end_date_utc)
                          for td_window in td_windows],
                'image_graph_url': image_graph_url,
                'flot_graph_data_url': flot_graph_data_url,
                'url': self.workspace_mixin_item.url(
                        "lizard_map_adapter_values", [identifier, ],
                        extra_kwargs={'output_type': 'csv'}),
                'workspace_item': self.workspace_mixin_item,
                'adapter': self
            })

        return render_to_string(
            'lizard_rainapp/popup_rainapp.html',
            {
                'title': parameter_name,
                'symbol_url': symbol_url,
                'add_snippet': add_snippet,
                'workspace_item': self.workspace_item,
                'info': info
            }
        )
示例#2
0
    def html(self, identifiers=None, layout_options=None):
        """
        Popup with graph - table - bargraph.

        We're using the template of RainApp's popup, so this function was
        written to result in exactly the same context variables as RainApp's
        adapter results in.
        """
        add_snippet = layout_options.get('add_snippet', False)

        # Make table with given identifiers.
        # Layer options contain request - not the best way but it works.
        start_date, end_date = current_start_end_dates(
            layout_options['request'])

        # Convert start and end dates to utc.
        start_date_utc = dates.to_utc(start_date)
        end_date_utc = dates.to_utc(end_date)

        td_windows = [datetime.timedelta(days=2),
                      datetime.timedelta(days=1),
                      datetime.timedelta(hours=3),
                      datetime.timedelta(hours=1)]

        info = []

        symbol_url = self.symbol_url()

        for identifier in identifiers:
            logger.debug("IN HTML, identifier = {0}".format(identifier))
            image_graph_url = self.workspace_mixin_item.url(
                "lizard_map_adapter_image", (identifier,))
            flot_graph_data_url = self.workspace_mixin_item.url(
                "lizard_map_adapter_flot_graph_data", (identifier,))

            values = self.values(identifier, start_date_utc, end_date_utc)

            area_km2 = 1.0  # A defining feature of the Neerslagradar
                            # is that we always work in a 1km x 1km
                            # grid.

            period_summary_row = {
                'max': sum(v['value'] for v in values),
                'start': start_date,
                'end': end_date,
                'delta': (end_date - start_date).days,
                't': t_to_string(None),
            }
            infoname = self._grid_name(
                identifier['region_name'], identifier['identifier'])

            info.append({
                'identifier': identifier,
                'identifier_json': json.dumps(identifier).replace('"', '%22'),
                'shortname': infoname,
                'name': infoname,
                'location': infoname,
                'period_summary_row': period_summary_row,
                'table': [rain_stats(values,
                                     area_km2,
                                     td_window,
                                     start_date_utc,
                                     end_date_utc)
                          for td_window in td_windows],
                'image_graph_url': image_graph_url,
                'flot_graph_data_url': flot_graph_data_url,
                'url': self.workspace_mixin_item.url(
                        "lizard_map_adapter_values", [identifier, ],
                        extra_kwargs={'output_type': 'csv'}),
                'workspace_item': self.workspace_mixin_item,
                'adapter': self
            })

        return render_to_string(
            'lizard_rainapp/popup_rainapp.html',
            {
                'title': infoname,
                'symbol_url': symbol_url,
                'add_snippet': add_snippet,
                'workspace_item': self.workspace_item,
                'info': info
            }
        )