Exemplo n.º 1
0
    def testReadWrite(self):
        props = QgsVectorLayerTemporalProperties()
        props.setMode(QgsVectorLayerTemporalProperties.
                      ModeFeatureDateTimeInstantFromField)
        props.setFixedTemporalRange(
            QgsDateTimeRange(QDateTime(QDate(2019, 3, 4), QTime(11, 12, 13)),
                             QDateTime(QDate(2020, 5, 6), QTime(8, 9, 10))))
        props.setStartField('start')
        props.setEndField('end')
        props.setDurationField('duration')
        props.setDurationUnits(QgsUnitTypes.TemporalWeeks)
        props.setFixedDuration(5.6)
        props.setAccumulateFeatures(True)
        props.setStartExpression('start exp')
        props.setEndExpression('end exp')

        # save to xml
        doc = QDomDocument("testdoc")
        elem = doc.createElement('test')
        elem = props.writeXml(elem, doc, QgsReadWriteContext())

        # restore from xml
        props2 = QgsVectorLayerTemporalProperties()
        self.assertTrue(props2.readXml(elem, QgsReadWriteContext()))

        self.assertEqual(props2.mode(), props.mode())
        self.assertEqual(props2.fixedTemporalRange(),
                         props.fixedTemporalRange())
        self.assertEqual(props2.startField(), props.startField())
        self.assertEqual(props2.endField(), props.endField())
        self.assertEqual(props2.durationField(), props.durationField())
        self.assertEqual(props2.durationUnits(), props.durationUnits())
        self.assertEqual(props2.fixedDuration(), props.fixedDuration())
        self.assertEqual(props2.accumulateFeatures(),
                         props.accumulateFeatures())
        self.assertEqual(props2.startExpression(), props.startExpression())
        self.assertEqual(props2.endExpression(), props.endExpression())
Exemplo n.º 2
0
    def testStartAndDurationMode(self):
        layer = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer&field=start_field:datetime&field=duration:double",
            "test", "memory")
        self.assertTrue(layer.isValid())
        self.assertEqual(layer.fields()[2].type(), QVariant.DateTime)
        self.assertEqual(layer.fields()[3].type(), QVariant.Double)

        range = QgsDateTimeRange(
            QDateTime(QDate(2019, 3, 4), QTime(11, 12, 13)),
            QDateTime(QDate(2020, 5, 6), QTime(8, 9, 10)))

        props = QgsVectorLayerTemporalProperties(enabled=False)
        props.setMode(QgsVectorLayerTemporalProperties.
                      ModeFeatureDateTimeStartAndDurationFromFields)
        props.setStartField('start_field')
        props.setDurationField('duration')
        props.setDurationUnits(QgsUnitTypes.TemporalMilliseconds)
        self.assertFalse(props.createFilterString(layer, range))

        props.setIsActive(True)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,0,0,0,0,0,"duration"/1000) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalSeconds)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,0,0,0,0,0,"duration") >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalMinutes)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,0,0,0,0,"duration",0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalHours)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,0,0,0,"duration",0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalDays)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,0,0,"duration",0,0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalWeeks)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,0,"duration",0,0,0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalMonths)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(0,"duration",0,0,0,0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalYears)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval("duration",0,0,0,0,0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalDecades)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(10 * "duration",0,0,0,0,0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )

        props.setDurationUnits(QgsUnitTypes.TemporalCenturies)
        self.assertEqual(
            props.createFilterString(layer, range),
            '("start_field" <= make_datetime(2020,5,6,8,9,10) OR "start_field" IS NULL) AND (("start_field" + make_interval(100 * "duration",0,0,0,0,0,0) >= make_datetime(2019,3,4,11,12,13)) OR "duration" IS NULL)'
        )