Skip to content
Permalink
Browse files

Better method to ensure memory layers always have unique sources

  • Loading branch information
nyalldawson committed May 17, 2017
1 parent 49a7158 commit afc9788881ab68c4a6c040974fa05f26e337165b
@@ -65,8 +65,6 @@ QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name,
{
parts << QStringLiteral( "field=%1:%2" ).arg( field.name(), memoryLayerFieldType( field.type() ) );
}
// required so that source differs between memory layers
parts << QStringLiteral( "uid=%1" ).arg( QUuid::createUuid().toString() );

QString uri = geomType + '?' + parts.join( '&' );

@@ -1465,6 +1465,11 @@ bool QgsVectorLayer::setDataProvider( QString const &provider )
if ( mDataSource.right( 10 ) == QLatin1String( "|layerid=0" ) )
mDataSource.chop( 10 );
}
else if ( provider == QStringLiteral( "memory" ) )
{
// required so that source differs between memory layers
mDataSource = mDataSource + QStringLiteral( "&uid=%1" ).arg( QUuid::createUuid().toString() );
}

connect( mDataProvider, &QgsVectorDataProvider::dataChanged, this, &QgsVectorLayer::dataChanged );
connect( mDataProvider, &QgsVectorDataProvider::dataChanged, this, &QgsVectorLayer::removeSelection );
@@ -338,6 +338,15 @@ def testRenameAttributes(self):
self.assertEqual(fet.fields()[1].name(), 'mapinfo_is_the_stone_age')
self.assertEqual(fet.fields()[2].name(), 'super_size')

def testUniqueSource(self):
"""
Similar memory layers should have unique source - some code checks layer source to identify
matching layers
"""
layer = QgsVectorLayer("Point", "test", "memory")
layer2 = QgsVectorLayer("Point", "test2", "memory")
self.assertNotEqual(layer.source(), layer2.source())

def testCreateMemoryLayer(self):
"""
Test QgsMemoryProviderUtils.createMemoryLayer()

0 comments on commit afc9788

Please sign in to comment.