Skip to content
Permalink
Browse files

Add a time unit for "irregular steps"

This is a special "irregular step" time unit, used for temporal data
which uses irregular, non-real-world unit steps
  • Loading branch information
nyalldawson committed Mar 25, 2021
1 parent cae8aae commit d443e4a5363fe09c0a0d10047d8c353f98e64135
@@ -120,6 +120,7 @@ Helper functions for various unit types.
TemporalYears,
TemporalDecades,
TemporalCenturies,
TemporalIrregularStep,
TemporalUnknownUnit
};

@@ -1275,6 +1275,8 @@ QString QgsUnitTypes::encodeUnit( QgsUnitTypes::TemporalUnit unit )
return QStringLiteral( "dec" );
case TemporalCenturies:
return QStringLiteral( "c" );
case TemporalIrregularStep:
return QStringLiteral( "xxx" );
case TemporalUnknownUnit:
return QStringLiteral( "<unknown>" );
}
@@ -1308,6 +1310,8 @@ QgsUnitTypes::TemporalUnit QgsUnitTypes::decodeTemporalUnit( const QString &stri
return TemporalDecades;
if ( normalized == encodeUnit( TemporalCenturies ) )
return TemporalCenturies;
if ( normalized == encodeUnit( TemporalIrregularStep ) )
return TemporalIrregularStep;
if ( normalized == encodeUnit( TemporalUnknownUnit ) )
return TemporalUnknownUnit;

@@ -1341,6 +1345,8 @@ QString QgsUnitTypes::toString( QgsUnitTypes::TemporalUnit unit )
return QObject::tr( "decades", "temporal" );
case TemporalCenturies:
return QObject::tr( "centuries", "temporal" );
case TemporalIrregularStep:
return QObject::tr( "steps", "temporal" );
case TemporalUnknownUnit:
return QObject::tr( "<unknown>", "temporal" );
}
@@ -1371,6 +1377,8 @@ QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::TemporalUnit unit )
return QObject::tr( "dec", "temporal" );
case TemporalCenturies:
return QObject::tr( "cen", "temporal" );
case TemporalIrregularStep:
return QObject::tr( "steps", "temporal" );
case TemporalUnknownUnit:
return QObject::tr( "<unknown>", "temporal" );
}
@@ -1404,6 +1412,8 @@ QgsUnitTypes::TemporalUnit QgsUnitTypes::stringToTemporalUnit( const QString &st
return TemporalDecades;
if ( normalized == toString( TemporalCenturies ) )
return TemporalCenturies;
if ( normalized == toString( TemporalIrregularStep ) )
return TemporalIrregularStep;
if ( normalized == toString( TemporalUnknownUnit ) )
return TemporalUnknownUnit;

@@ -1442,6 +1452,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 3155760000.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1471,6 +1482,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 315576000000.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1500,6 +1512,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 52596000.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1529,6 +1542,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 876600.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1558,6 +1572,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 36525.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1587,6 +1602,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 7 / 36525.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1616,6 +1632,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 30 / 36525.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1645,6 +1662,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 0.01;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1674,6 +1692,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 0.1;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
@@ -1704,12 +1723,14 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
}

case TemporalUnknownUnit:
case TemporalIrregularStep:
{
return 1.0;
}
@@ -158,6 +158,7 @@ class CORE_EXPORT QgsUnitTypes
TemporalYears, //!< Years
TemporalDecades, //!< Decades
TemporalCenturies, //!< Centuries
TemporalIrregularStep, //!< Special "irregular step" time unit, used for temporal data which uses irregular, non-real-world unit steps (since QGIS 3.20)
TemporalUnknownUnit //!< Unknown time unit
};
Q_ENUM( TemporalUnit )
@@ -494,6 +494,7 @@ QString QgsVectorLayerTemporalProperties::createFilterString( const QgsVectorLay
break;

case QgsUnitTypes::TemporalUnknownUnit:
case QgsUnitTypes::TemporalIrregularStep:
return QString();
}
return QStringLiteral( "(%1 %2 %3 OR %1 IS NULL) AND ((%1 + %4 %5 %6) OR %7 IS NULL)" ).arg( QgsExpression::quotedColumnRef( mStartFieldName ),
@@ -287,6 +287,7 @@ def testEncodeDecodeTemporalUnits(self):
QgsUnitTypes.TemporalYears,
QgsUnitTypes.TemporalDecades,
QgsUnitTypes.TemporalCenturies,
QgsUnitTypes.TemporalIrregularStep,
QgsUnitTypes.TemporalUnknownUnit]

