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

Memory layers clone: use updated source #34199

Merged
merged 1 commit into from
Feb 3, 2020
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
11 changes: 10 additions & 1 deletion src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,20 @@ QgsVectorLayer::~QgsVectorLayer()
QgsVectorLayer *QgsVectorLayer::clone() const
{
QgsVectorLayer::LayerOptions options;
// We get the data source string from the provider when
// possible because some providers may have changed it
// directly (memory provider does that).
QString dataSource;
if ( mDataProvider )
{
dataSource = mDataProvider->dataSourceUri();
options.transformContext = mDataProvider->transformContext();
}
QgsVectorLayer *layer = new QgsVectorLayer( source(), name(), mProviderKey, options );
else
{
dataSource = source();
}
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, name(), mProviderKey, options );
if ( mDataProvider && layer->dataProvider() )
{
layer->dataProvider()->handlePostCloneOperations( mDataProvider );
Expand Down
19 changes: 19 additions & 0 deletions tests/src/python/test_provider_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
__date__ = '2015-04-23'
__copyright__ = 'Copyright 2015, The QGIS Project'

from urllib.parse import parse_qs

from qgis.core import (
QgsField,
Expand Down Expand Up @@ -652,6 +653,24 @@ def testSpatialIndex(self):
vl.dataProvider().createSpatialIndex()
self.assertEqual(vl.hasSpatialIndex(), QgsFeatureSource.SpatialIndexPresent)

def testClone(self):
"""Test that a cloned layer has a single new id and
the same fields as the source layer"""

vl = QgsVectorLayer(
'Point?crs=epsg:4326',
'test', 'memory')
self.assertTrue(vl.isValid)
dp = vl.dataProvider()
self.assertTrue(dp.addAttributes([QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)]))
vl2 = vl.clone()
self.assertTrue('memory?geometry=Point&crs=EPSG:4326&field=name:(0,0)&field=age:(0,0)&field=size:(0,0)' in vl2.publicSource())
self.assertEqual(len(parse_qs(vl.publicSource())['uid']), 1)
self.assertEqual(len(parse_qs(vl2.publicSource())['uid']), 1)
self.assertNotEqual(parse_qs(vl2.publicSource())['uid'][0], parse_qs(vl.publicSource())['uid'][0])


class TestPyQgsMemoryProviderIndexed(unittest.TestCase, ProviderTestCase):

Expand Down