2

I wrote a QGIS plugin and at the moment I'm going through the error messages. I'm stuck on the following:

enter image description here

Now I show you the top of my code where I import everything and the function containing the gdal algorithm:

from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QFileDialog
from qgis.core import *
from osgeo import gdal
from processing.core.Processing import Processing
import glob, os.path
from .resources import *
from .Clip_XYZ_dialog import ClipXYZDialog


def clipRasters(self):
    Processing.initialize()
    inputDir = self.getInputPath()
    outputDir = self.getOutputPath()
    for lyr in glob.glob(os.path.join(inputDir, "*xyz")):
        processing.run("gdalogr:cliprasterbymasklayer", {lyr, self.getVectorLayer(), 'none', True, False, 0,
                       outputDir + lyr})

Interesting is that "from osgeo import gdal" is actually grayed out in my code and processing has a red underline that marks some sort of error. It says "unresolved reference 'processing'" but I don't know what that means. Can someone explain me the error and what should be done?

6
  • 1
    Try adding import processing after the line from osgeo import gdal.
    – Joseph
    Oct 25 '19 at 10:57
  • ok I did that, but now I get the error "algorithm gdalogr:cliprasterbymasklayer not found" which is strange since I imported processing and gdal... Also "from osgeo import gdal" is still grayed out.
    – DGIS
    Oct 25 '19 at 11:01
  • 2
    The algorithm is called gdal:cliprasterbymasklayer (remove the ogr). Also, you shouldn't need from osgeo import gdal since you're calling the processing module which would contain the algorithm instead, unless you're using gdal directly later in your plugin.
    – Joseph
    Oct 25 '19 at 11:17
  • 1
    You should also move Processing.initialize() outside of the function as it only needs to be executed once.
    – Joseph
    Oct 25 '19 at 11:36
  • 3
    Possible duplicate of Error in processing.run("gdalogr:cliprastermymasklayer") Oct 25 '19 at 14:03
3

To summarise the comments:

  1. Include import processing as part of importing modules;
  2. The algorithm is named gdal:cliprasterbymasklayer and not gdalogr:cliprasterbymasklayer;
  3. Move Processing.initialize() outside the function as it is only required once.

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.