Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport queued_ltr_backports] Correctly handle geometry value conversion for geometry fields #51859

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,24 @@ bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
return false;
}

if ( !v.convert( d->type ) )
if ( d->type == QVariant::UserType && d->typeName.compare( QLatin1String( "geometry" ), Qt::CaseInsensitive ) == 0 )
{
if ( v.userType() == QMetaType::type( "QgsReferencedGeometry" ) || v.userType() == QMetaType::type( "QgsGeometry" ) )
{
return true;
}
else if ( v.type() == QVariant::String )
{
const QgsGeometry geom = QgsGeometry::fromWkt( v.toString() );
if ( !geom.isNull() )
{
v = QVariant::fromValue( geom );
return true;
}
}
return false;
}
else if ( !v.convert( d->type ) )
{
v = QVariant( d->type );

Expand Down
19 changes: 18 additions & 1 deletion tests/src/core/testqgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#include <memory>

#include "qgssettings.h"
#include "qgsfield.h"
#include "qgsapplication.h"
#include "qgstest.h"
#include "qgsreferencedgeometry.h"

class TestQgsField: public QObject
{
Expand Down Expand Up @@ -796,6 +796,23 @@ void TestQgsField::convertCompatible()
QVERIFY( jsonField.convertCompatible( jsonValue ) );
QCOMPARE( jsonValue.type(), QVariant::String );
QCOMPARE( jsonValue, QString( "{\"a\":1,\"c\":3}" ) );

// geometry field conversion
const QgsField geometryField( QStringLiteral( "geometry" ), QVariant::UserType, QStringLiteral( "geometry" ) );
QVariant geometryValue;
QVERIFY( geometryField.convertCompatible( geometryValue ) );
QVERIFY( geometryValue.isNull() );
geometryValue = QVariant::fromValue( QgsGeometry::fromWkt( QStringLiteral( "Point( 1 2 )" ) ) );
QVERIFY( geometryField.convertCompatible( geometryValue ) );
QCOMPARE( geometryValue.userType(), QMetaType::type( "QgsGeometry" ) );

geometryValue = QVariant::fromValue( QgsReferencedGeometry( QgsGeometry::fromWkt( QStringLiteral( "Point( 1 2 )" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ) );
QVERIFY( geometryField.convertCompatible( geometryValue ) );
QCOMPARE( geometryValue.userType(), QMetaType::type( "QgsReferencedGeometry" ) );

geometryValue = QStringLiteral( "LineString( 1 2, 3 4 )" );
QVERIFY( geometryField.convertCompatible( geometryValue ) );
QCOMPARE( geometryValue.userType(), QMetaType::type( "QgsGeometry" ) );
}

void TestQgsField::dataStream()
Expand Down