1

I have the following script that i'd like to make into a QGIS toolbox.

# -*- coding: utf-8 -*-
##shpgen = name
##Fichier_CSV = file
##Chemin_de_sortie =  folder
##nomodeler
import sys
import re 
import csv
from PyQt4.QtCore import *
from PyQt4.QtGui import * 
import os.path

reload(sys)  
sys.setdefaultencoding('utf8')
final_list = []

with open(Fichier_CSV, "rU") as s:  
    reader_tables = csv.DictReader(s,delimiter=';')
    liste_tables = list(reader_tables)
    l_tables = [i['table'] for i in liste_tables]
    l_tables = set(l_tables)
    for table in l_tables:
        l1 = [line for line in liste_tables if line['table'] == table] 
        geometry = l1[1]['geometry']
        layer_mem = QgsVectorLayer(geometry+"?crs=epsg:2154",table,"memory")
        QgsVectorFileWriter.writeAsVectorFormat(layer_mem,os.path.joint(Chemin_de_sortie+"{}".format(table)),"utf-8",None,"ESRI Shapefile",False)
        layer_path = os.path.join(Chemin_de_sortie,table+".shp")
        shp = QgsVectorLayer(layer_path,table,'ogr')
        pr = shp.dataProvider()
        shp.startEditing()
        for field in l1:
            if field['type'] != 'Double':
                pr.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']))])
            else:
                pr.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']),int(field['precision']))])
        shp.updateFields()
        shp.commitChanges()
        del layer_mem,shp,pr

The script doesnt execute for some reason. Could someone hep me out?

EDITED

I removed # -*- coding: utf-8 -*- and now i have the following msg:

name 'Fichier_CSV' is not defined See log for more details

closed as off-topic by underdark Jan 20 '18 at 11:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – underdark
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Bottom right corner of QGIS, the small comic strip bubbles. Did you restart QGIS just in case? – etrimaille Dec 18 '17 at 16:21
  • 2017-12-18T17:32:04 2 Uncaught error while executing algorithm Traceback (most recent call last): File "C:/PROGRA~1/QGIS2~1.14/apps/qgis-ltr/./python/plugins\processing\core\GeoAlgorithm.py", line 203, in execute self.processAlgorithm(progress) File "C:/PROGRA~1/QGIS2~1.14/apps/qgis-ltr/./python/plugins\processing\script\ScriptAlgorithm.py", line 323, in processAlgorithm exec((script), ns) File "<string>", line 20, in <module> NameError: name 'Fichier_CSV' is not defined – user3327567 Dec 18 '17 at 16:34
  • @user3327567 - Make sure there are no spaces in your parameters. E.g. change ##Fichier_CSV = file to ##Fichier_CSV=file – Joseph Dec 19 '17 at 10:22
  • @user3327567 - I'm assuming you solved this issue you were having, in which case you should post an answer :) – Joseph Dec 20 '17 at 9:53
  • oops! i totally forgot to post the solution. It was the spaces like you said in the comment above. – user3327567 Dec 20 '17 at 9:55