Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move logic to calculate combined extent of a set of map layers
to new QgsMapLayerUtils class
- Loading branch information
1 parent
700390a
commit dc50988
Showing
8 changed files
with
254 additions
and
60 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,42 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsmaplayerutils.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsMapLayerUtils | ||
{ | ||
%Docstring(signature="appended") | ||
Contains utility functions for working with map layers. | ||
|
||
.. versionadded:: 3.20 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsmaplayerutils.h" | ||
%End | ||
public: | ||
|
||
static QgsRectangle combinedExtent( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext ); | ||
%Docstring | ||
Returns the combined extent of a list of ``layers``. | ||
|
||
The ``crs`` argument specifies the desired coordinate reference system for the combined extent. | ||
%End | ||
|
||
}; | ||
|
||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsmaplayerutils.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,86 @@ | ||
/*************************************************************************** | ||
qgsmaplayerutils.cpp | ||
------------------- | ||
begin : May 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 "qgsmaplayerutils.h" | ||
#include "qgsrectangle.h" | ||
#include "qgscoordinatereferencesystem.h" | ||
#include "qgscoordinatetransformcontext.h" | ||
#include "qgsreferencedgeometry.h" | ||
#include "qgslogger.h" | ||
#include "qgsmaplayer.h" | ||
|
||
QgsRectangle QgsMapLayerUtils::combinedExtent( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext ) | ||
{ | ||
// We can't use a constructor since QgsRectangle normalizes the rectangle upon construction | ||
QgsRectangle fullExtent; | ||
fullExtent.setMinimal(); | ||
|
||
// iterate through the map layers and test each layers extent | ||
// against the current min and max values | ||
QgsDebugMsgLevel( QStringLiteral( "Layer count: %1" ).arg( layers.count() ), 5 ); | ||
for ( const QgsMapLayer *layer : layers ) | ||
{ | ||
QgsDebugMsgLevel( "Updating extent using " + layer->name(), 5 ); | ||
QgsDebugMsgLevel( "Input extent: " + layer->extent().toString(), 5 ); | ||
|
||
if ( layer->extent().isNull() ) | ||
continue; | ||
|
||
// Layer extents are stored in the coordinate system (CS) of the | ||
// layer. The extent must be projected to the canvas CS | ||
QgsCoordinateTransform ct( layer->crs(), crs, transformContext ); | ||
ct.setBallparkTransformsAreAppropriate( true ); | ||
try | ||
{ | ||
const QgsRectangle extent = ct.transformBoundingBox( layer->extent() ); | ||
|
||
QgsDebugMsgLevel( "Output extent: " + extent.toString(), 5 ); | ||
fullExtent.combineExtentWith( extent ); | ||
} | ||
catch ( QgsCsException & ) | ||
{ | ||
QgsDebugMsg( QStringLiteral( "Could not reproject layer extent" ) ); | ||
} | ||
} | ||
|
||
if ( fullExtent.width() == 0.0 || fullExtent.height() == 0.0 ) | ||
{ | ||
// If all of the features are at the one point, buffer the | ||
// rectangle a bit. If they are all at zero, do something a bit | ||
// more crude. | ||
|
||
if ( fullExtent.xMinimum() == 0.0 && fullExtent.xMaximum() == 0.0 && | ||
fullExtent.yMinimum() == 0.0 && fullExtent.yMaximum() == 0.0 ) | ||
{ | ||
fullExtent.set( -1.0, -1.0, 1.0, 1.0 ); | ||
} | ||
else | ||
{ | ||
const double padFactor = 1e-8; | ||
double widthPad = fullExtent.xMinimum() * padFactor; | ||
double heightPad = fullExtent.yMinimum() * padFactor; | ||
double xmin = fullExtent.xMinimum() - widthPad; | ||
double xmax = fullExtent.xMaximum() + widthPad; | ||
double ymin = fullExtent.yMinimum() - heightPad; | ||
double ymax = fullExtent.yMaximum() + heightPad; | ||
fullExtent.set( xmin, ymin, xmax, ymax ); | ||
} | ||
} | ||
|
||
QgsDebugMsgLevel( "Full extent: " + fullExtent.toString(), 5 ); | ||
return fullExtent; | ||
} |
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,50 @@ | ||
/*************************************************************************** | ||
qgsmaplayerutils.h | ||
------------------- | ||
begin : May 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. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSMAPLAYERUTILS_H | ||
#define QGSMAPLAYERUTILS_H | ||
|
||
#include "qgis_sip.h" | ||
#include "qgis_core.h" | ||
#include "qgis.h" | ||
|
||
class QgsMapLayer; | ||
class QgsRectangle; | ||
class QgsCoordinateReferenceSystem; | ||
class QgsCoordinateTransformContext; | ||
|
||
/** | ||
* \ingroup core | ||
* \brief Contains utility functions for working with map layers. | ||
* \since QGIS 3.20 | ||
*/ | ||
class CORE_EXPORT QgsMapLayerUtils | ||
{ | ||
|
||
public: | ||
|
||
/** | ||
* Returns the combined extent of a list of \a layers. | ||
* | ||
* The \a crs argument specifies the desired coordinate reference system for the combined extent. | ||
*/ | ||
static QgsRectangle combinedExtent( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext ); | ||
|
||
}; | ||
|
||
#endif // QGSMAPLAYERUTILS_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,69 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsMapLayerUtils. | ||
.. 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__ = '2021-05' | ||
__copyright__ = 'Copyright 2021, The QGIS Project' | ||
|
||
|
||
import qgis # NOQA | ||
|
||
from qgis.testing import unittest | ||
from qgis.core import ( | ||
QgsMapLayerUtils, | ||
QgsCoordinateReferenceSystem, | ||
QgsCoordinateTransformContext, | ||
QgsVectorLayer, | ||
QgsRasterLayer, | ||
QgsRectangle | ||
) | ||
from qgis.testing import start_app, unittest | ||
from utilities import unitTestDataPath | ||
|
||
start_app() | ||
|
||
|
||
class TestQgsMapLayerUtils(unittest.TestCase): | ||
|
||
def testCombinedExtent(self): | ||
extent = QgsMapLayerUtils.combinedExtent([], QgsCoordinateReferenceSystem(), QgsCoordinateTransformContext()) | ||
self.assertTrue(extent.isEmpty()) | ||
|
||
layer1 = QgsVectorLayer(unitTestDataPath() + '/points.shp', 'l1') | ||
self.assertTrue(layer1.isValid()) | ||
|
||
# one layer | ||
extent = QgsMapLayerUtils.combinedExtent([layer1], QgsCoordinateReferenceSystem(), QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(3), '-118.889,22.800 : -83.333,46.872') | ||
|
||
extent = QgsMapLayerUtils.combinedExtent([layer1], QgsCoordinateReferenceSystem('EPSG:4326'), | ||
QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(3), '-118.889,22.800 : -83.333,46.872') | ||
extent = QgsMapLayerUtils.combinedExtent([layer1], QgsCoordinateReferenceSystem('EPSG:3857'), | ||
QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(0), '-13234651,2607875 : -9276624,5921203') | ||
|
||
# two layers | ||
layer2 = QgsRasterLayer(unitTestDataPath() + '/landsat-f32-b1.tif', 'l2') | ||
self.assertTrue(layer2.isValid()) | ||
extent = QgsMapLayerUtils.combinedExtent([layer1, layer2], QgsCoordinateReferenceSystem('EPSG:4326'), | ||
QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(3), '-118.889,22.800 : 18.046,46.872') | ||
extent = QgsMapLayerUtils.combinedExtent([layer2, layer1], QgsCoordinateReferenceSystem('EPSG:4326'), | ||
QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(3), '-118.889,22.800 : 18.046,46.872') | ||
extent = QgsMapLayerUtils.combinedExtent([layer1, layer2], QgsCoordinateReferenceSystem('EPSG:3857'), | ||
QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(0), '-13234651,2607875 : 2008833,5921203') | ||
extent = QgsMapLayerUtils.combinedExtent([layer2, layer1], QgsCoordinateReferenceSystem('EPSG:3857'), | ||
QgsCoordinateTransformContext()) | ||
self.assertEqual(extent.toString(0), '-13234651,2607875 : 2008833,5921203') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |