[Qgis-developer] Changes to a newly created memory layer are affecting the original layer from which it was copied
Nyall Dawson
nyall.dawson at gmail.com
Thu Mar 17 13:05:18 PDT 2016
On 16 Mar 2016 20:56, "Tom Chadwin" <tom.chadwin at nnpa.org.uk> wrote:
>
> def writeTmpLayer(layer, popup):
> usedFields = getUsedFields(layer)
> if popup != ALL_ATTRIBUTES:
> uri = TYPE_MAP[layer.wkbType()]
> crs = layer.crs()
> if crs.isValid():
> uri += '?crs=' + crs.authid()
> if popup != NO_POPUP:
> usedFields.append(popup)
> for field in usedFields:
> fieldType = layer.pendingFields().field(field).type()
> fieldType = "double" if (fieldType == QVariant.Double or
> fieldType == QVariant.Int) else (
> "string")
> uri += '&field=' + unicode(field) + ":" + fieldType
> newlayer = QgsVectorLayer(uri, layer.name(), 'memory')
> writer = newlayer.dataProvider()
> outFeat = QgsFeature()
> for feature in layer.getFeatures():
> outFeat.setGeometry(feature.geometry())
> attrs = [feature[f] for f in usedFields]
> if attrs:
> outFeat.setAttributes(attrs)
> writer.addFeatures([outFeat])
> layer = newlayer
> return layer
>
> The snippet which calls the function above and is meant to add the fields
to
> the memory layer is:
>
> if (layer.type() == layer.VectorLayer)
> cleanLayer = writeTmpLayer(layer, popup)
> if is25d(layer, canvas):
> provider = cleanLayer.dataProvider()
> provider.addAttributes([QgsField("height", QVariant.Double),
> QgsField("wallColor", QVariant.String),
> QgsField("roofColor",
> QVariant.String)])
> cleanLayer.updateFields()
>
If popup == ALL_ATTRIBUTES then your writeTmpLayer function is returning
the original layer.
Nyall
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160318/3ebf7817/attachment.html>
More information about the Qgis-developer
mailing list