[QGIS-Developer] Saving selected features in a different projection

Ronald Römer rroemer at gmail.com
Tue Mar 17 05:41:10 PDT 2020


Hello,

this is my code:

---
id_ = 'my_layer_2bbd2539_97b3_417d_8a2a_97ec4f755a82'

crs = QgsCoordinateReferenceSystem()
crs.createFromString('epsg:4326')

crs2 = QgsCoordinateReferenceSystem()
crs2.createFromString('epsg:32632')

lay = QgsProject.instance().mapLayers()[id_]

lay.selectByExpression('"fclass" = \'{}\''.format(w))
new = processing.run('native:saveselectedfeatures', {'INPUT': lay,
'OUTPUT': 'memory:'})['OUTPUT']

opts = QgsVectorFileWriter.SaveVectorOptions()
opts.fileEncoding = 'UTF-8'
opts.driverName = 'ESRI Shapefile'
if os.path.exists('/home/rr/test.shp'):
    opts.actionOnExistingFile =
QgsVectorFileWriter.ActionOnExistingFile.AppendToLayerAddFields # important

ctx = QgsCoordinateTransformContext()
ctx.addCoordinateOperation(crs, crs2, '', False)

err = QgsVectorFileWriter.writeAsVectorFormatV2(new, '/home/rr/test.shp',
ctx, opts)

lay.removeSelection()
---

The result still has WGS84-coordinates. What I'm doing wrong?

I can't use native:reprojectlayer because I need the append-feature from
the QgsVectorFileWriter.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200317/e063d483/attachment.html>


More information about the QGIS-Developer mailing list