0

I have this code I use in the QGIS Python console. I used it a few days ago and it worked, but I tried using it again and I got this error:

Traceback (most recent call last): 
File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, 
in runcode exec(code, self.locals) File "<input>", line 1, 
in <module> File "<string>", line 33, 
in <module> File "C:/PROGRA~1/QGIS3~1.10/apps/qgis-ltr/./python/plugins\processing\tools\general.py", line 136, 
in runAndLoadResults for param in alg.parameterDefinitions(): 
AttributeError: 'NoneType' object has no attribute 'parameterDefinitions'

I checked all my definitions, and I see they are correct. How can I possibly resolve this?

# Import necessary modules
from qgis.core import QgsApplication, QgsRasterLayer
import processing

# Initialize QGIS application 
QgsApplication.setPrefixPath("/path/to/qgis", True)
QgsApplication.initQgis()

# Set path to input raster layer
file_dir = 'D:/WDF_03012023'
mask_dir = 'D:/WDF_03012023/masks'
Date = '030123' #to always edit depending on Date involved
Ring_num = '1aRuta' #always change this to what is after the R in the file name

# Construct the Ring-specific names and paths
Ring_name = f'030123_R{Ring_num}' #always change date here to the date working on
mask_name = f'mask_{Ring_name[7:]}_{Date}'
Ring = file_dir + '/' + Ring_name + '.tif'
mask = mask_dir + '/' + mask_name + '.tif'

# Load the input raster layer
raster_layer = QgsRasterLayer(mask)

# Polygonize the raster layer
polygonized_layer = processing.runAndLoadResults(
    "gdal:polygonize",
    {
        'INPUT': raster_layer,
        'BAND': 1,
        'FIELD': 'DN',
        'EIGHT_CONNECTEDNESS': False,
        'EXTRA': '',
        'OUTPUT': 'TEMPORARY_OUTPUT'  # Let QGIS use the specified temporary directory
    }
)

# Fix geometries of the polygonized layer
fixed_geometries = processing.runAndLoadResults("native:fixgeometries", {'INPUT': polygonized_layer['OUTPUT'], 'OUTPUT': 'TEMPORARY_OUTPUT'})

# Clip the input raster based on the mask
clipped_ring = processing.runAndLoadResults(
    "gdal:cliprasterbymasklayer",
    {
        'INPUT': Ring,
        'MASK': fixed_geometries['OUTPUT'],
        'SOURCE_CRS': None,
        'TARGET_CRS': None,
        'NODATA': None,
        'ALPHA_BAND': False,
        'CROP_TO_CUTLINE': True,
        'KEEP_RESOLUTION': True,
        'SET_RESOLUTION': False,
        'X_RESOLUTION': None,
        'Y_RESOLUTION': None,
        'MULTITHREADING': False,
        'OPTIONS': '',
        'DATA_TYPE': 0,
        'EXTRA': '',
        'OUTPUT': 'TEMPORARY_OUTPUT'  
    }
)
10
  • Is there more to the error message? There's no mention of parameterDefinitions in your code so it must be a problem from something called by it, which should be revealed by the full traceback error...
    – Spacedman
    Commented Sep 26, 2023 at 9:40
  • 2
    " I use in the QGIS Python console" - isn't this a standalone PyQGIS script that should be run standalone from the shell console, not the QGIS Python console? Maybe it works both ways but it looks like a standalone script to me...
    – Spacedman
    Commented Sep 26, 2023 at 9:42
  • @Spacedman thanks for your response, I currently have issues using it as a standalone (I would resolve that soon), hence I use it in the QGIS console. Not sure if I can put the traceback error here, but here it is: Commented Sep 26, 2023 at 10:46
  • Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 33, in <module> File "C:/PROGRA~1/QGIS3~1.10/apps/qgis-ltr/./python/plugins\processing\tools\general.py", line 136, in runAndLoadResults for param in alg.parameterDefinitions(): AttributeError: 'NoneType' object has no attribute 'parameterDefinitions' Here is why I looked at the parameterDefinitions. Commented Sep 26, 2023 at 10:48
  • Please Edit the Question in response to requests for clarification. This is particularly true for error text.
    – Vince
    Commented Sep 26, 2023 at 11:32

0

Your Answer

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