Skip to content

Commit

Permalink
[processing] preserve field length and precision for temporary outputs (
Browse files Browse the repository at this point in the history
#5654)



* add test case to cover length and precision through uri
  • Loading branch information
nirvn committed Nov 16, 2017
1 parent bb6fda6 commit 05d38b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/core/providers/memory/qgsmemoryproviderutils.cpp
Expand Up @@ -63,7 +63,16 @@ QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name,
}
for ( const auto &field : fields )
{
parts << QStringLiteral( "field=%1:%2" ).arg( field.name(), memoryLayerFieldType( field.type() ) );
QString lengthPrecision;
if ( field.length() > 0 && field.precision() > 0 )
{
lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
}
else if ( field.length() > 0 )
{
lengthPrecision = QStringLiteral( "(%1)" ).arg( field.length() );
}
parts << QStringLiteral( "field=%1:%2%3" ).arg( field.name(), memoryLayerFieldType( field.type() ), lengthPrecision );
}

QString uri = geomType + '?' + parts.join( '&' );
Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_provider_memory.py
Expand Up @@ -263,6 +263,16 @@ def testFromUri(self):
myProvider = myMemoryLayer.dataProvider()
assert myProvider is not None

def testLengthPrecisionFromUri(self):
"""Test we can assign length and precision from a uri"""
myMemoryLayer = QgsVectorLayer(
('Point?crs=epsg:4326&field=size:double(12,9)&index=yes'),
'test',
'memory')

self.assertEqual(myMemoryLayer.fields().field('size').length(), 12)
self.assertEqual(myMemoryLayer.fields().field('size').precision(), 9)

def testSaveFields(self):
# Create a new memory layer with no fields
myMemoryLayer = QgsVectorLayer(
Expand Down

0 comments on commit 05d38b3

Please sign in to comment.