Skip to content
Permalink
Browse files

Fix vector file writer field length

Backported from master commit 2174577
  • Loading branch information
elpaso authored and nyalldawson committed Apr 1, 2019
1 parent 979debc commit abcf953d6b4e329b63155c31e3d4cfb4f2673d5d
Showing with 41 additions and 1 deletion.
  1. +1 −1 src/core/qgsvectorfilewriter.cpp
  2. +40 −0 tests/src/core/testqgsvectorfilewriter.cpp
@@ -538,7 +538,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
}
case QVariant::String:
ogrType = OFTString;
if ( ogrWidth <= 0 || ogrWidth > 255 )
if ( ( ogrWidth <= 0 || ogrWidth > 255 ) && mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
ogrWidth = 255;
break;

@@ -82,6 +82,8 @@ class TestQgsVectorFileWriter: public QObject
void regression1141();
//! Test prepareWriteAsVectorFormat
void prepareWriteAsVectorFormat();
//! Test regression #21714 (Exported GeoPackages have wrong field definitions)
void testTextFieldLength();

private:
// a little util fn used by all tests
@@ -491,5 +493,43 @@ void TestQgsVectorFileWriter::prepareWriteAsVectorFormat()
QCOMPARE( details.providerUriParams.value( "path" ).toString(), fileName );
}


void TestQgsVectorFileWriter::testTextFieldLength()
{
QTemporaryFile tmpFile( QDir::tempPath() + "/test_qgsvectorfilewriter2_XXXXXX.gpkg" );
tmpFile.open();
QString fileName( tmpFile.fileName( ) );
QgsVectorLayer vl( "Point?field=firstfield:string(1024)", "test", "memory" );
QCOMPARE( vl.fields().at( 0 ).length(), 1024 );
QgsVectorFileWriter w( fileName,
QStringLiteral( "UTF-8" ),
vl.fields(),
QgsWkbTypes::Point,
vl.crs() );
QgsFeature f { vl.fields() };
f.setAttribute( 0, QString( 1024, 'x' ) );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "point(9 45)" ) ) );
QVERIFY( vl.startEditing() );
QVERIFY( vl.addFeature( f ) );
QgsVectorFileWriter::SaveVectorOptions options;
options.driverName = "GPKG";
options.layerName = "test";
QString errorMessage;
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormat(
&vl,
fileName,
options,
&errorMessage ) );
QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
QCOMPARE( errorMessage, fileName );
QgsVectorLayer vl2( QStringLiteral( "%1|layername=test" ).arg( fileName ), "src_test", "ogr" );
QVERIFY( vl2.isValid() );
QCOMPARE( vl2.featureCount(), 1L );
QCOMPARE( vl2.fields().at( 1 ).length(), 1024 );
QCOMPARE( vl2.getFeature( 1 ).attribute( 1 ).toString(), QString( 1024, 'x' ) );

}


QGSTEST_MAIN( TestQgsVectorFileWriter )
#include "testqgsvectorfilewriter.moc"

0 comments on commit abcf953

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