Skip to content
Permalink
Browse files

Adds a few magic to make the parser more tolerant of typos and allows…

… the creation of empty geometry using empty parentheses like 'LineString ( )'.
  • Loading branch information
lbartoletti committed Aug 24, 2020
1 parent 7212fec commit d349fc4b2b3ea07b4463c0ccb4995f1e9cbb8366
@@ -314,12 +314,13 @@ bool QgsCircularString::fromWkt( const QString &wkt )
return false;
mWkbType = parts.first;

if ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 )
parts.second = parts.second.remove( '(' ).remove( ')' );
QString secondWithoutParentheses = parts.second;
secondWithoutParentheses = secondWithoutParentheses.simplified().remove( ' ' );
if ( ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
secondWithoutParentheses.isEmpty() )
return true;

if ( parts.second.compare( QLatin1String( "NULL" ), Qt::CaseInsensitive ) == 0 )
return false;

QgsPointSequence points = QgsGeometryUtils::pointsFromWKT( parts.second, is3D(), isMeasure() );
if ( points.isEmpty() )
return false;
@@ -179,12 +179,12 @@ bool QgsCompoundCurve::fromWkt( const QString &wkt )
return false;
mWkbType = parts.first;

if ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 )
QString secondWithoutParentheses = parts.second;
secondWithoutParentheses = secondWithoutParentheses.remove( '(' ).remove( ')' ).simplified().remove( ' ' );
if ( ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
secondWithoutParentheses.isEmpty() )
return true;

if ( parts.second.compare( QLatin1String( "NULL" ), Qt::CaseInsensitive ) == 0 )
return false;

QString defaultChildWkbType = QStringLiteral( "LineString%1%2" ).arg( is3D() ? QStringLiteral( "Z" ) : QString(), isMeasure() ? QStringLiteral( "M" ) : QString() );

const QStringList blocks = QgsGeometryUtils::wktGetChildBlocks( parts.second, defaultChildWkbType );
@@ -215,12 +215,12 @@ bool QgsCurvePolygon::fromWkt( const QString &wkt )

mWkbType = parts.first;

if ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 )
QString secondWithoutParentheses = parts.second;
secondWithoutParentheses = secondWithoutParentheses.remove( '(' ).remove( ')' ).simplified().remove( ' ' );
if ( ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
secondWithoutParentheses.isEmpty() )
return true;

if ( parts.second.compare( QLatin1String( "NULL" ), Qt::CaseInsensitive ) == 0 )
return false;

QString defaultChildWkbType = QStringLiteral( "LineString%1%2" ).arg( is3D() ? QStringLiteral( "Z" ) : QString(), isMeasure() ? QStringLiteral( "M" ) : QString() );

const QStringList blocks = QgsGeometryUtils::wktGetChildBlocks( parts.second, defaultChildWkbType );
@@ -698,12 +698,12 @@ bool QgsGeometryCollection::fromCollectionWkt( const QString &wkt, const QVector
}
mWkbType = parts.first;

if ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 )
QString secondWithoutParentheses = parts.second;
secondWithoutParentheses = secondWithoutParentheses.remove( '(' ).remove( ')' ).simplified().remove( ' ' );
if ( ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
secondWithoutParentheses.isEmpty() )
return true;

if ( parts.second.compare( QLatin1String( "NULL" ), Qt::CaseInsensitive ) == 0 )
return false;

QString defChildWkbType = QStringLiteral( "%1%2%3 " ).arg( defaultChildWkbType, is3D() ? QStringLiteral( "Z" ) : QString(), isMeasure() ? QStringLiteral( "M" ) : QString() );

const QStringList blocks = QgsGeometryUtils::wktGetChildBlocks( parts.second, defChildWkbType );
@@ -1100,6 +1100,7 @@ QgsPointSequence QgsGeometryUtils::pointsFromWKT( const QString &wktCoordinateLi
{
int dim = 2 + is3D + isMeasure;
QgsPointSequence points;

const QStringList coordList = wktCoordinateList.split( ',', QString::SkipEmptyParts );

//first scan through for extra unexpected dimensions
@@ -1306,9 +1307,7 @@ QPair<QgsWkbTypes::Type, QString> QgsGeometryUtils::wktReadBlock( const QString
{
QString wktParsed = wkt;
QString contents;
if ( wktParsed.count( '(' ) != wktParsed.count( ')' ) ) // if unbalanced parenthesis will returns false
contents = QStringLiteral( "NULL" );
else if ( wkt.contains( QString( "EMPTY" ), Qt::CaseInsensitive ) )
if ( wkt.contains( QString( "EMPTY" ), Qt::CaseInsensitive ) )
{
QRegularExpression wktRegEx( QStringLiteral( "^\\s*(\\w+)\\s+(\\w+)\\s*$" ) );
wktRegEx.setPatternOptions( QRegularExpression::DotMatchesEverythingOption );
@@ -1321,6 +1320,14 @@ QPair<QgsWkbTypes::Type, QString> QgsGeometryUtils::wktReadBlock( const QString
}
else
{
const int openedParenthesisCount = wktParsed.count( '(' );
const int closedParenthesisCount = wktParsed.count( ')' );
// closes missing parentheses
for ( int i = 0 ; i < openedParenthesisCount - closedParenthesisCount; ++i )
wktParsed.push_back( ')' );
// removes extra parentheses
wktParsed.truncate( wktParsed.size() - ( closedParenthesisCount - openedParenthesisCount ) );

QRegularExpression cooRegEx( QStringLiteral( "^[^\\(]*\\((.*)\\)[^\\)]*$" ) );
cooRegEx.setPatternOptions( QRegularExpression::DotMatchesEverythingOption );
QRegularExpressionMatch match = cooRegEx.match( wktParsed );
@@ -434,13 +434,16 @@ bool QgsLineString::fromWkt( const QString &wkt )
return false;
mWkbType = parts.first;

if ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 )
QString secondWithoutParentheses = parts.second;
secondWithoutParentheses = secondWithoutParentheses.remove( '(' ).remove( ')' ).simplified().remove( ' ' );
parts.second = parts.second.remove( '(' ).remove( ')' );
if ( ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
secondWithoutParentheses.isEmpty() )
return true;

if ( parts.second.compare( QLatin1String( "NULL" ), Qt::CaseInsensitive ) == 0 )
return false;

QgsPointSequence points = QgsGeometryUtils::pointsFromWKT( parts.second, is3D(), isMeasure() );
// There is a non number in the coordinates sequence
// LineString ( A b, 1 2)
if ( points.isEmpty() )
return false;

@@ -169,12 +169,13 @@ bool QgsPoint::fromWkt( const QString &wkt )
return false;
mWkbType = parts.first;

if ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 )
QString secondWithoutParentheses = parts.second;
secondWithoutParentheses = secondWithoutParentheses.remove( '(' ).remove( ')' ).simplified().remove( ' ' );
parts.second = parts.second.remove( '(' ).remove( ')' );
if ( ( parts.second.compare( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
secondWithoutParentheses.isEmpty() )
return true;

if ( parts.second.compare( QLatin1String( "NULL" ), Qt::CaseInsensitive ) == 0 )
return false;

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

0 comments on commit d349fc4

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