2

I would like to get the quantiles of a .jp2 raster by the following code, and write the results to a text file:

from qgis.core import QgsRasterLayer
from PyQt4.QtCore import QFileInfo
import processing
import gdal

fileName = "d:/bitbucket/cnn-lcm/T33TWM_A012703_20171127T100339_B04.jp2"
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)
if not rlayer.isValid():
  print "Layer failed to load!"

ext = rlayer.extent()
xmin = ext.xMinimum()
xmax = ext.xMaximum()
ymin = ext.yMinimum()
ymax = ext.yMaximum()
coords = "%f,%f,%f,%f" %(xmin, xmax, ymin, ymax)

layer = processing.getObject(rlayer.source())
processing.runalg("grass:r.quantile",fileName,5, "",coords,"","d:/bitbucket/cnn-lcm/T33TWM_A012703_20171127T100339_B04_q.txt")

If I call the procedure from Processing Toolbox GUI it works, but I have to solve it by script.

Error message:

[Errno 22] invalid mode ('w') or filename: '' See log for more details

2
  • i don't know that much of pyqgis but is the grass.script called by any of those libraries?
    – Elio Diaz
    Dec 27 '17 at 2:27
  • I don't have any issue with your code (except for third parameter that is boolean; not string). If you still experience problems, check your path file out carefully .
    – xunilk
    Dec 27 '17 at 17:57
3

Parameters for this processing tool are:

ALGORITHM: r.quantile - Compute quantiles using two passes.
    input <ParameterRaster>
    quantiles <ParameterNumber>
    -r <ParameterBoolean>
    GRASS_REGION_PARAMETER <ParameterExtent>
    html <OutputHTML>
    rawoutput <OutputFile>

So, you have one issue in your code: third parameter is boolean (not string).

I tried out a following modified version of your code (with my path to jp2 file):

from qgis.core import QgsRasterLayer
from PyQt4.QtCore import QFileInfo
import processing
import gdal

fileName = "/home/zeito/S2A_MSIL1C_20170422T110651_N0204_R137_T30STF_20170422T111300.SAFE/GRANULE/L1C_T30STF_A009572_20170422T111300/IMG_DATA/T30STF_20170422T110651_B01.jp2"
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)
if not rlayer.isValid():
  print "Layer failed to load!"

ext = rlayer.extent()
xmin = ext.xMinimum()
xmax = ext.xMaximum()
ymin = ext.yMinimum()
ymax = ext.yMaximum()
coords = "%f,%f,%f,%f" %(xmin, xmax, ymin, ymax)

#layer = processing.getObject(rlayer.source()) not necessary line

processing.runalg("grass:r.quantile",
                  fileName,
                  5, 
                  False,
                  coords,
                  None,
                  "/home/zeito/S2A_MSIL1C_20170422T110651_N0204_R137_T30STF_20170422T111300.SAFE/GRANULE/L1C_T30STF_A009572_20170422T111300/IMG_DATA/T30STF_20170422T110651_B01.txt")

and I got a successful result for my text file after running it at Python Console of QGIS:

0:20.000000:1530.000000
1:40.000000:1636.000000
2:60.000000:1834.000000
3:80.000000:3477.000000
1

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.