Skip to content
Permalink
Browse files

Fix Point with invalid value and add tests

  • Loading branch information
lbartoletti committed Aug 24, 2020
1 parent d4239cc commit 3fcade03d43e310dea706910f90bcf37a5788ead
Showing with 30 additions and 0 deletions.
  1. +16 −0 src/core/geometry/qgspoint.cpp
  2. +14 −0 tests/src/core/testqgsgeometry.cpp
@@ -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();
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) )" ) );

0 comments on commit 3fcade0

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