Skip to content
Permalink
Browse files

Move method for determining project temporal extent to new utils clas…

…s QgsTemporalUtils
  • Loading branch information
nyalldawson committed Mar 12, 2020
1 parent 1272445 commit ade535b4c424b73ccd4c7550698e25e5469a3cdf
@@ -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 *
************************************************************************/
@@ -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
@@ -70,6 +70,7 @@
#include "qgsbearingnumericformat.h"
#include "qgsprojectdisplaysettings.h"
#include "qgsprojecttimesettings.h"
#include "qgstemporalutils.h"

//qt includes
#include <QInputDialog>
@@ -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;
@@ -387,6 +387,7 @@ SET(QGIS_CORE_SRCS
qgstemporalnavigationobject.cpp
qgstemporalproperty.cpp
qgstemporalrangeobject.cpp
qgstemporalutils.cpp
qgstessellator.cpp
qgstextrenderer.cpp
qgstilecache.cpp
@@ -911,6 +912,7 @@ SET(QGIS_CORE_HDRS
qgstemporalnavigationobject.h
qgstemporalproperty.h
qgstemporalrangeobject.h
qgstemporalutils.h
qgstessellator.h
qgstestutils.h
qgstextrenderer.h
@@ -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 );
}
@@ -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
@@ -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)
@@ -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.
You can’t perform that action at this time.