Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move method for determining project temporal extent to new utils clas…
…s QgsTemporalUtils
- Loading branch information
1 parent
1272445
commit ade535b
Showing
8 changed files
with
216 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgstemporalutils.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
class QgsTemporalUtils | ||
{ | ||
%Docstring | ||
Contains utility methods for working with temporal layers and projects. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgstemporalutils.h" | ||
%End | ||
public: | ||
|
||
static QgsDateTimeRange calculateTemporalRangeForProject( QgsProject *project ); | ||
%Docstring | ||
Calculates the temporal range for a ``project``. | ||
|
||
This method considers the temporal range available from layers contained within the project and | ||
returns the maximal combined temporal extent of these layers. | ||
%End | ||
|
||
}; | ||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgstemporalutils.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/*************************************************************************** | ||
qgstemporalutils.cpp | ||
----------------------- | ||
Date : March 2020 | ||
Copyright : (C) 2020 by 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 "qgstemporalutils.h" | ||
#include "qgsproject.h" | ||
#include "qgsmaplayertemporalproperties.h" | ||
#include "qgsrasterlayer.h" | ||
|
||
QgsDateTimeRange QgsTemporalUtils::calculateTemporalRangeForProject( QgsProject *project ) | ||
{ | ||
const QMap<QString, QgsMapLayer *> &mapLayers = project->mapLayers(); | ||
QgsMapLayer *currentLayer = nullptr; | ||
|
||
QDateTime minDate; | ||
QDateTime maxDate; | ||
|
||
for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it ) | ||
{ | ||
currentLayer = it.value(); | ||
|
||
if ( !currentLayer->temporalProperties() || !currentLayer->temporalProperties()->isActive() ) | ||
continue; | ||
|
||
if ( currentLayer->type() == QgsMapLayerType::RasterLayer ) | ||
{ | ||
QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( currentLayer ); | ||
|
||
QgsDateTimeRange layerRange; | ||
switch ( rasterLayer->temporalProperties()->mode() ) | ||
{ | ||
case QgsRasterLayerTemporalProperties::ModeFixedTemporalRange: | ||
layerRange = rasterLayer->temporalProperties()->fixedTemporalRange(); | ||
break; | ||
|
||
case QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider: | ||
layerRange = rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange(); | ||
break; | ||
} | ||
|
||
if ( !minDate.isValid() || layerRange.begin() < minDate ) | ||
minDate = layerRange.begin(); | ||
if ( !maxDate.isValid() || layerRange.end() > maxDate ) | ||
maxDate = layerRange.end(); | ||
} | ||
} | ||
|
||
return QgsDateTimeRange( minDate, maxDate ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*************************************************************************** | ||
qgstemporalutils.h | ||
------------------ | ||
Date : March 2020 | ||
Copyright : (C) 2020 by 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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSTEMPORALUTILS_H | ||
#define QGSTEMPORALUTILS_H | ||
|
||
#include "qgis_core.h" | ||
#include "qgsrange.h" | ||
|
||
class QgsProject; | ||
|
||
/** | ||
* \ingroup core | ||
* \class QgsTemporalUtils | ||
* \brief Contains utility methods for working with temporal layers and projects. | ||
* | ||
* \since QGIS 3.14 | ||
*/ | ||
|
||
class CORE_EXPORT QgsTemporalUtils | ||
{ | ||
public: | ||
|
||
/** | ||
* Calculates the temporal range for a \a project. | ||
* | ||
* This method considers the temporal range available from layers contained within the project and | ||
* returns the maximal combined temporal extent of these layers. | ||
*/ | ||
static QgsDateTimeRange calculateTemporalRangeForProject( QgsProject *project ); | ||
|
||
}; | ||
|
||
|
||
#endif // QGSTEMPORALUTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsTemporalUtils. | ||
.. note:: 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. | ||
""" | ||
__author__ = 'Nyall Dawson' | ||
__date__ = '13/3/2020' | ||
__copyright__ = 'Copyright 2020, The QGIS Project' | ||
|
||
import qgis # NOQA | ||
|
||
from qgis.core import (QgsProject, | ||
QgsTemporalUtils, | ||
QgsRasterLayer, | ||
QgsDateTimeRange, | ||
QgsDateTimeRange) | ||
|
||
from qgis.PyQt.QtCore import (QDate, | ||
QTime, | ||
QDateTime, | ||
Qt) | ||
|
||
from qgis.testing import start_app, unittest | ||
from utilities import (unitTestDataPath) | ||
|
||
app = start_app() | ||
TEST_DATA_DIR = unitTestDataPath() | ||
|
||
|
||
class TestQgsTemporalUtils(unittest.TestCase): | ||
|
||
def testTemporalRangeForProject(self): | ||
p = QgsProject() | ||
r1 = QgsRasterLayer('', '', 'wms') | ||
r2 = QgsRasterLayer('', '', 'wms') | ||
r3 = QgsRasterLayer('', '', 'wms') | ||
r1.temporalProperties().setIsActive(True) | ||
r1.temporalProperties().setFixedTemporalRange(QgsDateTimeRange(QDateTime(QDate(2020, 1, 1), QTime(), Qt.UTC), | ||
QDateTime(QDate(2020, 3, 31), QTime(), Qt.UTC))) | ||
r2.temporalProperties().setIsActive(True) | ||
r2.temporalProperties().setFixedTemporalRange(QgsDateTimeRange(QDateTime(QDate(2020, 4, 1), QTime(), Qt.UTC), | ||
QDateTime(QDate(2020, 7, 31), QTime(), Qt.UTC))) | ||
r3.temporalProperties().setIsActive(True) | ||
r3.temporalProperties().setFixedTemporalRange(QgsDateTimeRange(QDateTime(QDate(2019, 1, 1), QTime(), Qt.UTC), | ||
QDateTime(QDate(2020, 2, 28), QTime(), Qt.UTC))) | ||
|
||
p.addMapLayers([r1, r2, r3]) | ||
|
||
range = QgsTemporalUtils.calculateTemporalRangeForProject(p) | ||
self.assertEqual(range.begin(), QDateTime(QDate(2019, 1, 1), QTime(), Qt.UTC)) | ||
self.assertEqual(range.end(), QDateTime(QDate(2020, 7, 31), QTime(), Qt.UTC)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |