1

I'm trying to run a console script, but it gives an error: :

AttributeError:'NoneType' object has no attribute 'identify'

Yet it works well in QGIS 3.7

Here is the script:

import sys
import time
import os
import PyQt5
import numpy

# Qgis modules
from osgeo import gdal
from qgis.core import QgsRasterLayer

#from qgis.core import *
import qgis.utils
from PyQt5.QtCore import QFileInfo, QSettings
from qgis.gui import *
from qgis.utils import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis import *
from PyQt5 import *
from qgis.core import QgsRasterDataProvider

chemin = 'D:/Projet_Python_Soil/tests/'

for element in os.listdir(chemin):
    if element.endswith('.tif'):
        fileInfo = QFileInfo(element)
        baseName = fileInfo.baseName()
        rlayer = QgsRasterLayer(chemin + element, baseName)
        print (baseName)
        ident = rlayer.dataProvider().identify(QgsPoint(368371, -406601), QgsRaster.IdentifyFormatValue)
        #os.system("pause")
        print (ident.results())
        if ident.isValid():
            iden = {}
            iden = ident.results()
            print (iden[1])
  • 1
    change your path to chemin = r'D:\Projet_Python_Soil\tests' , but you have a error in the identify method too – Fran Raga May 19 at 9:59
  • I'm trying to run this console script, but it gets me wrong: AttributeError: 'NoneType' object has no 'identify' attribute yet it works well in Qgis3.7 here is the script the question: – divine May 19 at 10:51
1

Fix your code for work. You need put the correct path (Look mine),identify method need a QgsPointXYobject and make a correct source for create QgsRasterLayer object

Note : For this snippet you don't need all these imports

Code:

import sys
import time
import os
import PyQt5
import numpy

# Qgis modules
from osgeo import gdal
from qgis.core import QgsRasterLayer

#from qgis.core import *
import qgis.utils
from PyQt5.QtCore import QFileInfo, QSettings
from qgis.gui import *
from qgis.utils import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis import *
from PyQt5 import *
from qgis.core import QgsRasterDataProvider

chemin = r'C:\Users\fran\Downloads\qgis_sample_data\qgis_sample_data\raster'

for element in os.listdir(chemin):
    if element.endswith('.tif'):
        fileInfo = QFileInfo(element)
        baseName = fileInfo.baseName()
        rlayer = QgsRasterLayer(chemin+ "\\" + element, baseName)
        print (baseName)
        ident = rlayer.dataProvider().identify(QgsPointXY(48256,3536957), QgsRaster.IdentifyFormatValue)
        if ident.isValid():
            iden = {}
            iden = ident.results()
            print (iden[1])
  • j'ai changé le chemin mais jai toujours la meme erreur; l'erreur dans la methode d'identification est a quel niveau svp? – divine May 19 at 10:45
  • Asking in English please – Fran Raga May 19 at 11:25
  • I'm trying to run this console script, but it gets me wrong: AttributeError: 'NoneType' object has no 'identify' attribute yet it works well in Qgis3.7 here is the script the question: – divine May 19 at 11:51
  • What qgis version are you using? – Fran Raga May 19 at 12:09
  • la version qgis3.6 – divine May 19 at 12:16

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.