Skip to content

Commit

Permalink
Fix Point with invalid value and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti committed Aug 24, 2020
1 parent d4239cc commit 3fcade0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core/geometry/qgspoint.cpp
Expand Up @@ -177,6 +177,22 @@ bool QgsPoint::fromWkt( const QString &wkt )

QRegularExpression rx( QStringLiteral( "\\s" ) );
QStringList coordinates = parts.second.split( rx, QString::SkipEmptyParts );

// So far the parser hasn't looked at the coordinates. We'll avoid having anything but numbers and return NULL instead of 0 as a coordinate.
// Without this check, "POINT (a, b)" or "POINT (( 4, 3 ))" returned "POINT (0 ,0)"
// And some strange conversion...
// .. python:
// p = QgsPoint()
// p.fromWkt("POINT (-3.12, -4.2")
// False
// p.fromWkt( "POINT (-5.1234, -1.4321)" )
// True
// p.asWkt()
// 'Point (0 -1.43209999999999993)'
QRegularExpression rxIsNumber( QStringLiteral( "^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$" ) );
if ( coordinates.filter( rxIsNumber ).size() != coordinates.size() )
return false;

if ( coordinates.size() < 2 )
{
clear();
Expand Down
14 changes: 14 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -18106,6 +18106,20 @@ void TestQgsGeometry::wktParser()
QVERIFY( ! QgsPoint().fromWkt( "POINT(0, 1) )" ) );
QVERIFY( ! QgsPoint().fromWkt( "POINT ((0, 1)" ) );
QVERIFY( ! QgsPoint().fromWkt( "POINT (0, 1) )" ) );
// not a number
QVERIFY( ! QgsPoint().fromWkt( "POINT ( (5, 1) )" ) );
QVERIFY( ! QgsPoint().fromWkt( "POINT (a, b)" ) );
// valid
QgsPoint p;
QVERIFY( p.fromWkt( "POINT (5 1)" ) );
QCOMPARE( p.asWkt(), QStringLiteral( "Point (5 1)" ) );
QVERIFY( p.fromWkt( "POINT (5.1234 1.4321)" ) );
QCOMPARE( p.asWkt( 4 ), QStringLiteral( "Point (5.1234 1.4321)" ) );
QVERIFY( p.fromWkt( "POINT (-5.1234 -1.4321)" ) );
QCOMPARE( p.asWkt( 4 ), QStringLiteral( "Point (-5.1234 -1.4321)" ) );
QVERIFY( p.fromWkt( "POINT (-12e4 -1.4e-1)" ) );
QCOMPARE( p.asWkt( 2 ), QStringLiteral( "Point (-120000 -0.14)" ) );

QVERIFY( ! QgsLineString().fromWkt( "LineString(0, 1) )" ) );
QVERIFY( ! QgsLineString().fromWkt( "LineString(0 1, 1 2) )" ) );
QVERIFY( ! QgsLineString().fromWkt( "LineString (0, 1) )" ) );
Expand Down

0 comments on commit 3fcade0

Please sign in to comment.