[Qgis-user] How to define generic VectorFileWriter
Nyall Dawson
nyall.dawson at gmail.com
Fri Dec 21 02:12:44 PST 2018
On Fri, 21 Dec 2018 at 19:03, Reginald <Reginald.Carlier at ingelmunster.be> wrote:
>
> Hi,
>
> I'm trying to implement a way to copy the geometry of selected parcels to an
> editable layer in a qgs project.
> I'm using QGIS 3.4.2.
>
> My code looks like this:
>
> from qgis.PyQt.QtCore import *
> from qgis.core import WKB
This should be
from qgis.core import QgsWkbTypes
See https://qgis.org/pyqgis/master/core/QgsWkbTypes.html
Nyall
>
> perceellayer=None
> editlayer=None
> layers = [layer for layer in QgsProject.instance().mapLayers().values()]
> for lyr in layers:
> if lyr.name() == "Adpf1836007":
> perceellayer = lyr
> if lyr.type() == QgsMapLayer.VectorLayer:
> cap = lyr.dataProvider().capabilities()
> if cap & QgsVectorDataProvider.AddFeatures:
> editlayer = lyr
> percelen = []
> features = perceellayer.selectedFeatures()
> geom = None
> for f in features:
> if geom == None:
> geom = f.geometry()
> else:
> geom = geom.combine(f.geometry())
>
> velden = editlayer.fields()
> fpad = editlayer.dataProvider().dataSourceUri()
> feat = QgsFeature(velden)
> feat.setGeometry(geom)
> fout = QgsVectorFileWriter.writeAsVectorFormat(editlayer, fpad, "31370",
> WKB.Polygon, "ESRI Shapefile")
> if fout == QgsVectorFileWriter.NoError:
> print("succes")
> print("done")
>
> In this example I try to write to a shapefile, but it fails complaining it
> can not import WKB. I would like to write to any kind of Vectorfile
> depending of the vectorfile that was set writeable. Any ideas on how to do
> this? The cookbook is kind of short concerning writing features.
>
> Regards,
>
>
>
> --
> Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
More information about the Qgis-user
mailing list