Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 abcf953
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -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;

Expand Down
40 changes: 40 additions & 0 deletions tests/src/core/testqgsvectorfilewriter.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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.