for u in units:
@@ -316,6 +317,7 @@ def testTemporalUnitsToFromString(self):
QgsUnitTypes.TemporalYears,
QgsUnitTypes.TemporalDecades,
QgsUnitTypes.TemporalCenturies,
QgsUnitTypes.TemporalIrregularStep,
QgsUnitTypes.TemporalUnknownUnit]

for u in units:
@@ -892,7 +894,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 3.170980821917834278e-11,
QgsUnitTypes.TemporalDecades: 3.170980821917834117e-12,
QgsUnitTypes.TemporalCenturies: 3.170980821917834319e-13,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalSeconds: {
QgsUnitTypes.TemporalMilliseconds: 1000.0,
@@ -905,7 +908,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 3.170980821917834046e-8,
QgsUnitTypes.TemporalDecades: 3.170980821917834046e-9,
QgsUnitTypes.TemporalCenturies: 3.170980821917834149e-10,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalMinutes: {
QgsUnitTypes.TemporalMilliseconds: 60000.0,
@@ -918,7 +922,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 1.902828841643645226e-6,
QgsUnitTypes.TemporalDecades: 1.902828841643645332e-7,
QgsUnitTypes.TemporalCenturies: 1.9028288416436452e-8,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalHours: {
QgsUnitTypes.TemporalMilliseconds: 3600000.0,
@@ -931,7 +936,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 0.00011407711613050422,
QgsUnitTypes.TemporalDecades: 1.141553424664109737e-5,
QgsUnitTypes.TemporalCenturies: 1.141553424664109737e-6,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalDays: {
QgsUnitTypes.TemporalMilliseconds: 8.64e+7,
@@ -944,7 +950,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 0.0027378507871321013,
QgsUnitTypes.TemporalDecades: 0.0002737850787132101,
QgsUnitTypes.TemporalCenturies: 2.739723287683189167e-5,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalWeeks: {
QgsUnitTypes.TemporalMilliseconds: 6.048e+8,
@@ -957,7 +964,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 0.019164955509924708,
QgsUnitTypes.TemporalDecades: 0.0019164955509924709,
QgsUnitTypes.TemporalCenturies: 0.0001916495550992471,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalMonths: {
QgsUnitTypes.TemporalMilliseconds: 2592000000.0,
@@ -970,7 +978,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 0.08213552361396304,
QgsUnitTypes.TemporalDecades: 0.008213552361396304,
QgsUnitTypes.TemporalCenturies: 0.0008213552361396304,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalYears: {
QgsUnitTypes.TemporalMilliseconds: 31557600000.0,
@@ -983,7 +992,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 1,
QgsUnitTypes.TemporalDecades: 0.1,
QgsUnitTypes.TemporalCenturies: 0.01,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalDecades: {
QgsUnitTypes.TemporalMilliseconds: 315576000000.0,
@@ -996,7 +1006,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 10,
QgsUnitTypes.TemporalDecades: 1,
QgsUnitTypes.TemporalCenturies: 0.1,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
},
QgsUnitTypes.TemporalCenturies: {
QgsUnitTypes.TemporalMilliseconds: 3155760000000.0,
@@ -1009,7 +1020,8 @@ def testTemporalFromUnitToUnitFactor(self):
QgsUnitTypes.TemporalYears: 100,
QgsUnitTypes.TemporalDecades: 10,
QgsUnitTypes.TemporalCenturies: 1,
QgsUnitTypes.TemporalUnknownUnit: 1.0
QgsUnitTypes.TemporalUnknownUnit: 1.0,
QgsUnitTypes.TemporalIrregularStep: 1.0,
}
}

@@ -1027,6 +1039,10 @@ def testTemporalFromUnitToUnitFactor(self):
self.assertAlmostEqual(res,
1.0,
msg='got {:.7f}, expected 1.0 when converting from {} to unknown units'.format(res, QgsUnitTypes.toString(from_unit)))
res = QgsUnitTypes.fromUnitToUnitFactor(from_unit, QgsUnitTypes.TemporalIrregularStep)
self.assertAlmostEqual(res,
1.0,
msg='got {:.7f}, expected 1.0 when converting from {} to unknown units'.format(res, QgsUnitTypes.toString(from_unit)))

def testDistanceToVolumeUnit(self):
"""Test distanceToVolumeUnit conversion"""

0 comments on commit d443e4a

Please sign in to comment.