4

I would like to save a selection of a polygon vector layer to a newly created line vector layer. Manually, this can be done by: Right-Click on vector layer > Save As > Geometry Type > Line String.

How would I get the same result with PyQGIS?

Until now this works, but creates a polygon vector layer (same as input):

QgsVectorFileWriter.writeAsVectorFormat(vlayer, lines, "utf-8", None, "ESRI Shapefile", 1)

From https://qgis.org/api/classQgsVectorFileWriter.html, I don't understand how or if it is possible to overwrite the output WKB type to MultiLineString. I tried several versions of:

QgsVectorFileWriter.writeAsVectorFormat(vlayer, lines, "utf-8", None, "ESRI Shapefile", 1, Qgis.WKBMultiLineString)

Does anyone know how to do this?

1 Answer 1

3

You're close. Try:

QgsVectorFileWriter.writeAsVectorFormat( vl, "/tmp/lines.shp", "utf-8", None, "ESRI Shapefile", True, overrideGeometryType=QgsWKBTypes.MultiLineString )

As you're not providing all parameters between onlySelected and overrideGeometryType, you need to pass the geometry type as named parameter (parameter name and value).

overrideGeometryType=QgsWKBTypes.MultiLineString

From the URL you mentioned, you can see that the expected geometry type should be taken from the QgsWKBTypes.Type enum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.