Wednesday, 4 July 2018

export - Error when writing SHAPE to WKT with pyqgis and QGIS 2.14



It seems the answer linked here doesn't work in latest version of QGIS 2.14


layer = QgsVectorLayer("/home/reyman/Projets/cours/coursPython/pyQgis/shape/passages/passages.shp", "passages", "ogr")
if not layer.isValid():
print "Layer failed to load!"

QgsVectorFileWriter.writeAsVectorFormat(layer, "wkt.csv", "utf-8", None, "CSV", layerOptions='GEOMETRY=AS_WKT')

return the warning :


Warning 6: layer creation option 'G' is not formatted with the key=value format
Warning 6: layer creation option 'E' is not formatted with the key=value format

Warning 6: layer creation option 'O' is not formatted with the key=value format
Warning 6: layer creation option 'M' is not formatted with the key=value format
Warning 6: layer creation option 'E' is not formatted with the key=value format
Warning 6: layer creation option 'T' is not formatted with the key=value format
Warning 6: layer creation option 'R' is not formatted with the key=value format
Warning 6: layer creation option 'Y' is not formatted with the key=value format
Warning 6: '' is an unexpected value for layer creation option of type string-select.
Warning 6: layer creation option 'A' is not formatted with the key=value format
Warning 6: layer creation option 'S' is not formatted with the key=value format
Warning 6: layer creation option '_' is not formatted with the key=value format

Warning 6: layer creation option 'W' is not formatted with the key=value format
Warning 6: layer creation option 'K' is not formatted with the key=value format
Warning 6: layer creation option 'T' is not formatted with the key=value format

Answer



You can find the answer in the QGIS API documentation: http://www.qgis.org/api/2.14/classQgsVectorFileWriter.html#ac25d02c9ad8bfe6113ba7a06a981a447


What layerOptions parameter is expecting is a list of strings, not a single string. So, your layerOptions value should be: ['GEOMETRY=AS_WKT']


QgsVectorFileWriter.writeAsVectorFormat( layer, 
"wkt.csv",
"utf-8",
None,

"CSV",
layerOptions=['GEOMETRY=AS_WKT'] )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...