Skip to content

Commit e1eef7e

Browse files
committedSep 24, 2017
Allow use of QgsPointXY/QgsReferencedPointXY for point parameter values
1 parent 8902d5f commit e1eef7e

File tree

8 files changed

+180
-9
lines changed

8 files changed

+180
-9
lines changed
 

‎python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,26 @@ class QgsProcessingAlgorithm
696696
:rtype: QgsCoordinateReferenceSystem
697697
%End
698698

699-
QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
699+
QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
700+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
700701
%Docstring
701702
Evaluates the parameter with matching ``name`` to a point.
703+
704+
If ``crs`` is set then the point will be automatically
705+
reprojected so that it is in the specified ``crs``.
706+
707+
.. seealso:: parameterAsPointCrs()
702708
:rtype: QgsPointXY
703709
%End
704710

711+
QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
712+
%Docstring
713+
Returns the coordinate reference system associated with an point parameter value.
714+
715+
.. seealso:: parameterAsPoint()
716+
:rtype: QgsCoordinateReferenceSystem
717+
%End
718+
705719
QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
706720
%Docstring
707721
Evaluates the parameter with matching ``name`` to a file/folder name.

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,25 @@ class QgsProcessingParameters
570570
:rtype: QgsCoordinateReferenceSystem
571571
%End
572572

573-
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
573+
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
574+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
574575
%Docstring
575576
Evaluates the parameter with matching ``definition`` to a point.
577+
578+
If ``crs`` is set then the point will be automatically reprojected so that it is in the specified ``crs``.
579+
580+
.. seealso:: parameterAsPointCrs()
576581
:rtype: QgsPointXY
577582
%End
578583

584+
static QgsCoordinateReferenceSystem parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
585+
%Docstring
586+
Returns the coordinate reference system associated with an point parameter value.
587+
588+
.. seealso:: parameterAsPoint()
589+
:rtype: QgsCoordinateReferenceSystem
590+
%End
591+
579592
static QString parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
580593
%Docstring
581594
Evaluates the parameter with matching ``definition`` to a file/folder name.
@@ -817,6 +830,8 @@ class QgsProcessingParameterPoint : QgsProcessingParameterDefinition
817830
virtual QString type() const;
818831
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
819832

833+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
834+
820835

821836
static QgsProcessingParameterPoint *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
822837
%Docstring

‎src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,14 @@ QgsGeometry QgsProcessingAlgorithm::parameterAsExtentGeometry( const QVariantMap
565565
return QgsProcessingParameters::parameterAsExtentGeometry( parameterDefinition( name ), parameters, context, crs );
566566
}
567567

568-
QgsPointXY QgsProcessingAlgorithm::parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
568+
QgsPointXY QgsProcessingAlgorithm::parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
569569
{
570-
return QgsProcessingParameters::parameterAsPoint( parameterDefinition( name ), parameters, context );
570+
return QgsProcessingParameters::parameterAsPoint( parameterDefinition( name ), parameters, context, crs );
571+
}
572+
573+
QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context )
574+
{
575+
return QgsProcessingParameters::parameterAsPointCrs( parameterDefinition( name ), parameters, context );
571576
}
572577

573578
QString QgsProcessingAlgorithm::parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const

‎src/core/processing/qgsprocessingalgorithm.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,21 @@ class CORE_EXPORT QgsProcessingAlgorithm
680680

681681
/**
682682
* Evaluates the parameter with matching \a name to a point.
683+
*
684+
* If \a crs is set then the point will be automatically
685+
* reprojected so that it is in the specified \a crs.
686+
*
687+
* \see parameterAsPointCrs()
688+
*/
689+
QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
690+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
691+
692+
/**
693+
* Returns the coordinate reference system associated with an point parameter value.
694+
*
695+
* \see parameterAsPoint()
683696
*/
684-
QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
697+
QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
685698

