Skip to content

Commit

Permalink
Add method to recover extent parameter CRS
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 15, 2017
1 parent 4fd2a1a commit 9b11228
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -688,6 +688,14 @@ class QgsProcessingAlgorithm
:rtype: QgsGeometry
%End

QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
%Docstring
Returns the coordinate reference system associated with an extent parameter value.

.. seealso:: parameterAsExtent()
:rtype: QgsCoordinateReferenceSystem
%End

QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a point.
Expand Down
10 changes: 10 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -543,6 +543,7 @@ class QgsProcessingParameters
reprojected so that it is in the specified ``crs``. In this case the extent of the reproject rectangle will be returned.

.. seealso:: parameterAsExtentGeometry()
.. seealso:: parameterAsExtentCrs()
:rtype: QgsRectangle
%End

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

.. seealso:: parameterAsExtent()
.. seealso:: parameterAsExtentCrs()
:rtype: QgsGeometry
%End

static QgsCoordinateReferenceSystem parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Returns the coordinate reference system associated with an extent parameter value.

.. seealso:: parameterAsExtent()
:rtype: QgsCoordinateReferenceSystem
%End

static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` to a point.
Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -550,6 +550,11 @@ QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsCrs( const QVari
return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
}

QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context )
{
return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
}

QgsRectangle QgsProcessingAlgorithm::parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
{
return QgsProcessingParameters::parameterAsExtent( parameterDefinition( name ), parameters, context, crs );
Expand Down
7 changes: 7 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -670,6 +670,13 @@ class CORE_EXPORT QgsProcessingAlgorithm
QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );

/**
* Returns the coordinate reference system associated with an extent parameter value.
*
* \see parameterAsExtent()
*/
QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );

/**
* Evaluates the parameter with matching \a name to a point.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -569,6 +569,25 @@ QgsGeometry QgsProcessingParameters::parameterAsExtentGeometry( const QgsProcess
return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
}

QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
{
QVariant val = parameters.value( definition->name() );

if ( val.canConvert< QgsReferencedRectangle >() )
{
QgsReferencedRectangle rr = val.value<QgsReferencedRectangle>();
if ( rr.crs().isValid() )
{
return rr.crs();
}
}

if ( context.project() )
return context.project()->crs();
else
return QgsCoordinateReferenceSystem();
}

QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
{
if ( !definition )
Expand Down
9 changes: 9 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -570,6 +570,7 @@ class CORE_EXPORT QgsProcessingParameters
* reprojected so that it is in the specified \a crs. In this case the extent of the reproject rectangle will be returned.
*
* \see parameterAsExtentGeometry()
* \see parameterAsExtentCrs()
*/
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
Expand All @@ -583,10 +584,18 @@ class CORE_EXPORT QgsProcessingParameters
* just the extent of the reprojected rectangle).
*
* \see parameterAsExtent()
* \see parameterAsExtentCrs()
*/
static QgsGeometry parameterAsExtentGeometry( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );

/**
* Returns the coordinate reference system associated with an extent parameter value.
*
* \see parameterAsExtent()
*/
static QgsCoordinateReferenceSystem parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );

/**
* Evaluates the parameter with matching \a definition to a point.
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -1915,13 +1915,17 @@ void TestQgsProcessing::parameterExtent()
QgsGeometry gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
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))" ) );

p.setCrs( QgsCoordinateReferenceSystem( "EPSG:3785" ) );
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:3785" ) );

// QgsReferencedRectangle
params.insert( "non_optional", QgsReferencedRectangle( QgsRectangle( 1.1, 2.2, 3.3, 4.4 ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) );
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context );
QGSCOMPARENEAR( ext.xMinimum(), 1.1, 0.001 );
QGSCOMPARENEAR( ext.xMaximum(), 3.3, 0.001 );
QGSCOMPARENEAR( ext.yMinimum(), 2.2, 0.001 );
QGSCOMPARENEAR( ext.yMaximum(), 4.4, 0.001 );
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );

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

0 comments on commit 9b11228

Please sign in to comment.