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?

Joseph
76.7k8 gold badges173 silver badges286 bronze badges
asked Oct 25, 2019 at 10:43
6
  • 1
    Try adding import processing after the line from osgeo import gdal. Commented Oct 25, 2019 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. Commented Oct 25, 2019 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. Commented Oct 25, 2019 at 11:17
  • 1
    You should also move Processing.initialize() outside of the function as it only needs to be executed once. Commented Oct 25, 2019 at 11:36
  • 3
    Possible duplicate of Error in processing.run("gdalogr:cliprastermymasklayer") Commented Oct 25, 2019 at 14:03

1 Answer 1

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.
answered Oct 25, 2019 at 13:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.