686699
/**
687700
* Evaluates the parameter with matching \a name to a file/folder name.

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,14 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara
503503
if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
504504
{
505505
QgsCoordinateTransform ct( rr.crs(), crs );
506-
return ct.transformBoundingBox( rr );
506+
try
507+
{
508+
return ct.transformBoundingBox( rr );
509+
}
510+
catch ( QgsCsException & )
511+
{
512+
QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
513+
}
507514
}
508515
return rr;
509516
}
@@ -588,11 +595,34 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( cons
588595
return QgsCoordinateReferenceSystem();
589596
}
590597

591-
QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
598+
QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs )
592599
{
593600
if ( !definition )
594601
return QgsPointXY();
595602

603+
QVariant val = parameters.value( definition->name() );
604+
if ( val.canConvert< QgsPointXY >() )
605+
{
606+
return val.value<QgsPointXY>();
607+
}
608+
if ( val.canConvert< QgsReferencedPointXY >() )
609+
{
610+
QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
611+
if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
612+
{
613+
QgsCoordinateTransform ct( rp.crs(), crs );
614+
try
615+
{
616+
return ct.transform( rp );
617+
}
618+
catch ( QgsCsException & )
619+
{
620+
QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
621+
}
622+
}
623+
return rp;
624+
}
625+
596626
QString pointText = parameterAsString( definition, parameters, context );
597627
if ( pointText.isEmpty() )
598628
pointText = definition->defaultValue().toString();
@@ -614,6 +644,25 @@ QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParamet
614644
return QgsPointXY();
615645
}
616646

647+
QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
648+
{
649+
QVariant val = parameters.value( definition->name() );
650+
651+
if ( val.canConvert< QgsReferencedPointXY >() )
652+
{
653+
QgsReferencedPointXY rr = val.value<QgsReferencedPointXY>();
654+
if ( rr.crs().isValid() )
655+
{
656+
return rr.crs();
657+
}
658+
}
659+
660+
if ( context.project() )
661+
return context.project()->crs();
662+
else
663+
return QgsCoordinateReferenceSystem();
664+
}
665+
617666
QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
618667
{
619668
if ( !definition )
@@ -1296,6 +1345,15 @@ bool QgsProcessingParameterPoint::checkValueIsAcceptable( const QVariant &input,
12961345
return true;
12971346
}
12981347

1348+
if ( input.canConvert< QgsPointXY >() )
1349+
{
1350+
return true;
1351+
}
1352+
if ( input.canConvert< QgsReferencedPointXY >() )
1353+
{
1354+
return true;
1355+
}
1356+
12991357
if ( input.type() == QVariant::String )
13001358
{
13011359
if ( input.toString().isEmpty() )
@@ -1315,6 +1373,28 @@ bool QgsProcessingParameterPoint::checkValueIsAcceptable( const QVariant &input,
13151373
return false;
13161374
}
13171375

1376+
QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1377+
{
1378+
if ( value.canConvert<QgsProperty>() )
1379+
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1380+
1381+
if ( value.canConvert< QgsPointXY >() )
1382+
{
1383+
QgsPointXY r = value.value<QgsPointXY>();
1384+
return QStringLiteral( "QgsPointXY( %1, %2 )" ).arg( qgsDoubleToString( r.x() ),
1385+
qgsDoubleToString( r.y() ) );
1386+
}
1387+
if ( value.canConvert< QgsReferencedPointXY >() )
1388+
{
1389+
QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
1390+
return QStringLiteral( "QgsReferencedPointXY( QgsPointXY( %1, %2 ), QgsCoordinateReferenceSystem( '%3' ) )" ).arg( qgsDoubleToString( r.x() ),
1391+
qgsDoubleToString( r.y() ),
1392+
r.crs().authid() );
1393+
}
1394+
1395+
return QgsProcessingParameterDefinition::valueAsPythonString( value, context );
1396+
}
1397+
13181398
QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
13191399
{
13201400
return new QgsProcessingParameterPoint( name, description, definition, isOptional );

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,20 @@ class CORE_EXPORT QgsProcessingParameters
598598

599599
/**
600600
* Evaluates the parameter with matching \a definition to a point.
601+
*
602+
* If \a crs is set then the point will be automatically reprojected so that it is in the specified \a crs.
603+
*
604+
* \see parameterAsPointCrs()
601605
*/
602-
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
606+
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
607+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
608+
609+
/**
610+
* Returns the coordinate reference system associated with an point parameter value.
611+
*
612+
* \see parameterAsPoint()
613+
*/
614+
static QgsCoordinateReferenceSystem parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
603615

