Skip to content

Commit ec44e60

Browse files
committedSep 24, 2017
[processing] Allow encoding crs into text definitions of points
1 parent a4ed721 commit ec44e60

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed
 

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -630,15 +630,36 @@ QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParamet
630630
if ( pointText.isEmpty() )
631631
return QgsPointXY();
632632

633-
QStringList parts = pointText.split( ',' );
634-
if ( parts.count() == 2 )
633+
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
634+
635+
QString valueAsString = parameterAsString( definition, parameters, context );
636+
QRegularExpressionMatch match = rx.match( valueAsString );
637+
if ( match.hasMatch() )
635638
{
636639
bool xOk = false;
637-
double x = parts.at( 0 ).toDouble( &xOk );
640+
double x = match.captured( 1 ).toDouble( &xOk );
638641
bool yOk = false;
639-
double y = parts.at( 1 ).toDouble( &yOk );
642+
double y = match.captured( 2 ).toDouble( &yOk );
643+
640644
if ( xOk && yOk )
641-
return QgsPointXY( x, y );
645+
{
646+
QgsPointXY pt( x, y );
647+
648+
QgsCoordinateReferenceSystem pointCrs( match.captured( 3 ) );
649+
if ( crs.isValid() && pointCrs.isValid() && crs != pointCrs )
650+
{
651+
QgsCoordinateTransform ct( pointCrs, crs );
652+
try
653+
{
654+
return ct.transform( pt );
655+
}
656+
catch ( QgsCsException & )
657+
{
658+
QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
659+
}
660+
}
661+
return pt;
662+
}
642663
}
643664

644665
return QgsPointXY();
@@ -657,6 +678,17 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const
657678
}
658679
}
659680

681+
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
682+
683+
QString valueAsString = parameterAsString( definition, parameters, context );
684+
QRegularExpressionMatch match = rx.match( valueAsString );
685+
if ( match.hasMatch() )
686+
{
687+
QgsCoordinateReferenceSystem crs( match.captured( 3 ) );
688+
if ( crs.isValid() )
689+
return crs;
690+
}
691+
660692
if ( context.project() )
661693
return context.project()->crs();
662694
else
@@ -1360,13 +1392,15 @@ bool QgsProcessingParameterPoint::checkValueIsAcceptable( const QVariant &input,
13601392
return mFlags & FlagOptional;
13611393
}
13621394

1363-
QStringList parts = input.toString().split( ',' );
1364-
if ( parts.count() == 2 )
1395+
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
1396+
1397+
QRegularExpressionMatch match = rx.match( input.toString() );
1398+
if ( match.hasMatch() )
13651399
{
13661400
bool xOk = false;
1367-
( void )parts.at( 0 ).toDouble( &xOk );
1401+
( void )match.captured( 1 ).toDouble( &xOk );
13681402
bool yOk = false;
1369-
( void )parts.at( 1 ).toDouble( &yOk );
1403+
( void )match.captured( 2 ).toDouble( &yOk );
13701404
return xOk && yOk;
13711405
}
13721406
else
@@ -1381,13 +1415,13 @@ QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value,
13811415
if ( value.canConvert< QgsPointXY >() )
13821416
{
13831417
QgsPointXY r = value.value<QgsPointXY>();
1384-
return QStringLiteral( "QgsPointXY( %1, %2 )" ).arg( qgsDoubleToString( r.x() ),
1385-
qgsDoubleToString( r.y() ) );
1418+
return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
1419+
qgsDoubleToString( r.y() ) );
13861420
}
13871421
if ( value.canConvert< QgsReferencedPointXY >() )
13881422
{
13891423
QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
1390-
return QStringLiteral( "QgsReferencedPointXY( QgsPointXY( %1, %2 ), QgsCoordinateReferenceSystem( '%3' ) )" ).arg( qgsDoubleToString( r.x() ),
1424+
return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
13911425
qgsDoubleToString( r.y() ),
13921426
r.crs().authid() );
13931427
}

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,13 @@ void TestQgsProcessing::parameterPoint()
20062006
QVERIFY( !def->checkValueIsAcceptable( true ) );
20072007
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
20082008
QVERIFY( def->checkValueIsAcceptable( "1.1,2" ) );
2009+
QVERIFY( def->checkValueIsAcceptable( " 1.1, 2 " ) );
2010+
QVERIFY( def->checkValueIsAcceptable( "-1.1,2" ) );
2011+
QVERIFY( def->checkValueIsAcceptable( "1.1,-2" ) );
2012+
QVERIFY( def->checkValueIsAcceptable( "-1.1,-2" ) );
2013+
QVERIFY( def->checkValueIsAcceptable( "1.1,2[EPSG:4326]" ) );
2014+
QVERIFY( def->checkValueIsAcceptable( "1.1,2 [EPSG:4326]" ) );
2015+
QVERIFY( def->checkValueIsAcceptable( " -1.1, -2 [EPSG:4326] " ) );
20092016
QVERIFY( !def->checkValueIsAcceptable( "1.1,a" ) );
20102017
QVERIFY( !def->checkValueIsAcceptable( "layer12312312" ) );
20112018
QVERIFY( !def->checkValueIsAcceptable( "" ) );
@@ -2026,6 +2033,18 @@ void TestQgsProcessing::parameterPoint()
20262033
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
20272034
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );
20282035

2036+
// with CRS as string
2037+
params.insert( "non_optional", QString( "1.1,2.2[EPSG:4326]" ) );
2038+
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
2039+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
2040+
QGSCOMPARENEAR( point.x(), 122451, 100 );
2041+
QGSCOMPARENEAR( point.y(), 244963, 100 );
2042+
params.insert( "non_optional", QString( "1.1,2.2 [EPSG:4326]" ) );
2043+
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
2044+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
2045+
QGSCOMPARENEAR( point.x(), 122451, 100 );
2046+
QGSCOMPARENEAR( point.y(), 244963, 100 );
2047+
20292048
// nonsense string
20302049
params.insert( "non_optional", QString( "i'm not a crs, and nothing you can do will make me one" ) );
20312050
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
@@ -2056,8 +2075,9 @@ void TestQgsProcessing::parameterPoint()
20562075
QGSCOMPARENEAR( point.y(), 244963, 100 );
20572076

20582077
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' ) )" ) );
2078+
QCOMPARE( def->valueAsPythonString( "1,2 [EPSG:4326]", context ), QStringLiteral( "'1,2 [EPSG:4326]'" ) );
2079+
QCOMPARE( def->valueAsPythonString( QgsPointXY( 11, 12 ), context ), QStringLiteral( "'11,12'" ) );
2080+
QCOMPARE( def->valueAsPythonString( QgsReferencedPointXY( QgsPointXY( 11, 12 ), QgsCoordinateReferenceSystem( "epsg:4326" ) ), context ), QStringLiteral( "'11,12 [EPSG:4326]'" ) );
20612081

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

0 commit comments

Comments
 (0)
Please sign in to comment.