Skip to content

Commit 9b11228

Browse files
committed
Add method to recover extent parameter CRS
1 parent 4fd2a1a commit 9b11228

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
lines changed

python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,14 @@ class QgsProcessingAlgorithm
688688
:rtype: QgsGeometry
689689
%End
690690

691+
QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
692+
%Docstring
693+
Returns the coordinate reference system associated with an extent parameter value.
694+
695+
.. seealso:: parameterAsExtent()
696+
:rtype: QgsCoordinateReferenceSystem
697+
%End
698+
691699
QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
692700
%Docstring
693701
Evaluates the parameter with matching ``name`` to a point.

python/core/processing/qgsprocessingparameters.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ class QgsProcessingParameters
543543
reprojected so that it is in the specified ``crs``. In this case the extent of the reproject rectangle will be returned.
544544

545545
.. seealso:: parameterAsExtentGeometry()
546+
.. seealso:: parameterAsExtentCrs()
546547
:rtype: QgsRectangle
547548
%End
548549

@@ -557,9 +558,18 @@ class QgsProcessingParameters
557558
just the extent of the reprojected rectangle).
558559

559560
.. seealso:: parameterAsExtent()
561+
.. seealso:: parameterAsExtentCrs()
560562
:rtype: QgsGeometry
561563
%End
562564

565+
static QgsCoordinateReferenceSystem parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
566+
%Docstring
567+
Returns the coordinate reference system associated with an extent parameter value.
568+
569+
.. seealso:: parameterAsExtent()
570+
:rtype: QgsCoordinateReferenceSystem
571+
%End
572+
563573
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
564574
%Docstring
565575
Evaluates the parameter with matching ``definition`` to a point.

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@ QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsCrs( const QVari
550550
return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
551551
}
552552

553+
QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context )
554+
{
555+
return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
556+
}
557+
553558
QgsRectangle QgsProcessingAlgorithm::parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
554559
{
555560
return QgsProcessingParameters::parameterAsExtent( parameterDefinition( name ), parameters, context, crs );

src/core/processing/qgsprocessingalgorithm.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,13 @@ class CORE_EXPORT QgsProcessingAlgorithm
670670
QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
671671
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
672672

673+
/**
674+
* Returns the coordinate reference system associated with an extent parameter value.
675+
*
676+
* \see parameterAsExtent()
677+
*/
678+
QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
679+
673680
/**
674681
* Evaluates the parameter with matching \a name to a point.
675682
*/

src/core/processing/qgsprocessingparameters.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,25 @@ QgsGeometry QgsProcessingParameters::parameterAsExtentGeometry( const QgsProcess
569569
return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
570570
}
571571

572+
QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
573+
{
574+
QVariant val = parameters.value( definition->name() );
575+
576+
if ( val.canConvert< QgsReferencedRectangle >() )
577+
{
578+
QgsReferencedRectangle rr = val.value<QgsReferencedRectangle>();
579+
if ( rr.crs().isValid() )
580+
{
581+
return rr.crs();
582+
}
583+
}
584+
585+
if ( context.project() )
586+
return context.project()->crs();
587+
else
588+
return QgsCoordinateReferenceSystem();
589+
}
590+
572591
QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
573592
{
574593
if ( !definition )

src/core/processing/qgsprocessingparameters.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ class CORE_EXPORT QgsProcessingParameters
570570
* reprojected so that it is in the specified \a crs. In this case the extent of the reproject rectangle will be returned.
571571
*
572572
* \see parameterAsExtentGeometry()
573+
* \see parameterAsExtentCrs()
573574
*/
574575
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
575576
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
@@ -583,10 +584,18 @@ class CORE_EXPORT QgsProcessingParameters
583584
* just the extent of the reprojected rectangle).
584585
*
585586
* \see parameterAsExtent()
587+
* \see parameterAsExtentCrs()
586588
*/
587589
static QgsGeometry parameterAsExtentGeometry( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
588590
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
589591

592+
/**
593+
* Returns the coordinate reference system associated with an extent parameter value.
594+
*
595+
* \see parameterAsExtent()
596+
*/
597+
static QgsCoordinateReferenceSystem parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
598+
590599
/**
591600
* Evaluates the parameter with matching \a definition to a point.
592601
*/

tests/src/core/testqgsprocessing.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,13 +1915,17 @@ void TestQgsProcessing::parameterExtent()
19151915
QgsGeometry gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
19161916
QCOMPARE( gExt.exportToWkt( 1 ), QStringLiteral( "Polygon ((11.1 12.2, 13.3 12.2, 13.3 14.4, 11.1 14.4, 11.1 12.2))" ) );
19171917

1918+
p.setCrs( QgsCoordinateReferenceSystem( "EPSG:3785" ) );
1919+
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:3785" ) );
1920+
19181921
// QgsReferencedRectangle
19191922
params.insert( "non_optional", QgsReferencedRectangle( QgsRectangle( 1.1, 2.2, 3.3, 4.4 ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) );
19201923
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context );
19211924
QGSCOMPARENEAR( ext.xMinimum(), 1.1, 0.001 );
19221925
QGSCOMPARENEAR( ext.xMaximum(), 3.3, 0.001 );
19231926
QGSCOMPARENEAR( ext.yMinimum(), 2.2, 0.001 );
19241927
QGSCOMPARENEAR( ext.yMaximum(), 4.4, 0.001 );
1928+
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
19251929

19261930
// with target CRS
19271931
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );

0 commit comments

Comments
 (0)