604616
/**
605617
* Evaluates the parameter with matching \a definition to a file/folder name.
@@ -804,6 +816,7 @@ class CORE_EXPORT QgsProcessingParameterPoint : public QgsProcessingParameterDef
804816
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
805817
QString type() const override { return typeName(); }
806818
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
819+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
807820

808821
/**
809822
* Creates a new parameter using the definition from a script code.

‎src/core/qgsapplication.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ void QgsApplication::init( QString profileFolder )
151151
qRegisterMetaType<QgsFeatureIds>( "QgsFeatureIds" );
152152
qRegisterMetaType<QgsMessageLog::MessageLevel>( "QgsMessageLog::MessageLevel" );
153153
qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedRectangle" );
154-
qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedPoint" );
155154

156155
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
157156
// QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) );

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,8 @@ void TestQgsProcessing::parameterPoint()
20102010
QVERIFY( !def->checkValueIsAcceptable( "layer12312312" ) );
20112011
QVERIFY( !def->checkValueIsAcceptable( "" ) );
20122012
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
2013+
QVERIFY( def->checkValueIsAcceptable( QgsPointXY( 1, 2 ) ) );
2014+
QVERIFY( def->checkValueIsAcceptable( QgsReferencedPointXY( QgsPointXY( 1, 2 ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) ) );
20132015

20142016
// string representing a point
20152017
QVariantMap params;
@@ -2019,13 +2021,43 @@ void TestQgsProcessing::parameterPoint()
20192021
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
20202022
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );
20212023

2024+
// with target CRS - should make no difference, because source CRS is unknown
2025+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
2026+
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
2027+
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );
2028+
20222029
// nonsense string
20232030
params.insert( "non_optional", QString( "i'm not a crs, and nothing you can do will make me one" ) );
20242031
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
20252032
QCOMPARE( point.x(), 0.0 );
20262033
QCOMPARE( point.y(), 0.0 );
20272034

2035+
// QgsPointXY
2036+
params.insert( "non_optional", QgsPointXY( 11.1, 12.2 ) );
2037+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
2038+
QGSCOMPARENEAR( point.x(), 11.1, 0.001 );
2039+
QGSCOMPARENEAR( point.y(), 12.2, 0.001 );
2040+
2041+
// with target CRS - should make no difference, because source CRS is unknown
2042+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
2043+
QGSCOMPARENEAR( point.x(), 11.1, 0.001 );
2044+
QGSCOMPARENEAR( point.y(), 12.2, 0.001 );
2045+
2046+
// QgsReferencedPointXY
2047+
params.insert( "non_optional", QgsReferencedPointXY( QgsPointXY( 1.1, 2.2 ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) );
2048+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
2049+
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
2050+
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );
2051+
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
2052+
2053+
// with target CRS
2054+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
2055+
QGSCOMPARENEAR( point.x(), 122451, 100 );
2056+
QGSCOMPARENEAR( point.y(), 244963, 100 );
2057+
20282058
QCOMPARE( def->valueAsPythonString( "1,2", context ), QStringLiteral( "'1,2'" ) );
2059+
QCOMPARE( def->valueAsPythonString( QgsPointXY( 11, 12 ), context ), QStringLiteral( "QgsPointXY( 11, 12 )" ) );
2060+
QCOMPARE( def->valueAsPythonString( QgsReferencedPointXY( QgsPointXY( 11, 12 ), QgsCoordinateReferenceSystem( "epsg:4326" ) ), context ), QStringLiteral( "QgsReferencedPointXY( QgsPointXY( 11, 12 ), QgsCoordinateReferenceSystem( 'EPSG:4326' ) )" ) );
20292061

20302062
QString code = def->asScriptCode();
20312063
QCOMPARE( code, QStringLiteral( "##non_optional=point 1,2" ) );

0 commit comments

Comments
 (0)
Please sign in to comment.