1

I've written around 30 scripts (both QGIS and non-QGIS scripts) in Python 2.7 and using it through QGIS 2.18 "Processing -> Scripts" tools so that within our office we can share and use the scripts.

enter image description here

We have moved to QGIS 3.4 and no issues with converting almost all of the 30 scripts from Python 2.7 to Python 3.7 and as of now, I am able to run those scripts through "QGIS Python Console". But i don't know how to convert those scripts to "Python processing tool scripts" which can be loaded in QGIS 3.4 and execute the scripts as I used to do in QGIS 2.18.

As an example, would like to brief about a script i am using - "Delete_lines_from_CSV"

1) Input will be a CSV file with 1st row - will have the list of CSVs for which the lines are to be deleted; IInd row - will have the corresponding new csv file name.

enter image description here

Below is the code I am using and it runs in the QGIS 3.4 Python Console without any issues.

import os, csv, re
from os import path, linesep
input_csvpath = r"D:\Test\pyqgis3\csvlist2.csv"
nlines = 2
nfirstlines = []

with open(input_csvpath, "r") as csvfile:
    reader = csv.reader(csvfile)

    for row in reader:
        input_csv= row[0]
        out_csv = row[1]


        with open(input_csv) as incsv, open(out_csv, "w") as outcsv:
            for x in range(int(nlines)):
                nfirstlines.append(next(incsv))
            for line in incsv:
                outcsv.write(line)

With the help of the links and comments mentioned in this thread, Writing a python processing script with Qgis 3.0 - I've modified the template code to the very basics

from PyQt5.QtCore import (QCoreApplication, 
                      QVariant)
from qgis.core import (QgsProcessing,
                       QgsFields,
                       QgsField,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterField,
                       QgsProcessingParameterFile,
                       QgsProcessingParameterString,
                       QgsProcessingParameterBoolean,
                       QgsProcessingParameterNumber,
                       )

import os, csv, re
from os import path, linesep

class deletelines(QgsProcessingAlgorithm):
    Input = 'input_iist'
    No_of_rows = 'no_of_rows'

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def deletelines(self):
        return deletelines()

    def name(self):
        return 'Deletelines'.lower()

    def displayName(self):
        return self.tr('Delete_lines_from_CSV_TXT')

    def group(self):
        return self.tr('My_Scripts')

    def groupId(self):
        return 'myscripts1'

    def shortHelpString(self):
        return self.tr('Delete Lines from csv')

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterFile(name=self.Input,description="Input CSV List",extension="csv(*.csv)"))
        self.addParameter(QgsProcessingParameterNumber(self.No_of_rows,self.tr('Field length'), minValue=0, defaultValue=2))

and when I run this, I am getting

enter image description here

Don't know how to proceed further. In QGIS 2.18, I'd just paste the remaining code - as shown in the image below, highlighted in red and will get the outputs.

enter image description here

If I can learn how to implement this non-QGIS script within QGIS 3.4, can manage with the other non-QGIS, QGIS scripts.

1

find here an example with an standalone codeblock. The processAlgorithm will return nothing or in this case only ''...

# -*- coding: utf-8 -*-

"""
testscript from stefan giese, 28.03.19
"""

from PyQt5.QtCore import QCoreApplication
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsProcessing,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFile)
import processing
import sys, os, string, csv, re


class csvAlgorithm(QgsProcessingAlgorithm):
    """
    takes a csv und do something with it
    """
    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return csvAlgorithm()

    def name(self):
        return 'csv'

    def displayName(self):
        return self.tr('csv Data')

    def group(self):
        return self.tr('test')

    def groupId(self):
        return 'test'

    def shortHelpString(self):
        return self.tr("do something with csv Data")

    def initAlgorithm(self, config=None):

        # We add the input File Parameter.
        self.addParameter(
            QgsProcessingParameterFile(
                self.INPUT,
                self.tr('csv File'),
                0,
                'csv'
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        inputFile = self.parameterAsFile(parameters, self.INPUT, context)

        nlines = 2
        nfirstlines = []

        with open(inputFile, "r") as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
               input_csv= row[0]
               out_csv = row[1]
               with open(input_csv) as incsv, open(out_csv, "w") as outcsv:
                  for x in range(int(nlines)):
                     nfirstlines.append(next(incsv))
                  for line in incsv:
                     outcsv.write(line) 

        #return something empty....
        return {self.OUTPUT:''} 
  • Thanks a lot mate. Worked perfectly. I've modified slightly by making "No_of_rows\Nlines" value user defined instead of 2. – joseph_k Mar 29 at 1:19

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.