Skip to content
Permalink
Browse files

Add method to merge a list of possibly non-contigous date/datetime ra…

…nges
  • Loading branch information
nyalldawson committed Mar 25, 2021
1 parent 57e5737 commit b7c1c3af4c9282e1ed2da48f4bac63cdaa27cbb6
@@ -375,6 +375,7 @@ If the range is empty and ``other`` is not, the range is changed and set to ``ot
.. versionadded:: 3.12
%End


bool operator==( const QgsTemporalRange<T> &other ) const;

bool operator!=( const QgsTemporalRange<T> &other ) const;
@@ -599,6 +599,53 @@ class QgsTemporalRange
return changed;
}

#ifndef SIP_RUN

/**
* Merges a list of temporal ranges.
*
* Any overlapping ranges will be converted to a single range which covers the entire
* range of the input ranges.
*
* The returned value will be a list of non-contiguous ranges which completely encompass
* the input \a ranges, sorted in ascending order.
*
* \note Not available in Python bindings
*
* \since QGIS 3.20
*/
static QList< QgsTemporalRange<T> > mergeRanges( const QList< QgsTemporalRange<T> > &ranges )
{
QList< QgsTemporalRange<T > > res;

QList< QgsTemporalRange<T> > queue = ranges;
while ( !queue.empty() )
{
QgsTemporalRange<T> range = queue.back();
queue.pop_back();

bool extended = false;
for ( int i = 0; i < res.count(); ++i )
{
if ( res[i].overlaps( range ) )
{
QgsTemporalRange< T > other = res.takeAt( i );
other.extend( range );
queue.push_back( other );
extended = true;
break;
}
}

if ( !extended )
res.push_back( range );
}

std::sort( res.begin(), res.end(), []( const QgsTemporalRange< T > &a, const QgsTemporalRange< T > &b ) -> bool { return a.begin() < b.begin(); } );
return res;
}
#endif

bool operator==( const QgsTemporalRange<T> &other ) const
{
return mLower == other.mLower &&
@@ -171,6 +171,7 @@ set(TESTS
testqgsproperty.cpp
testqgsprovidermetadata.cpp
testqgis.cpp
testqgsrange.cpp
testqgsrasterfilewriter.cpp
testqgsrasterfill.cpp
testqgsrastermarker.cpp
@@ -0,0 +1,92 @@
/***************************************************************************
testqgsrange.cpp
-------------------
Date : March 2021
Copyright : (C) 2021 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include <QObject>

#include "qgsrange.h"

class TestQgsRange: public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void testMergeRangesDate();
void testMergeRangesDateTime();

private:

};

void TestQgsRange::initTestCase()
{
}

void TestQgsRange::cleanupTestCase()
{
}

void TestQgsRange::init()
{

}

void TestQgsRange::cleanup()
{

}

void TestQgsRange::testMergeRangesDate()
{
QList< QgsDateRange > ranges { QgsDateRange( QDate( 2020, 1, 10 ), QDate( 2020, 1, 15 ) ),
QgsDateRange( QDate( 2020, 1, 20 ), QDate( 2020, 1, 25 ) ),
QgsDateRange( QDate( 2020, 1, 9 ), QDate( 2020, 1, 11 ) ),
QgsDateRange( QDate( 2020, 1, 19 ), QDate( 2020, 1, 27 ) ),
QgsDateRange( QDate( 2020, 1, 1 ), QDate( 2020, 1, 3 ) ) };

QList< QgsDateRange > res = QgsDateRange::mergeRanges( ranges );
QCOMPARE( res.size(), 3 );
QCOMPARE( res.at( 0 ).begin(), QDate( 2020, 1, 1 ) );
QCOMPARE( res.at( 0 ).end(), QDate( 2020, 1, 3 ) );
QCOMPARE( res.at( 1 ).begin(), QDate( 2020, 1, 9 ) );
QCOMPARE( res.at( 1 ).end(), QDate( 2020, 1, 15 ) );
QCOMPARE( res.at( 2 ).begin(), QDate( 2020, 1, 19 ) );
QCOMPARE( res.at( 2 ).end(), QDate( 2020, 1, 27 ) );
}

void TestQgsRange::testMergeRangesDateTime()
{
QList< QgsDateTimeRange > ranges { QgsDateTimeRange( QDateTime( QDate( 2020, 1, 10 ), QTime( 0, 0, 0 ) ), QDateTime( QDate( 2020, 1, 15 ), QTime( 0, 0, 0 ) ) ),
QgsDateTimeRange( QDateTime( QDate( 2020, 1, 20 ), QTime( 0, 0, 0 ) ), QDateTime( QDate( 2020, 1, 25 ), QTime( 0, 0, 0 ) ) ),
QgsDateTimeRange( QDateTime( QDate( 2020, 1, 9 ), QTime( 0, 0, 0 ) ), QDateTime( QDate( 2020, 1, 11 ), QTime( 0, 0, 0 ) ) ),
QgsDateTimeRange( QDateTime( QDate( 2020, 1, 19 ), QTime( 0, 0, 0 ) ), QDateTime( QDate( 2020, 1, 27 ), QTime( 0, 0, 0 ) ) ),
QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ), QTime( 0, 0, 0 ) ), QDateTime( QDate( 2020, 1, 3 ), QTime( 0, 0, 0 ) ) ) };

QList< QgsDateTimeRange > res = QgsDateTimeRange::mergeRanges( ranges );
QCOMPARE( res.size(), 3 );
QCOMPARE( res.at( 0 ).begin(), QDateTime( QDate( 2020, 1, 1 ), QTime( 0, 0, 0 ) ) );
QCOMPARE( res.at( 0 ).end(), QDateTime( QDate( 2020, 1, 3 ), QTime( 0, 0, 0 ) ) );
QCOMPARE( res.at( 1 ).begin(), QDateTime( QDate( 2020, 1, 9 ), QTime( 0, 0, 0 ) ) );
QCOMPARE( res.at( 1 ).end(), QDateTime( QDate( 2020, 1, 15 ), QTime( 0, 0, 0 ) ) );
QCOMPARE( res.at( 2 ).begin(), QDateTime( QDate( 2020, 1, 19 ), QTime( 0, 0, 0 ) ) );
QCOMPARE( res.at( 2 ).end(), QDateTime( QDate( 2020, 1, 27 ), QTime( 0, 0, 0 ) ) );
}


QGSTEST_MAIN( TestQgsRange )
#include "testqgsrange.moc"

0 comments on commit b7c1c3a

Please sign in to comment.