Example #1
0
 def time_window(self):
     page = self.datepicker_page
     start = jsdt.datetime_to_js(
         datetime.datetime.strptime(
             self.request.session[page]['datepicker']['start'], '%d-%m-%Y'))
     end = jsdt.datetime_to_js(
         datetime.datetime.strptime(
             self.request.session[page]['datepicker']['end'], '%d-%m-%Y'))
     return {'start': start, 'end': end}
Example #2
0
 def time_window(self):
     start = jsdt.datetime_to_js(dt.datetime.strptime(
         self.request.session['startpage']['datepicker']['start'], '%d-%m-%Y'
     ))
     end = jsdt.datetime_to_js(dt.datetime.strptime(
         self.request.session['startpage']['datepicker']['end'], '%d-%m-%Y'
     ))
     return {
         'start': start,
         'end': end
     }
Example #3
0
 def series_to_js(self, npseries, index, key, color='#2980b9', dates=True):
     values = [{'x': jsdt.datetime_to_js(index[i]) if dates else i,
                'y': float(value)}
             for i, value in enumerate(npseries)]
     return {
         'values': values,
         'key': key,
         'color': color
     }
Example #4
0
 def series_to_js(self, npseries, index, key, color='#2980b9', dates=True):
     try:
         values = [{
             'x':
             jsdt.datetime_to_js(index[i]) if dates else float(index[i]),
             'y':
             float(value)
         } for i, value in enumerate(npseries)]
     except IndexError:
         values = [{
             'x': i,
             'y': float(value)
         } for i, value in enumerate(npseries)]
     return {'values': values, 'key': key, 'color': color}
Example #5
0
    def bbox(self,
             south_west,
             north_east,
             statistic=None,
             start='0001-01-01T00:00:00Z',
             end=None,
             organisation=None):
        """
        Find all timeseries within a certain bounding box.
        Returns records within bounding box using Bounding Box format (min Lon,
        min Lat, max Lon, max Lat). Also returns features with overlapping
        geometry.
        :param south_west: lattitude and longtitude of the south-western point
        :param north_east: lattitude and longtitude of the north-eastern point
        :param start_: start timestamp in ISO 8601 format
        :param end: end timestamp in ISO 8601 format
        :return: a dictionary of the api-response.
        """
        if not end:
            end = jsdatetime.now_iso()
        if isinstance(start, int):
            start -= 10000
        if isinstance(end, int):
            end += 10000

        min_lat, min_lon = south_west
        max_lat, max_lon = north_east

        polygon_coordinates = [
            [min_lon, min_lat],
            [min_lon, max_lat],
            [max_lon, max_lat],
            [max_lon, min_lat],
            [min_lon, min_lat],
        ]
        points = [' '.join([str(x), str(y)]) for x, y in polygon_coordinates]
        geom_within = {'a': 'POLYGON ((' + ', '.join(points) + '))'}
        geom_within = urllib.parse.urlencode(geom_within).split('=')[1]
        org_query = self.organisation_query(organisation)
        self.statistic = statistic
        if statistic == 'mean':
            statistic = ['count', 'sum']
        elif not statistic:
            statistic = ['min', 'max', 'count', 'sum']
            self.statistic = None
        elif statistic == 'range (max - min)':
            statistic = ['min', 'max']
        elif statistic == 'difference (last - first)':
            statistic = 'count'
        elif statistic == 'difference (mean last - first year)':
            year = dt.timedelta(days=366)
            first_end = jsdatetime.datetime_to_js(
                jsdatetime.js_to_datetime(start) + year)
            last_start = jsdatetime.datetime_to_js(
                jsdatetime.js_to_datetime(end) - year)
            self.get(start=start,
                     end=first_end,
                     min_points=1,
                     fields=['count', 'sum'],
                     location__geom_within=geom_within,
                     **org_query)
            first_year = {}
            for r in self.results:
                try:
                    first_year[r['location']['uuid']] = {
                        'first_value_timestamp': r['first_value_timestamp'],
                        'mean': r['events'][0]['sum'] / r['events'][0]['count']
                    }
                except IndexError:
                    first_year[r['location']['uuid']] = {
                        'first_value_timestamp': np.nan,
                        'mean': np.nan
                    }
            self.results = []
            self.get(start=last_start,
                     end=end,
                     min_points=1,
                     fields=['count', 'sum'],
                     location__geom_within=geom_within,
                     **org_query)
            for r in self.results:
                try:
                    r['events'][0]['difference (mean last - first year)'] = \
                        r['events'][0]['sum'] / r['events'][0]['count'] - \
                        first_year[r['location']['uuid']]['mean']
                    r['first_value_timestamp'] = \
                        first_year[
                            r['location']['uuid']]['first_value_timestamp']
                except IndexError:
                    r['events'] = [{
                        'difference (mean last - first year)':
                        np.nan
                    }]
                    r['first_value_timestamp'] = np.nan
                    r['last_value_timestamp'] = np.nan
            return

        self.get(start=start,
                 end=end,
                 min_points=1,
                 fields=statistic,
                 location__geom_within=geom_within,
                 **org_query)