2828import AlgorithmsTestBase
2929from processing .algs .gdal .OgrToPostGis import OgrToPostGis
3030from processing .algs .gdal .GdalUtils import GdalUtils
31+ from processing .algs .gdal .translate import translate
3132from qgis .core import (QgsProcessingContext ,
3233 QgsProcessingFeedback ,
3334 QgsApplication ,
@@ -91,7 +92,8 @@ def testGetOgrCompatibleSourceFromMemoryLayer(self):
9192 parameters = {'INPUT' : 'testmem' }
9293 feedback = QgsProcessingFeedback ()
9394 # check that memory layer is automatically saved out to shape when required by GDAL algorithms
94- ogr_data_path , ogr_layer_name = alg .getOgrCompatibleSource ('INPUT' , parameters , context , feedback , executing = True )
95+ ogr_data_path , ogr_layer_name = alg .getOgrCompatibleSource ('INPUT' , parameters , context , feedback ,
96+ executing = True )
9597 self .assertTrue (ogr_data_path )
9698 self .assertTrue (ogr_data_path .endswith ('.shp' ))
9799 self .assertTrue (os .path .exists (ogr_data_path ))
@@ -180,9 +182,76 @@ def _copyFile(dst):
180182 self .assertEqual (name , 't' )
181183
182184 # PostgreSQL provider
183- name = GdalUtils .ogrLayerName ('port=5493 sslmode=disable key=\' edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=' )
185+ name = GdalUtils .ogrLayerName (
186+ 'port=5493 sslmode=disable key=\' edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=' )
184187 self .assertEqual (name , 'city_data.edge' )
185188
189+ def testGdalTranslate (self ):
190+ context = QgsProcessingContext ()
191+ feedback = QgsProcessingFeedback ()
192+ source = os .path .join (testDataPath , 'dem.tif' )
193+ translate_alg = translate ()
194+ translate_alg .initAlgorithm ()
195+
196+ # with no NODATA value
197+ self .assertEqual (
198+ translate_alg .getConsoleCommands ({'INPUT' : source ,
199+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
200+ ['gdal_translate' ,
201+ '-ot Float32 -of JPEG ' +
202+ source + ' ' +
203+ 'd:/temp/check.jpg' ])
204+ # with NODATA value
205+ self .assertEqual (
206+ translate_alg .getConsoleCommands ({'INPUT' : source ,
207+ 'NODATA' : 9999 ,
208+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
209+ ['gdal_translate' ,
210+ '-a_nodata 9999.0 ' +
211+ '-ot Float32 -of JPEG ' +
212+ source + ' ' +
213+ 'd:/temp/check.jpg' ])
214+ # with "0" NODATA value
215+ self .assertEqual (
216+ translate_alg .getConsoleCommands ({'INPUT' : source ,
217+ 'NODATA' : 0 ,
218+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
219+ ['gdal_translate' ,
220+ '-a_nodata 0.0 ' +
221+ '-ot Float32 -of JPEG ' +
222+ source + ' ' +
223+ 'd:/temp/check.jpg' ])
224+ # with target srs
225+ self .assertEqual (
226+ translate_alg .getConsoleCommands ({'INPUT' : source ,
227+ 'TARGET_CRS' : 'EPSG:3111' ,
228+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
229+ ['gdal_translate' ,
230+ '-a_srs EPSG:3111 ' +
231+ '-ot Float32 -of JPEG ' +
232+ source + ' ' +
233+ 'd:/temp/check.jpg' ])
234+ # with target srs
235+ self .assertEqual (
236+ translate_alg .getConsoleCommands ({'INPUT' : source ,
237+ 'TARGET_CRS' : 'EPSG:3111' ,
238+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
239+ ['gdal_translate' ,
240+ '-a_srs EPSG:3111 ' +
241+ '-ot Float32 -of JPEG ' +
242+ source + ' ' +
243+ 'd:/temp/check.jpg' ])
244+ # with copy subdatasets
245+ self .assertEqual (
246+ translate_alg .getConsoleCommands ({'INPUT' : source ,
247+ 'COPY_SUBDATASETS' : True ,
248+ 'OUTPUT' : 'd:/temp/check.tif' }, context , feedback ),
249+ ['gdal_translate' ,
250+ '-sds ' +
251+ '-ot Float32 -of GTiff ' +
252+ source + ' ' +
253+ 'd:/temp/check.tif' ])
254+
186255
187256class TestGdalOgrToPostGis (unittest .TestCase ):
188257
@@ -198,7 +267,6 @@ def tearDownClass(cls):
198267
199268 # See https://issues.qgis.org/issues/15706
200269 def test_getConnectionString (self ):
201-
202270 obj = OgrToPostGis ()
203271 obj .initAlgorithm ({})
204272
0 commit comments