コード例 #1
0
def pb_timestampfield(
        rel_type: FilterRelationship,
        value: Union[datetime.datetime, datetime.date] = None,
        start: Union[datetime.datetime, datetime.date] = None,
        end: Union[datetime.datetime, datetime.date] = None,
        sort_direction: SortDirection = SortDirection.NOT_SORTED,
        tzinfo: datetime.timezone = datetime.timezone.utc) -> TimestampFilter:
    """
    Create a protobuf query filter for a timestamp or a range of timestamps. If you use a datetime.date as
    the value combined with a rel_type of EQ then you will be creating a query filter for the
    24 period of that date.
    :param rel_type: the relationship type to query more
    [here](https://geo-grpc.github.io/api/#epl.protobuf.FieldRelationship)
    :param value: time to search by using >, >=, <, <=, etc. cannot be used with start or end
    :param start: start time for between/not between query. cannot be used with value
    :param end: end time for between/not between query. cannot be used with value
    :param sort_direction: sort direction for results. Defaults to not sorting by this field
    :param tzinfo: timezone info, defaults to UTC
    :return: TimestampFilter
    """
    if rel_type in UNSUPPORTED_TIME_FILTERS:
        raise ValueError("unsupported relationship type: {}".format(
            rel_type.name))

    if value is not None and rel_type != FilterRelationship.EQ and rel_type != FilterRelationship.NEQ:
        if not isinstance(value, datetime.datetime):
            if rel_type == FilterRelationship.GTE or rel_type == FilterRelationship.LT:
                return TimestampFilter(value=pb_timestamp(value,
                                                          tzinfo,
                                                          b_force_min=True),
                                       rel_type=rel_type,
                                       sort_direction=sort_direction)
            elif rel_type == FilterRelationship.LTE or rel_type == FilterRelationship.GT:
                return TimestampFilter(value=pb_timestamp(value,
                                                          tzinfo,
                                                          b_force_min=False),
                                       rel_type=rel_type,
                                       sort_direction=sort_direction)
        return TimestampFilter(value=pb_timestamp(value, tzinfo),
                               rel_type=rel_type,
                               sort_direction=sort_direction)
    elif value is not None and not isinstance(value, datetime.datetime) and \
            (rel_type == FilterRelationship.EQ or rel_type == FilterRelationship.NEQ):
        start = datetime.datetime.combine(value,
                                          datetime.datetime.min.time(),
                                          tzinfo=tzinfo)
        end = datetime.datetime.combine(value,
                                        datetime.datetime.max.time(),
                                        tzinfo=tzinfo)
        if rel_type == FilterRelationship.EQ:
            rel_type = FilterRelationship.BETWEEN
        else:
            rel_type = FilterRelationship.NOT_BETWEEN

    return TimestampFilter(start=pb_timestamp(start, tzinfo),
                           end=pb_timestamp(end, tzinfo),
                           rel_type=rel_type,
                           sort_direction=sort_direction)
コード例 #2
0
 def test_datetime_GT(self):
     bdt = datetime(2015, 11, 3, 1, 1, 1, tzinfo=timezone.utc)
     observed_range = TimestampFilter(value=utils.pb_timestamp(bdt),
                                      rel_type=FilterRelationship.GT)
     stac_request = StacRequest(observed=observed_range)
     stac_item = client.search_one(stac_request)
     self.assertIsNotNone(stac_item)
     self.assertLessEqual(
         utils.pb_timestamp(bdt).seconds, stac_item.datetime.seconds)
コード例 #3
0
 def test_datetime_not_range(self):
     start = datetime(2013, 4, 1, 12, 45, 59, tzinfo=timezone.utc)
     end = datetime(2014, 4, 1, 12, 45, 59, tzinfo=timezone.utc)
     observed_range = TimestampFilter(
         start=utils.pb_timestamp(start),
         end=utils.pb_timestamp(end),
         rel_type=FilterRelationship.NOT_BETWEEN)
     stac_request = StacRequest(observed=observed_range, limit=5)
     for stac_item in client.search(stac_request):
         print(
             datetime.fromtimestamp(stac_item.datetime.seconds,
                                    tz=timezone.utc))
         self.assertTrue(
             utils.pb_timestamp(end).seconds < stac_item.datetime.seconds or
             utils.pb_timestamp(start).seconds > stac_item.datetime.seconds)
コード例 #4
0
 def test_observed_not_range_desc(self):
     start = datetime(2013, 4, 1, 12, 45, 59, tzinfo=timezone.utc)
     end = datetime(2014, 4, 1, 12, 45, 59, tzinfo=timezone.utc)
     observed_range = TimestampFilter(
         start=utils.pb_timestamp(start),
         end=utils.pb_timestamp(end),
         rel_type=FilterRelationship.NOT_BETWEEN,
         sort_direction=enum.SortDirection.DESC)
     stac_request = StacRequest(observed=observed_range, limit=5)
     count = 0
     for stac_item in client.search(stac_request):
         count += 1
         print(
             datetime.fromtimestamp(stac_item.observed.seconds,
                                    tz=timezone.utc))
         self.assertTrue(
             utils.pb_timestamp(end).seconds < stac_item.observed.seconds)
     self.assertEqual(count, 5)
コード例 #5
0
    def test_count_more(self):
        start = datetime(2014, 4, 1, 12, 45, 59, tzinfo=timezone.utc)
        end = datetime(2014, 4, 1, 12, 52, 59, tzinfo=timezone.utc)
        observed_range = TimestampFilter(start=utils.pb_timestamp(start),
                                         end=utils.pb_timestamp(end),
                                         rel_type=FilterRelationship.BETWEEN)

        stac_request = StacRequest(observed=observed_range,
                                   limit=40,
                                   landsat=LandsatRequest())
        for stac_item in client.search(stac_request):
            self.assertEquals(Mission.LANDSAT, stac_item.mission_enum)
            print(
                datetime.fromtimestamp(stac_item.datetime.seconds,
                                       tz=timezone.utc))
            self.assertGreaterEqual(
                utils.pb_timestamp(end).seconds, stac_item.datetime.seconds)
            self.assertLessEqual(
                utils.pb_timestamp(start).seconds, stac_item.datetime.seconds)

        self.assertEquals(12, client.count(stac_request))