3

I have sucessfully written my first QGIS processing toolbox. But have this bizzare optional tag on my parameters and i would like to remove these tags.

Parameters tagged optionnel

##Fichier_csv=file
##Dossier_de_sortie=folder
##nomodeler

import sys
import re 
import os
import csv
from PyQt4.QtCore import *
from PyQt4.QtGui import * 
from qgis.core import *
import processing

reload(sys)
sys.setdefaultencoding('utf8')
final_list = []
f_csv = Fichier_csv
op = Dossier_de_sortie
with open(f_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.join(op,table),"utf-8",None,"ESRI Shapefile",False)
        layer_path = os.path.join(op,table)
        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
2

You can achieve this by slightly editing the source code for a processing parameter file and changing the optional setting. In your QGIS installation directory, go to:

../apps/qgis/python/plugins/processing/core/

And open the parameters.py. Search for the following line:

class ParameterFile(Parameter)

In its __init__() method, change the following:

optional=True

Change from True

to

optional=False

To False

Note that depending on permissions, you may need to save the file somewhere else, move/delete the original then move the newly saved on in its place.


Restart QGIS and hopefully those tags should no longer appear when you run the script. Keep in mind that these parameters must now be filled as they are no longer optional.

Result

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.