Skip to content

Commit

Permalink
Add a time unit for "irregular steps"
Browse files Browse the repository at this point in the history
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 d443e4a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsunittypes.sip.in
Expand Up @@ -120,6 +120,7 @@ Helper functions for various unit types.
TemporalYears,
TemporalDecades,
TemporalCenturies,
TemporalIrregularStep,
TemporalUnknownUnit
};

Expand Down
21 changes: 21 additions & 0 deletions src/core/qgsunittypes.cpp
Expand Up @@ -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>" );
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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" );
}
Expand Down Expand Up @@ -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" );
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -1442,6 +1452,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 3155760000.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1471,6 +1482,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 315576000000.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1500,6 +1512,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 52596000.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1529,6 +1542,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 876600.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1558,6 +1572,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 1 / 36525.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1587,6 +1602,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 7 / 36525.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1616,6 +1632,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 30 / 36525.0;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1645,6 +1662,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 0.01;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -1674,6 +1692,7 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalUnit fromUnit,
case TemporalCenturies:
return 0.1;
case TemporalUnknownUnit:
case TemporalIrregularStep:
return 1.0;
}
break;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsunittypes.h
Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/core/vector/qgsvectorlayertemporalproperties.cpp
Expand Up @@ -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 ),
Expand Down
36 changes: 26 additions & 10 deletions tests/src/python/test_qgsunittypes.py
Expand Up @@ -287,6 +287,7 @@ def testEncodeDecodeTemporalUnits(self):
QgsUnitTypes.TemporalYears,
QgsUnitTypes.TemporalDecades,
QgsUnitTypes.TemporalCenturies,
QgsUnitTypes.TemporalIrregularStep,
QgsUnitTypes.TemporalUnknownUnit]

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

for u in units:
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
}

Expand All @@ -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"""
Expand Down

0 comments on commit d443e4a

Please sign in to comment.