Skip to content
Permalink
Browse files

[processing] Accept strings of form "(1,2") for point parameters too

Since the help text above a point parameter indicates the acceptable
format is "(x, y)", it can be confusing for users to know whether
the brackets should be entered here. So be forgiving and accept
strings with or without brackets.

Refs https://gis.stackexchange.com/questions/278765/how-do-i-enter-start-point-in-shortest-path-algorithm-in-qgis-3-0-1
  • Loading branch information
nyalldawson committed Apr 10, 2018
1 parent c625f95 commit bf0c4f9413e3ac892949792f8f631cf7ef8dbe01
Showing with 38 additions and 7 deletions.
  1. +7 −7 src/core/processing/qgsprocessingparameters.cpp
  2. +31 −0 tests/src/analysis/testqgsprocessing.cpp
@@ -535,7 +535,7 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara
if ( rectText.isEmpty() && !layer )
return QgsRectangle();

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
QRegularExpressionMatch match = rx.match( rectText );
if ( match.hasMatch() )
{
@@ -626,7 +626,7 @@ QgsGeometry QgsProcessingParameters::parameterAsExtentGeometry( const QgsProcess

if ( !rectText.isEmpty() )
{
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
QRegularExpressionMatch match = rx.match( rectText );
if ( match.hasMatch() )
{
@@ -704,7 +704,7 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( cons
}
}

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );

QString valueAsString = parameterAsString( definition, parameters, context );
QRegularExpressionMatch match = rx.match( valueAsString );
@@ -762,7 +762,7 @@ QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParamet
if ( pointText.isEmpty() )
return QgsPointXY();

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );

QString valueAsString = parameterAsString( definition, parameters, context );
QRegularExpressionMatch match = rx.match( valueAsString );
@@ -810,7 +810,7 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const
}
}

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );

QString valueAsString = parameterAsString( definition, parameters, context );
QRegularExpressionMatch match = rx.match( valueAsString );
@@ -1465,7 +1465,7 @@ bool QgsProcessingParameterExtent::checkValueIsAcceptable( const QVariant &input
return true;
}

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
QRegularExpressionMatch match = rx.match( input.toString() );
if ( match.hasMatch() )
{
@@ -1557,7 +1557,7 @@ bool QgsProcessingParameterPoint::checkValueIsAcceptable( const QVariant &input,
return mFlags & FlagOptional;
}

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );

QRegularExpressionMatch match = rx.match( input.toString() );
if ( match.hasMatch() )
@@ -2469,16 +2469,25 @@ void TestQgsProcessing::parameterPoint()
QVERIFY( !def->checkValueIsAcceptable( true ) );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "1.1,2" ) );
QVERIFY( def->checkValueIsAcceptable( "(1.1,2)" ) );
QVERIFY( def->checkValueIsAcceptable( " 1.1, 2 " ) );
QVERIFY( def->checkValueIsAcceptable( " ( 1.1, 2 ) " ) );
QVERIFY( def->checkValueIsAcceptable( "-1.1,2" ) );
QVERIFY( def->checkValueIsAcceptable( "1.1,-2" ) );
QVERIFY( def->checkValueIsAcceptable( "-1.1,-2" ) );
QVERIFY( def->checkValueIsAcceptable( "(-1.1,-2)" ) );
QVERIFY( def->checkValueIsAcceptable( "1.1,2[EPSG:4326]" ) );
QVERIFY( def->checkValueIsAcceptable( "1.1,2 [EPSG:4326]" ) );
QVERIFY( def->checkValueIsAcceptable( "(1.1,2 [EPSG:4326] )" ) );
QVERIFY( def->checkValueIsAcceptable( " -1.1, -2 [EPSG:4326] " ) );
QVERIFY( def->checkValueIsAcceptable( " ( -1.1, -2 [EPSG:4326] ) " ) );
QVERIFY( !def->checkValueIsAcceptable( "1.1,a" ) );
QVERIFY( !def->checkValueIsAcceptable( "(1.1,a)" ) );
QVERIFY( !def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( !def->checkValueIsAcceptable( "(layer12312312)" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( "()" ) );
QVERIFY( !def->checkValueIsAcceptable( " ( ) " ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsPointXY( 1, 2 ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsReferencedPointXY( QgsPointXY( 1, 2 ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) ) );
@@ -2496,6 +2505,17 @@ void TestQgsProcessing::parameterPoint()
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );

// with optional brackets
params.insert( "non_optional", QString( "(1.1,2.2)" ) );
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );

params.insert( "non_optional", QString( " ( -1.1 ,-2.2 ) " ) );
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
QGSCOMPARENEAR( point.x(), -1.1, 0.001 );
QGSCOMPARENEAR( point.y(), -2.2, 0.001 );

// with CRS as string
params.insert( "non_optional", QString( "1.1,2.2[EPSG:4326]" ) );
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
@@ -2508,12 +2528,23 @@ void TestQgsProcessing::parameterPoint()
QGSCOMPARENEAR( point.x(), 122451, 100 );
QGSCOMPARENEAR( point.y(), 244963, 100 );

params.insert( "non_optional", QString( " ( 1.1,2.2 [EPSG:4326] ) " ) );
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
QGSCOMPARENEAR( point.x(), 122451, 100 );
QGSCOMPARENEAR( point.y(), 244963, 100 );

// nonsense string
params.insert( "non_optional", QString( "i'm not a crs, and nothing you can do will make me one" ) );
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
QCOMPARE( point.x(), 0.0 );
QCOMPARE( point.y(), 0.0 );

params.insert( "non_optional", QString( " ( ) " ) );
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
QCOMPARE( point.x(), 0.0 );
QCOMPARE( point.y(), 0.0 );

// QgsPointXY
params.insert( "non_optional", QgsPointXY( 11.1, 12.2 ) );
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );

0 comments on commit bf0c4f9

Please sign in to comment.
You can’t perform that action at this time.