Skip to content

Commit

Permalink
Move method for determining project temporal extent to new utils clas…
Browse files Browse the repository at this point in the history
…s QgsTemporalUtils
  • Loading branch information
nyalldawson committed Mar 13, 2020
1 parent 1272445 commit ade535b
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 35 deletions.
43 changes: 43 additions & 0 deletions python/core/auto_generated/qgstemporalutils.sip.in
@@ -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 *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -199,6 +199,7 @@
%Include auto_generated/qgstemporalnavigationobject.sip
%Include auto_generated/qgstemporalproperty.sip
%Include auto_generated/qgstemporalrangeobject.sip
%Include auto_generated/qgstemporalutils.sip
%Include auto_generated/qgstessellator.sip
%Include auto_generated/qgstestutils.sip
%Include auto_generated/qgstextrenderer.sip
Expand Down
39 changes: 4 additions & 35 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -70,6 +70,7 @@
#include "qgsbearingnumericformat.h"
#include "qgsprojectdisplaysettings.h"
#include "qgsprojecttimesettings.h"
#include "qgstemporalutils.h"

//qt includes
#include <QInputDialog>
Expand Down Expand Up @@ -2510,41 +2511,9 @@ void QgsProjectProperties::mButtonAddColor_clicked()

void QgsProjectProperties::calculateFromLayersButton_clicked()
{
const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->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();
}
}
const QgsDateTimeRange range = QgsTemporalUtils::calculateTemporalRangeForProject( QgsProject::instance() );
const QDateTime minDate = range.begin();
const QDateTime maxDate = range.end();

if ( !minDate.isValid() || !maxDate.isValid() )
return;
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -387,6 +387,7 @@ SET(QGIS_CORE_SRCS
qgstemporalnavigationobject.cpp
qgstemporalproperty.cpp
qgstemporalrangeobject.cpp
qgstemporalutils.cpp
qgstessellator.cpp
qgstextrenderer.cpp
qgstilecache.cpp
Expand Down Expand Up @@ -911,6 +912,7 @@ SET(QGIS_CORE_HDRS
qgstemporalnavigationobject.h
qgstemporalproperty.h
qgstemporalrangeobject.h
qgstemporalutils.h
qgstessellator.h
qgstestutils.h
qgstextrenderer.h
Expand Down
60 changes: 60 additions & 0 deletions src/core/qgstemporalutils.cpp
@@ -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 );
}
47 changes: 47 additions & 0 deletions src/core/qgstemporalutils.h
@@ -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
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -240,6 +240,7 @@ ADD_PYTHON_TEST(PyQgsSvgSourceLineEdit test_qgssvgsourcelineedit.py)
ADD_PYTHON_TEST(PyQgsSymbol test_qgssymbol.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerUtils test_qgssymbollayerutils.py)
ADD_PYTHON_TEST(PyQgsTaskManager test_qgstaskmanager.py)
ADD_PYTHON_TEST(PyQgsTemporalUtils test_qgstemporalutils.py)
ADD_PYTHON_TEST(PyQgsTextFormatWidget test_qgstextformatwidget.py)
ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)
Expand Down
58 changes: 58 additions & 0 deletions tests/src/python/test_qgstemporalutils.py
@@ -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()

0 comments on commit ade535b

Please sign in to comment.