4

A long time ago I wrote a plugin for my own use. The plugin works fine in Qgis2.18 but not in Qgis3.1 I used the script 2to3 and solved most problems. The only problem I am left with is the following.

First my code that i use.

import os.path

CSVDIR = 'd:/QGIS/Temp_map/TestCSV/2018/'
DESTDIR = 'd:/QGIS/Temp_map/TestSHP/2018/'

if not os.path.exists(DESTDIR):
os.makedirs(DESTDIR)

for root, dirs, files in os.walk(CSVDIR):
    for file in files:
        if file.endswith('.csv'):
            fullname = os.path.join(root, file).replace('/', '/')
            filename = os.path.splitext(os.path.basename(fullname))[0]
            uri = 'file:///%s?crs=%s&delimiter=%s&xField=%s&yField=%s&decimal=%s&useHeader=no&' % (fullname, 'EPSG:4326', ';', 'Field_7', 'Field_6', ',')
            layer = QgsVectorLayer(uri, 'my_layer', 'delimitedtext')
            QgsVectorFileWriter.writeAsVectorFormat(layer, DESTDIR + filename + '.shp', 'CP1250', None, 'ESRI Shapefile')

    self.iface.messageBar().clearWidgets()
    self.iface.mapCanvas().refresh()

    QMessageBox.information(iface.mainWindow(), "CSV naar Shape",
        "Uw Shapefiles zijn aangemaakt!", QMessageBox.Ok )

This code works in Qgis2.18 but give the following error in Qgis3.0

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.0\apps\Python36\lib\code.py", line 91, in runcode
  exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 24, in <module>
  TypeError: QgsVectorFileWriter.writeAsVectorFormat(): arguments did not match any overloaded call:
  overload 1: argument 4 has unexpected type 'NoneType'
  overload 2: argument 4 has unexpected type 'NoneType'
  overload 3: argument 3 has unexpected type 'str'

What can i do to make this working ??

8

writeAsVectorFormat() function expects QgsCoordinateReferenceSystemfor argument 4.

Replacing None with layer.crs() will probably solve your problem.

For more information, refer to QgsVectorFileWriter Class Reference

  • @NvBgm As this answer solved your problem, please accept it by clicking the big tick. – user2856 Apr 19 '18 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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