2

I'm working on PyQgis.

I have to convert a raster file to a file .asc. The classic procedure through the Qgis Menu is:

Raster -> Translate (convert format) -> on "Ouput file" select Arc / Info ASCII Grid.

Can this procedure be queried by the python console? What is the command?

EDIT

I tried to run:

processing.runalg('gdalogr:translate', '/path/to/raster.tif',100.0,True,None,0,'',None,False,5,4,75.0,6.0,1.0,False,0,False,None,'/path/to/output.asc')

But the result was:

Error: Wrong parameter value: None

Why?

mgri
16.4k6 gold badges48 silver badges80 bronze badges
asked Aug 2, 2017 at 21:22
3
  • You can use GDAL_Translate -of AAIGRID from a command window. The raster translate menu is only a shortcut to GDAL_Translate. gdal.org/gdal_translate.html If you only have QGIS installed and not GDAL the paths may not be set correctly, open your command window in the folder that contains the file gdal_translate.exe and it should work; there's no external driver needed for this format so paths shouldn't be an issue. Commented Aug 2, 2017 at 21:28
  • @MichaelStimson sorry but your answer does not help me. I edited my question to make it clearer Commented Aug 2, 2017 at 22:10
  • It looks like your rtype needs to be specified (last parameter before output file) docs.qgis.org/2.6/en/docs/user_manual/processing_algs/gdalogr/… (default 5 = Float32). Commented Aug 2, 2017 at 22:44

1 Answer 1

2

It's probably due to the PROJWIN <ParameterExtent> parameter where you need to specify the extent of the raster layer.

Try running the following:

from PyQt4.QtCore import QFileInfo
import processing
raster = 'path/to/raster'
fileInfo = QFileInfo(raster)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(raster, baseName)
extent = rlayer.extent()
xmin, xmax, ymin, ymax = extent.xMinimum(), extent.xMaximum(), extent.yMinimum(), extent.yMaximum()
processing.runalg('gdalogr:translate',rlayer,100.0,True,None,0,'',"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),False,5,4,75.0,6.0,1.0,False,0,False,None,'/path/to/output.asc')
answered Aug 3, 2017 at 9:06
4
  • I've already tried this solution and created the file .asc but when I load it on Qgis I get a visual result that is identical, but the maximum and minimum values (in the layer panel legend) are wrong. It is as if the 2 values reported in the legend represent the total number of NODATA and the total number of valid data and not the real value. Commented Aug 3, 2017 at 10:01
  • @DomenicoFuoco - Right-click your .asc file and to go Properties > Style. In the Load min/max values section, choose the Min / max and set the Accuracy to Actual (slower). Then click Load. This should give you the correct result :) Commented Aug 3, 2017 at 10:04
  • 1
    I'm sorry, I had made a mistake in returning your procedure. It works perfectly. Thank you! Commented Aug 3, 2017 at 10:51
  • @DomenicoFuoco - No problem! Glad you got it working :) Commented Aug 3, 2017 at 10:54

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.