2

I started working with QGIS, because I need to build a forest fire risk map for a university project. I tried to build a little standalone application to calculate the NDVI. I'm following the python cookbook and made a mix of the standalone app code of chapter 1 and the code to calculate de NDVI. Unfortunately, I get an error I can't find a solution to. I guess it’s because the file of the raster calculator is not being generated properly. So, when I try to access the layer afterwards I get an error because the file wasn't created. I have tried a lot of different approaches but I haven't found a solution so far.

I use a Windows PC and Qgis 2.18 Las Palmas.

from qgis.core import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.analysis import *
import sys

app = QgsApplication([], True)
app.setPrefixPath("D:/OSGeo4W64/apps/qgis",True)
app.initQgis()
rasterName="Banda"
raster = QgsRasterLayer("C:/Users/Name/workspace/MyMap/Datos/DatosSatelites/Landsat8_B4.TIF", rasterName)

if not(raster.isValid()):
    QMessageBox.information(None, "DEBUG:", "Error al cargar") 

ir = QgsRasterCalculatorEntry()

r = QgsRasterCalculatorEntry()

ir.raster = raster

r.raster = raster

ir.bandNumber = 2

r.bandNumber = 1

ir.ref = rasterName + "@2"

r.ref = rasterName + "@1"

references = (ir.ref, r.ref, ir.ref, r.ref)

exp = "1.0 * (%s - %s) / 1.0 + (%s + %s)" % references

output = 'C:/Users/Name/workspace/MyMap/Datos/Output/ndvi1.tif'

e = raster.extent()

w = raster.width()

h = raster.height()

entries = [ir,r]

**ndvi = QgsRasterCalculator(exp, output, "GTiff", e, w, h, entries)**

ndvi.processCalculation()

**lyr = QgsRasterLayer(output, "NDVI")** #in this line I get the message

algorithm = QgsContrastEnhancement.StretchToMinimumMaximum

limits = QgsRaster.ContrastEnhancementMinMax

lyr.setContrastEnhancement(algorithm, limits)

s = QgsRasterShader()

c = QgsColorRampShader()

c.setColorRampType(QgsColorRampShader.INTERPOLATED)

i = []

qri = QgsColorRampShader.ColorRampItem

i.append(qri(0, QColor(0,0,0,0), 'NODATA'))

i.append(qri(214, QColor(120,69,25,255), 'Lowest Biomass'))

i.append(qri(236, QColor(255,178,74,255), 'Lower Biomass'))

i.append(qri(258, QColor(255,237,166,255), 'Low Biomass'))

i.append(qri(280, QColor(173,232,94,255), 'Moderate Biomass'))

i.append(qri(303, QColor(135,181,64,255), 'High Biomass'))

i.append(qri(325, QColor(3,156,0,255), 'Higher Biomass'))

i.append(qri(400, QColor(1,100,0,255), 'Highest Biomass'))


c.setColorRampItemList(i)

s.setRasterShaderFunction(c)

ps = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1,s)

lyr.setRenderer(ps)


QgsMapLayerRegistry.instance().addMapLayer(lyr)


exitcode = app.exec_()

QgsApplication.exitQgis()

sys.exit(exitcode)
  • Try using double slashes in the folder names i.e. "C://Users//Name//workspace//......" – Liam G Jun 13 '17 at 5:04
  • Can you post the error message? Or is it just permission denied? If so try to safe into a non user folder like c:/test or something. – Matte Jun 13 '17 at 6:34
  • The error message is: ERROR 4: ERROR 4: C:/Users/Felix/workspace/MyMap//Datos/Output/ndvi1.tif: No such file or directory Using double slashes as Freighty suggested does not help. As I said I thnk the problem is that the rastercalculator does not save the file properly, so it obviously does not exist when I call it with QGSRasterLayer. I also tried saving it to C:/test/ndvi.tif but it didn't work :( – Felix Jun 13 '17 at 17:09
1

I had the same issue, tried all of your advices. The one that worked for me was to work in another disk, for example E:/ ..... Think that the problem is that my user, "José" was no good for qgis to search the file.

0

In case someone else encounters that problem - running QGIS as an administrator might help. If not, try placing the files on different disk as mention above.

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.