Ejemplo n.º 1
0
    def test_vector_with_int_timestamps(self):
        layer = Mock()
        layer.subsetString.return_value = ""
        layer.minimumValue.return_value = 60
        layer.maximumValue.return_value = 260
        layer.uniqueValues.return_value = [60, 260]
        settings = ls.LayerSettings()
        settings.layer = layer
        settings.startTimeAttribute = self.from_attr
        settings.endTimeAttribute = self.to_attr
        vector = TimeVectorLayer(settings, iface=Mock())

        assert (vector.getTimeFormat() == UTC)
        td = timedelta(minutes=5)
        currTime = datetime(1970, 1, 1, 0, 3, 0)
        vector.setTimeRestriction(currTime, td)
        layer.setSubsetString.assert_called_with(INT_FORMAT.format(self.from_attr,
                                                                   self.comparison_op,
                                                                   datetime_to_epoch(
                                                                       currTime + td),
                                                                   self.to_attr,
                                                                   datetime_to_epoch(currTime)))

        currTime = datetime(1980, 1, 1, 0, 0, 2)

        vector.setTimeRestriction(currTime, td)
        layer.setSubsetString.assert_called_with(INT_FORMAT.format(self.from_attr,
                                                                   self.comparison_op,
                                                                   datetime_to_epoch(
                                                                       currTime + td),
                                                                   self.to_attr,
                                                                   datetime_to_epoch(
                                                                       currTime)))
Ejemplo n.º 2
0
    def test_vector_with_int_timestamps(self):
        layer = Mock()
        layer.subsetString.return_value = ""
        layer.minimumValue.return_value = 60
        layer.maximumValue.return_value = 260
        layer.uniqueValues.return_value = [60, 260]
        settings = ls.LayerSettings()
        settings.layer = layer
        settings.startTimeAttribute = self.from_attr
        settings.endTimeAttribute = self.to_attr
        vector = TimeVectorLayer(settings, iface=Mock())

        assert (vector.getTimeFormat() == UTC)
        td = timedelta(minutes=5)
        currTime = datetime(1970, 1, 1, 0, 3, 0)
        vector.setTimeRestriction(currTime, td)
        layer.setSubsetString.assert_called_with(
            INT_FORMAT.format(self.from_attr, self.comparison_op,
                              datetime_to_epoch(currTime + td), self.to_attr,
                              datetime_to_epoch(currTime)))

        currTime = datetime(1980, 1, 1, 0, 0, 2)

        vector.setTimeRestriction(currTime, td)
        layer.setSubsetString.assert_called_with(
            INT_FORMAT.format(self.from_attr, self.comparison_op,
                              datetime_to_epoch(currTime + td), self.to_attr,
                              datetime_to_epoch(currTime)))
Ejemplo n.º 3
0
    def test_vector_with_int_timestamps(self):
        layer = Mock()
        provider = Mock()
        layer.dataProvider.return_value = provider
        layer.subsetString.return_value = ""
        provider.minimumValue.return_value = 60
        provider.maximumValue.return_value = 260
        provider.storageType.return_value = 'PostgreSQL database with PostGIS extension'

        vector = TimeVectorLayer(layer,
                                 fromTimeAttribute=self.from_attr,
                                 toTimeAttribute=self.to_attr,
                                 enabled=True,
                                 timeFormat=DEFAULT_FORMAT,
                                 offset=0)

        assert (vector.getTimeFormat() == UTC)
        td = timedelta(minutes=5)
        currTime = datetime(1970, 1, 1, 0, 3, 0)
        vector.setTimeRestriction(currTime, td)
        layer.setSubsetString.assert_called_with(
            INT_FORMAT.format(self.from_attr, self.comparison_op,
                              datetime_to_epoch(currTime + td), self.to_attr,
                              datetime_to_epoch(currTime)))

        currTime = datetime(1980, 1, 1, 0, 0, 2)

        vector.setTimeRestriction(currTime, td)
        layer.setSubsetString.assert_called_with(
            INT_FORMAT.format(self.from_attr, self.comparison_op,
                              datetime_to_epoch(currTime + td), self.to_attr,
                              datetime_to_epoch(currTime)))
    def _test_spatialite_layer(self, attr, layer, is_int=False):

        timeLayer = timevectorlayer.TimeVectorLayer(layer, attr, attr, True,
                                                    time_util.DEFAULT_FORMAT,
                                                    0)
        self.tlm.registerTimeLayer(timeLayer)
        if is_int:
            self.assertEquals(timeLayer.getDateType(),
                              timevectorlayer.DateTypes.IntegerTimestamps)
            self.assertEquals(timeLayer.getTimeFormat(), time_util.UTC)
        else:
            self.assertEquals(timeLayer.getDateType(),
                              timevectorlayer.DateTypes.DatesAsStrings)
            self.assertEquals(timeLayer.getTimeFormat(),
                              time_util.DEFAULT_FORMAT)
        start_time = time_util.str_to_datetime(timeLayer.getMinMaxValues()[0],
                                               time_util.DEFAULT_FORMAT)
        self.assertEquals(time_util.epoch_to_datetime(STARTTIME), start_time)

        self.tlm.setTimeFrameType("minutes")
        self.tlm.stepForward()
        assert (start_time +
                timedelta(minutes=1) == self.tlm.getCurrentTimePosition())
        # only one feature is selected now, because there is one feature per minute
        self.assertEquals(layer.featureCount(), 1)
        FS = 5
        self.tlm.setTimeFrameSize(FS)
        # we have one feature per minute
        self.assertEquals(layer.featureCount(), FS)
        subsetString = layer.subsetString()

        if is_int:
            expectedSubsetString = INT_FORMAT.format(
                attr, self.comparison_op,
                time_util.datetime_to_epoch(self.tlm.getCurrentTimePosition() +
                                            timedelta(minutes=FS)), attr,
                time_util.datetime_to_epoch(self.tlm.getCurrentTimePosition()))

            self.assertEquals(subsetString, expectedSubsetString)
            minimum_bound_seconds = int(subsetString.split(" ")[6])
            self.assertEquals(
                self.tlm.getCurrentTimePosition(),
                time_util.epoch_to_datetime(minimum_bound_seconds))
        if not is_int:
            self.assertEqual(timeLayer.getTimeFormat(),
                             time_util.DEFAULT_FORMAT)
            expectedSubsetString = STRING_FORMAT.format(
                attr, self.comparison_op,
                time_util.datetime_to_str(
                    self.tlm.getCurrentTimePosition() + timedelta(minutes=FS),
                    timeLayer.getTimeFormat()), attr,
                time_util.datetime_to_str(self.tlm.getCurrentTimePosition(),
                                          timeLayer.getTimeFormat()))

            self.assertEquals(subsetString, expectedSubsetString)

        self.tlm.stepForward()
        self.assertEquals(layer.featureCount(), FS)
