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

Read source from provider before saving memory layer (fixes #8997) #2624

Merged
merged 2 commits into from
Jan 1, 2016
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
6 changes: 6 additions & 0 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "qgsrasterlayer.h"
#include "qgsrectangle.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"


QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
Expand Down Expand Up @@ -569,6 +570,11 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
urlDest.setQueryItems( urlSource.queryItems() );
src = QString::fromAscii( urlDest.toEncoded() );
}
else if ( vlayer && vlayer->providerType() == "memory" )
{
// Refetch the source from the provider, because adding fields actually changes the source for this provider.
src = vlayer->dataProvider()->dataSourceUri();
}
else
{
bool handled = false;
Expand Down
35 changes: 34 additions & 1 deletion tests/src/python/test_provider_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import shutil
import glob

from qgis.core import QGis, QgsField, QgsPoint, QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry, \
from qgis.core import QGis, QgsField, QgsPoint, QgsMapLayer, QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry, \
QgsGeometry, NULL
from PyQt4.QtCore import QSettings
from utilities import (unitTestDataPath,
Expand Down Expand Up @@ -171,6 +171,39 @@ def testFromUri(self):
myProvider = myMemoryLayer.dataProvider()
assert myProvider is not None

def testSaveFields(self):
# Create a new memory layer with no fields
myMemoryLayer = QgsVectorLayer(
('Point?crs=epsg:4326&index=yes'),
'test',
'memory')

# Add some fields to the layer
myFields = [QgsField('TestInt', QVariant.Int, 'integer', 2, 0),
QgsField('TestDbl', QVariant.Double, 'double', 8, 6),
QgsField('TestString', QVariant.String, 'string', 50, 0)]
assert myMemoryLayer.startEditing()
for f in myFields:
assert myMemoryLayer.addAttribute(f)
assert myMemoryLayer.commitChanges()
myMemoryLayer.updateFields()

# Export the layer to a layer-definition-XML
qlr = QgsMapLayer.asLayerDefinition([myMemoryLayer])
assert qlr is not None

# Import the layer from the layer-definition-XML
layers = QgsMapLayer.fromLayerDefinition(qlr)
assert layers is not None
myImportedLayer = layers[0]
assert myImportedLayer is not None

# Check for the presence of the fields
importedFields = myImportedLayer.fields()
assert importedFields is not None
for f in myFields:
assert f == importedFields.field(f.name())


if __name__ == '__main__':
unittest.main()