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?
-
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.Michael Stimson– Michael Stimson2017年08月02日 21:28:55 +00:00Commented Aug 2, 2017 at 21:28
-
@MichaelStimson sorry but your answer does not help me. I edited my question to make it clearerDomenico Fuoco– Domenico Fuoco2017年08月02日 22:10:38 +00:00Commented 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).Michael Stimson– Michael Stimson2017年08月02日 22:44:29 +00:00Commented Aug 2, 2017 at 22:44
1 Answer 1
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')
-
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.Domenico Fuoco– Domenico Fuoco2017年08月03日 10:01:08 +00:00Commented 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 theMin / max
and set the Accuracy toActual (slower)
. Then click Load. This should give you the correct result :)Joseph– Joseph2017年08月03日 10:04:40 +00:00Commented Aug 3, 2017 at 10:04 -
1I'm sorry, I had made a mistake in returning your procedure. It works perfectly. Thank you!Domenico Fuoco– Domenico Fuoco2017年08月03日 10:51:46 +00:00Commented Aug 3, 2017 at 10:51
-
@DomenicoFuoco - No problem! Glad you got it working :)Joseph– Joseph2017年08月03日 10:54:22 +00:00Commented Aug 3, 2017 at 10:54
Explore related questions
See similar questions with these tags.