Ejemplo n.º 5
0
    def _test_spatialite_layer(self, attr, layer, is_int=False):

        settings = LayerSettings()
        settings.layer = layer
        settings.startTimeAttribute = attr
        settings.endTimeAttribute = attr

        timeLayer = timevectorlayer.TimeVectorLayer(settings, iface=Mock())
        self.tlm.registerTimeLayer(timeLayer)
        if is_int:
            self.assertEquals(timeLayer.getDateType(), timevectorlayer.DateTypes.IntegerTimestamps)
            self.assertEquals(timeLayer.getTimeFormat(), time_util.UTC)
        else:
            self.assertEquals(timeLayer.getDateType(), timevectorlayer.DateTypes.DatesAsStrings)
            self.assertEquals(timeLayer.getTimeFormat(), time_util.DEFAULT_FORMAT)
        start_time = time_util.str_to_datetime(timeLayer.getMinMaxValues()[0], time_util.PENDING)
        self.assertEquals(time_util.epoch_to_datetime(STARTTIME), start_time)

        self.tlm.setTimeFrameType("minutes")
        self.tlm.stepForward()
        assert start_time + timedelta(minutes=1) == self.tlm.getCurrentTimePosition()
        # only one feature is selected now, because there is one feature per minute
        self.assertEquals(layer.featureCount(), 1)
        FS = 5
        self.tlm.setTimeFrameSize(FS)
        # we have one feature per minute
        self.assertEquals(layer.featureCount(), FS)
        subsetString = layer.subsetString()

        if is_int:
            expectedSubsetString = INT_FORMAT.format(
                attr,
                self.comparison_op,
                time_util.datetime_to_epoch(self.tlm.getCurrentTimePosition() + timedelta(minutes=FS)),
                attr,
                time_util.datetime_to_epoch(self.tlm.getCurrentTimePosition()),
            )

            self.assertEquals(subsetString, expectedSubsetString)
            minimum_bound_seconds = int(subsetString.split(" ")[6])
            self.assertEquals(self.tlm.getCurrentTimePosition(), time_util.epoch_to_datetime(minimum_bound_seconds))
        if not is_int:
            self.assertEqual(timeLayer.getTimeFormat(), time_util.DEFAULT_FORMAT)
            expectedSubsetString = STRING_FORMAT.format(
                attr,
                self.comparison_op,
                time_util.datetime_to_str(
                    self.tlm.getCurrentTimePosition() + timedelta(minutes=FS), timeLayer.getTimeFormat()
                ),
                attr,
                time_util.datetime_to_str(self.tlm.getCurrentTimePosition(), timeLayer.getTimeFormat()),
            )

            self.assertEquals(subsetString, expectedSubsetString)

        self.tlm.stepForward()
        self.assertEquals(layer.featureCount(), FS)