GdalAlgorithmsRasterTest.py 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971
  1. """
  2. ***************************************************************************
  3. GdalAlgorithmRasterTest.py
  4. ---------------------
  5. Date : January 2016
  6. Copyright : (C) 2016 by Matthias Kuhn
  7. Email : matthias@opengis.ch
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Matthias Kuhn'
  18. __date__ = 'January 2016'
  19. __copyright__ = '(C) 2016, Matthias Kuhn'
  20. import nose2
  21. import os
  22. import shutil
  23. import tempfile
  24. from qgis.core import (QgsProcessingContext,
  25. QgsProcessingException,
  26. QgsProcessingFeedback,
  27. QgsRectangle,
  28. QgsReferencedRectangle,
  29. QgsRasterLayer,
  30. QgsProject,
  31. QgsProjUtils,
  32. QgsPointXY,
  33. QgsCoordinateReferenceSystem)
  34. from qgis.testing import (start_app,
  35. unittest)
  36. import AlgorithmsTestBase
  37. from processing.algs.gdal.GdalUtils import GdalUtils
  38. from processing.algs.gdal.AssignProjection import AssignProjection
  39. from processing.algs.gdal.ClipRasterByExtent import ClipRasterByExtent
  40. from processing.algs.gdal.ClipRasterByMask import ClipRasterByMask
  41. from processing.algs.gdal.ColorRelief import ColorRelief
  42. from processing.algs.gdal.GridAverage import GridAverage
  43. from processing.algs.gdal.GridDataMetrics import GridDataMetrics
  44. from processing.algs.gdal.GridInverseDistance import GridInverseDistance
  45. from processing.algs.gdal.GridInverseDistanceNearestNeighbor import GridInverseDistanceNearestNeighbor
  46. from processing.algs.gdal.GridLinear import GridLinear
  47. from processing.algs.gdal.GridNearestNeighbor import GridNearestNeighbor
  48. from processing.algs.gdal.gdal2tiles import gdal2tiles
  49. from processing.algs.gdal.gdalcalc import gdalcalc
  50. from processing.algs.gdal.gdaltindex import gdaltindex
  51. from processing.algs.gdal.contour import contour, contour_polygon
  52. from processing.algs.gdal.gdalinfo import gdalinfo
  53. from processing.algs.gdal.hillshade import hillshade
  54. from processing.algs.gdal.aspect import aspect
  55. from processing.algs.gdal.buildvrt import buildvrt
  56. from processing.algs.gdal.proximity import proximity
  57. from processing.algs.gdal.rasterize import rasterize
  58. from processing.algs.gdal.retile import retile
  59. from processing.algs.gdal.translate import translate
  60. from processing.algs.gdal.warp import warp
  61. from processing.algs.gdal.fillnodata import fillnodata
  62. from processing.algs.gdal.rearrange_bands import rearrange_bands
  63. from processing.algs.gdal.gdaladdo import gdaladdo
  64. from processing.algs.gdal.sieve import sieve
  65. from processing.algs.gdal.gdal2xyz import gdal2xyz
  66. from processing.algs.gdal.polygonize import polygonize
  67. from processing.algs.gdal.pansharp import pansharp
  68. from processing.algs.gdal.merge import merge
  69. from processing.algs.gdal.nearblack import nearblack
  70. from processing.algs.gdal.slope import slope
  71. from processing.algs.gdal.rasterize_over import rasterize_over
  72. from processing.algs.gdal.rasterize_over_fixed_value import rasterize_over_fixed_value
  73. from processing.algs.gdal.viewshed import viewshed
  74. from processing.algs.gdal.roughness import roughness
  75. from processing.algs.gdal.pct2rgb import pct2rgb
  76. from processing.algs.gdal.rgb2pct import rgb2pct
  77. testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')
  78. class TestGdalRasterAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
  79. @classmethod
  80. def setUpClass(cls):
  81. start_app()
  82. from processing.core.Processing import Processing
  83. Processing.initialize()
  84. cls.cleanup_paths = []
  85. @classmethod
  86. def tearDownClass(cls):
  87. for path in cls.cleanup_paths:
  88. shutil.rmtree(path)
  89. def test_definition_file(self):
  90. return 'gdal_algorithm_raster_tests.yaml'
  91. @staticmethod
  92. def get_param_value_and_expected_string_for_custom_crs(proj_def):
  93. crs = QgsCoordinateReferenceSystem.fromProj(proj_def)
  94. custom_crs = f'proj4: {proj_def}'
  95. return custom_crs, crs.toWkt(QgsCoordinateReferenceSystem.WKT_PREFERRED_GDAL).replace('"', '"""')
  96. def testAssignProjection(self):
  97. context = QgsProcessingContext()
  98. feedback = QgsProcessingFeedback()
  99. source = os.path.join(testDataPath, 'dem.tif')
  100. alg = AssignProjection()
  101. alg.initAlgorithm()
  102. # with target srs
  103. self.assertEqual(
  104. alg.getConsoleCommands({'INPUT': source,
  105. 'CRS': 'EPSG:3111'}, context, feedback),
  106. ['gdal_edit.py',
  107. '-a_srs EPSG:3111 ' +
  108. source])
  109. # with target using proj string
  110. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  111. self.assertEqual(
  112. alg.getConsoleCommands({'INPUT': source,
  113. 'CRS': custom_crs}, context, feedback),
  114. ['gdal_edit.py',
  115. '-a_srs EPSG:20936 ' +
  116. source])
  117. # with target using custom projection
  118. custom_crs, expected_crs_string = self.get_param_value_and_expected_string_for_custom_crs(
  119. '+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
  120. self.assertEqual(
  121. alg.getConsoleCommands({'INPUT': source,
  122. 'CRS': custom_crs}, context, feedback),
  123. ['gdal_edit.py',
  124. f'-a_srs "{expected_crs_string}" ' +
  125. source])
  126. # with non-EPSG crs code
  127. self.assertEqual(
  128. alg.getConsoleCommands({'INPUT': source,
  129. 'CRS': 'POSTGIS:3111'}, context, feedback),
  130. ['gdal_edit.py',
  131. '-a_srs EPSG:3111 ' +
  132. source])
  133. @unittest.skipIf(os.environ.get('TRAVIS', '') == 'true',
  134. 'gdal_edit.py: not found')
  135. def testRunAssignProjection(self):
  136. # Check that assign projection updates QgsRasterLayer info
  137. # GDAL Assign Projection is based on gdal_edit.py
  138. context = QgsProcessingContext()
  139. feedback = QgsProcessingFeedback()
  140. source = os.path.join(testDataPath, 'dem.tif')
  141. alg = AssignProjection()
  142. alg.initAlgorithm()
  143. with tempfile.TemporaryDirectory() as outdir:
  144. fake_dem = os.path.join(outdir, 'dem-fake-crs.tif')
  145. shutil.copy(source, fake_dem)
  146. self.assertTrue(os.path.exists(fake_dem))
  147. rlayer = QgsRasterLayer(fake_dem, "Fake dem")
  148. self.assertTrue(rlayer.isValid())
  149. self.assertEqual(rlayer.crs().authid(), 'EPSG:4326')
  150. project = QgsProject()
  151. project.setFileName(os.path.join(outdir, 'dem-fake-crs.qgs'))
  152. project.addMapLayer(rlayer)
  153. self.assertEqual(project.count(), 1)
  154. context.setProject(project)
  155. alg.run({'INPUT': fake_dem, 'CRS': 'EPSG:3111'},
  156. context, feedback)
  157. self.assertEqual(rlayer.crs().authid(), 'EPSG:3111')
  158. def testGdalTranslate(self):
  159. context = QgsProcessingContext()
  160. feedback = QgsProcessingFeedback()
  161. source = os.path.join(testDataPath, 'dem.tif')
  162. translate_alg = translate()
  163. translate_alg.initAlgorithm()
  164. with tempfile.TemporaryDirectory() as outdir:
  165. # without NODATA value
  166. self.assertEqual(
  167. translate_alg.getConsoleCommands({'INPUT': source,
  168. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  169. ['gdal_translate',
  170. '-of JPEG ' +
  171. source + ' ' +
  172. outdir + '/check.jpg'])
  173. # with None NODATA value
  174. self.assertEqual(
  175. translate_alg.getConsoleCommands({'INPUT': source,
  176. 'NODATA': None,
  177. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  178. ['gdal_translate',
  179. '-of JPEG ' +
  180. source + ' ' +
  181. outdir + '/check.jpg'])
  182. # with NODATA value
  183. self.assertEqual(
  184. translate_alg.getConsoleCommands({'INPUT': source,
  185. 'NODATA': 9999,
  186. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  187. ['gdal_translate',
  188. '-a_nodata 9999.0 ' +
  189. '-of JPEG ' +
  190. source + ' ' +
  191. outdir + '/check.jpg'])
  192. # with "0" NODATA value
  193. self.assertEqual(
  194. translate_alg.getConsoleCommands({'INPUT': source,
  195. 'NODATA': 0,
  196. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  197. ['gdal_translate',
  198. '-a_nodata 0.0 ' +
  199. '-of JPEG ' +
  200. source + ' ' +
  201. outdir + '/check.jpg'])
  202. # with "0" NODATA value and custom data type
  203. self.assertEqual(
  204. translate_alg.getConsoleCommands({'INPUT': source,
  205. 'NODATA': 0,
  206. 'DATA_TYPE': 6,
  207. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  208. ['gdal_translate',
  209. '-a_nodata 0.0 ' +
  210. '-ot Float32 -of JPEG ' +
  211. source + ' ' +
  212. outdir + '/check.jpg'])
  213. # with target srs
  214. self.assertEqual(
  215. translate_alg.getConsoleCommands({'INPUT': source,
  216. 'TARGET_CRS': 'EPSG:3111',
  217. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  218. ['gdal_translate',
  219. '-a_srs EPSG:3111 ' +
  220. '-of JPEG ' +
  221. source + ' ' +
  222. outdir + '/check.jpg'])
  223. # with target using proj string
  224. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  225. self.assertEqual(
  226. translate_alg.getConsoleCommands({'INPUT': source,
  227. 'TARGET_CRS': custom_crs,
  228. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  229. ['gdal_translate',
  230. '-a_srs EPSG:20936 ' +
  231. '-of JPEG ' +
  232. source + ' ' +
  233. outdir + '/check.jpg'])
  234. # with target using custom projection
  235. custom_crs, expected_crs_string = self.get_param_value_and_expected_string_for_custom_crs('+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
  236. self.assertEqual(
  237. translate_alg.getConsoleCommands({'INPUT': source,
  238. 'TARGET_CRS': custom_crs,
  239. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  240. ['gdal_translate',
  241. f'-a_srs "{expected_crs_string}" ' +
  242. '-of JPEG ' +
  243. source + ' ' +
  244. outdir + '/check.jpg'])
  245. # with non-EPSG crs code
  246. self.assertEqual(
  247. translate_alg.getConsoleCommands({'INPUT': source,
  248. 'TARGET_CRS': 'POSTGIS:3111',
  249. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  250. ['gdal_translate',
  251. '-a_srs EPSG:3111 ' +
  252. '-of JPEG ' +
  253. source + ' ' +
  254. outdir + '/check.jpg'])
  255. # with copy subdatasets
  256. self.assertEqual(
  257. translate_alg.getConsoleCommands({'INPUT': source,
  258. 'COPY_SUBDATASETS': True,
  259. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  260. ['gdal_translate',
  261. '-sds ' +
  262. '-of GTiff ' +
  263. source + ' ' +
  264. outdir + '/check.tif'])
  265. # additional parameters
  266. self.assertEqual(
  267. translate_alg.getConsoleCommands({'INPUT': source,
  268. 'EXTRA': '-strict -unscale -epo',
  269. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  270. ['gdal_translate',
  271. '-of JPEG -strict -unscale -epo ' +
  272. source + ' ' +
  273. outdir + '/check.jpg'])
  274. def testClipRasterByExtent(self):
  275. context = QgsProcessingContext()
  276. feedback = QgsProcessingFeedback()
  277. source = os.path.join(testDataPath, 'dem.tif')
  278. alg = ClipRasterByExtent()
  279. alg.initAlgorithm()
  280. extent = QgsRectangle(1, 2, 3, 4)
  281. with tempfile.TemporaryDirectory() as outdir:
  282. # with no NODATA value
  283. self.assertEqual(
  284. alg.getConsoleCommands({'INPUT': source,
  285. 'EXTENT': extent,
  286. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  287. ['gdal_translate',
  288. '-of JPEG ' +
  289. source + ' ' +
  290. outdir + '/check.jpg'])
  291. # with NODATA value
  292. self.assertEqual(
  293. alg.getConsoleCommands({'INPUT': source,
  294. 'EXTENT': extent,
  295. 'NODATA': 9999,
  296. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  297. ['gdal_translate',
  298. '-a_nodata 9999.0 -of JPEG ' +
  299. source + ' ' +
  300. outdir + '/check.jpg'])
  301. # with "0" NODATA value
  302. self.assertEqual(
  303. alg.getConsoleCommands({'INPUT': source,
  304. 'EXTENT': extent,
  305. 'NODATA': 0,
  306. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  307. ['gdal_translate',
  308. '-a_nodata 0.0 -of JPEG ' +
  309. source + ' ' +
  310. outdir + '/check.jpg'])
  311. # with "0" NODATA value and custom data type
  312. self.assertEqual(
  313. alg.getConsoleCommands({'INPUT': source,
  314. 'EXTENT': extent,
  315. 'NODATA': 0,
  316. 'DATA_TYPE': 6,
  317. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  318. ['gdal_translate',
  319. '-a_nodata 0.0 -ot Float32 -of JPEG ' +
  320. source + ' ' +
  321. outdir + '/check.jpg'])
  322. # with creation options
  323. self.assertEqual(
  324. alg.getConsoleCommands({'INPUT': source,
  325. 'EXTENT': extent,
  326. 'OPTIONS': 'COMPRESS=DEFLATE|PREDICTOR=2|ZLEVEL=9',
  327. 'DATA_TYPE': 0,
  328. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  329. ['gdal_translate',
  330. '-of JPEG -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 ' +
  331. source + ' ' +
  332. outdir + '/check.jpg'])
  333. # with additional parameters
  334. self.assertEqual(
  335. alg.getConsoleCommands({'INPUT': source,
  336. 'EXTENT': extent,
  337. 'EXTRA': '-s_srs EPSG:4326 -tps -tr 0.1 0.1',
  338. 'DATA_TYPE': 0,
  339. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  340. ['gdal_translate',
  341. '-of JPEG -s_srs EPSG:4326 -tps -tr 0.1 0.1 ' +
  342. source + ' ' +
  343. outdir + '/check.jpg'])
  344. # override CRS
  345. self.assertEqual(
  346. alg.getConsoleCommands({'INPUT': source,
  347. 'EXTENT': extent,
  348. 'OVERCRS': True,
  349. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  350. ['gdal_translate',
  351. '-a_srs EPSG:4326 -of JPEG ' +
  352. source + ' ' +
  353. outdir + '/check.jpg'])
  354. def testClipRasterByMask(self):
  355. context = QgsProcessingContext()
  356. feedback = QgsProcessingFeedback()
  357. source = os.path.join(testDataPath, 'dem.tif')
  358. mask = os.path.join(testDataPath, 'polys.gml')
  359. extent = QgsReferencedRectangle(QgsRectangle(1, 2, 3, 4), QgsCoordinateReferenceSystem('EPSG:4236'))
  360. alg = ClipRasterByMask()
  361. alg.initAlgorithm()
  362. with tempfile.TemporaryDirectory() as outdir:
  363. # with no NODATA value
  364. self.assertEqual(
  365. alg.getConsoleCommands({'INPUT': source,
  366. 'MASK': mask,
  367. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  368. ['gdalwarp',
  369. '-overwrite -of JPEG -cutline ' +
  370. mask + ' -cl polys2 -crop_to_cutline ' + source + ' ' +
  371. outdir + '/check.jpg'])
  372. # with NODATA value
  373. self.assertEqual(
  374. alg.getConsoleCommands({'INPUT': source,
  375. 'MASK': mask,
  376. 'NODATA': 9999,
  377. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  378. ['gdalwarp',
  379. '-overwrite -of JPEG -cutline ' +
  380. mask + ' -cl polys2 -crop_to_cutline -dstnodata 9999.0 ' + source + ' ' +
  381. outdir + '/check.jpg'])
  382. # with "0" NODATA value
  383. self.assertEqual(
  384. alg.getConsoleCommands({'INPUT': source,
  385. 'MASK': mask,
  386. 'NODATA': 0,
  387. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  388. ['gdalwarp',
  389. '-overwrite -of JPEG -cutline ' +
  390. mask + ' -cl polys2 -crop_to_cutline -dstnodata 0.0 ' + source + ' ' +
  391. outdir + '/check.jpg'])
  392. # with "0" NODATA value and custom data type
  393. self.assertEqual(
  394. alg.getConsoleCommands({'INPUT': source,
  395. 'MASK': mask,
  396. 'NODATA': 0,
  397. 'DATA_TYPE': 6,
  398. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  399. ['gdalwarp',
  400. '-overwrite -ot Float32 -of JPEG -cutline ' +
  401. mask + ' -cl polys2 -crop_to_cutline -dstnodata 0.0 ' + source + ' ' +
  402. outdir + '/check.jpg'])
  403. # with creation options
  404. self.assertEqual(
  405. alg.getConsoleCommands({'INPUT': source,
  406. 'MASK': mask,
  407. 'OPTIONS': 'COMPRESS=DEFLATE|PREDICTOR=2|ZLEVEL=9',
  408. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  409. ['gdalwarp',
  410. '-overwrite -of JPEG -cutline ' +
  411. mask + ' -cl polys2 -crop_to_cutline -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 ' +
  412. source + ' ' +
  413. outdir + '/check.jpg'])
  414. # with multothreading and additional parameters
  415. self.assertEqual(
  416. alg.getConsoleCommands({'INPUT': source,
  417. 'MASK': mask,
  418. 'MULTITHREADING': True,
  419. 'EXTRA': '-nosrcalpha -wm 2048 -nomd',
  420. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  421. ['gdalwarp',
  422. '-overwrite -of JPEG -cutline ' +
  423. mask + ' -cl polys2 -crop_to_cutline -multi -nosrcalpha -wm 2048 -nomd ' +
  424. source + ' ' +
  425. outdir + '/check.jpg'])
  426. # with target extent value
  427. self.assertEqual(
  428. alg.getConsoleCommands({'INPUT': source,
  429. 'MASK': mask,
  430. 'TARGET_EXTENT': extent,
  431. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  432. ['gdalwarp',
  433. '-overwrite -te 1.0 2.0 3.0 4.0 -te_srs EPSG:4236 -of JPEG -cutline ' +
  434. mask + ' -cl polys2 -crop_to_cutline ' + source + ' ' +
  435. outdir + '/check.jpg'])
  436. def testContourPolygon(self):
  437. context = QgsProcessingContext()
  438. feedback = QgsProcessingFeedback()
  439. source = os.path.join(testDataPath, 'dem.tif')
  440. alg = contour_polygon()
  441. alg.initAlgorithm()
  442. with tempfile.TemporaryDirectory() as outdir:
  443. self.assertEqual(
  444. alg.getConsoleCommands({'INPUT': source,
  445. 'BAND': 1,
  446. 'FIELD_NAME_MIN': 'min',
  447. 'FIELD_NAME_MAX': 'max',
  448. 'INTERVAL': 5,
  449. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  450. ['gdal_contour',
  451. '-p -amax max -amin min -b 1 -i 5.0 -f "ESRI Shapefile" ' +
  452. source + ' ' +
  453. outdir + '/check.shp'])
  454. def testContour(self):
  455. context = QgsProcessingContext()
  456. feedback = QgsProcessingFeedback()
  457. source = os.path.join(testDataPath, 'dem.tif')
  458. alg = contour()
  459. alg.initAlgorithm()
  460. with tempfile.TemporaryDirectory() as outdir:
  461. # with no NODATA value
  462. self.assertEqual(
  463. alg.getConsoleCommands({'INPUT': source,
  464. 'BAND': 1,
  465. 'FIELD_NAME': 'elev',
  466. 'INTERVAL': 5,
  467. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  468. ['gdal_contour',
  469. '-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' +
  470. source + ' ' +
  471. outdir + '/check.shp'])
  472. # with NODATA value
  473. self.assertEqual(
  474. alg.getConsoleCommands({'INPUT': source,
  475. 'BAND': 1,
  476. 'FIELD_NAME': 'elev',
  477. 'INTERVAL': 5,
  478. 'NODATA': 9999,
  479. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  480. ['gdal_contour',
  481. '-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' +
  482. source + ' ' +
  483. outdir + '/check.shp'])
  484. # with "0" NODATA value
  485. self.assertEqual(
  486. alg.getConsoleCommands({'INPUT': source,
  487. 'BAND': 1,
  488. 'FIELD_NAME': 'elev',
  489. 'INTERVAL': 5,
  490. 'NODATA': 0,
  491. 'OUTPUT': outdir + '/check.gpkg'}, context, feedback),
  492. ['gdal_contour',
  493. '-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' +
  494. source + ' ' +
  495. outdir + '/check.gpkg'])
  496. # with CREATE_3D
  497. self.assertEqual(
  498. alg.getConsoleCommands({'INPUT': source,
  499. 'BAND': 1,
  500. 'CREATE_3D': True,
  501. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  502. ['gdal_contour',
  503. '-b 1 -a ELEV -i 10.0 -3d -f "ESRI Shapefile" ' +
  504. source + ' ' +
  505. outdir + '/check.shp'])
  506. # with IGNORE_NODATA and OFFSET
  507. self.assertEqual(
  508. alg.getConsoleCommands({'INPUT': source,
  509. 'BAND': 1,
  510. 'IGNORE_NODATA': True,
  511. 'OFFSET': 100,
  512. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  513. ['gdal_contour',
  514. '-b 1 -a ELEV -i 10.0 -inodata -off 100.0 -f "ESRI Shapefile" ' +
  515. source + ' ' +
  516. outdir + '/check.shp'])
  517. # with additional command line parameters
  518. self.assertEqual(
  519. alg.getConsoleCommands({'INPUT': source,
  520. 'BAND': 1,
  521. 'EXTRA': '-e 3 -amin MIN_H',
  522. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  523. ['gdal_contour',
  524. '-b 1 -a ELEV -i 10.0 -f "ESRI Shapefile" -e 3 -amin MIN_H ' +
  525. source + ' ' +
  526. outdir + '/check.shp'])
  527. # obsolete OPTIONS param
  528. self.assertEqual(
  529. alg.getConsoleCommands({'INPUT': source,
  530. 'BAND': 1,
  531. 'OPTIONS': '-fl 100 125 150 200',
  532. 'OUTPUT': outdir + '/check.shp'}, context, feedback),
  533. ['gdal_contour',
  534. '-b 1 -a ELEV -i 10.0 -f "ESRI Shapefile" -fl 100 125 150 200 ' +
  535. source + ' ' +
  536. outdir + '/check.shp'])
  537. def testGdal2Tiles(self):
  538. context = QgsProcessingContext()
  539. feedback = QgsProcessingFeedback()
  540. source = os.path.join(testDataPath, 'dem.tif')
  541. alg = gdal2tiles()
  542. alg.initAlgorithm()
  543. with tempfile.TemporaryDirectory() as outdir:
  544. # with no NODATA value
  545. self.assertEqual(
  546. alg.getConsoleCommands({'INPUT': source,
  547. 'OUTPUT': outdir + '/'}, context, feedback),
  548. ['gdal2tiles.py',
  549. '-p mercator -w all -r average ' +
  550. source + ' ' +
  551. outdir + '/'])
  552. # with NODATA value
  553. self.assertEqual(
  554. alg.getConsoleCommands({'INPUT': source,
  555. 'NODATA': -9999,
  556. 'OUTPUT': outdir + '/'}, context, feedback),
  557. ['gdal2tiles.py',
  558. '-p mercator -w all -r average -a -9999.0 ' +
  559. source + ' ' +
  560. outdir + '/'])
  561. # with "0" NODATA value
  562. self.assertEqual(
  563. alg.getConsoleCommands({'INPUT': source,
  564. 'NODATA': 0,
  565. 'OUTPUT': outdir + '/'}, context, feedback),
  566. ['gdal2tiles.py',
  567. '-p mercator -w all -r average -a 0.0 ' +
  568. source + ' ' +
  569. outdir + '/'])
  570. # with input srs
  571. self.assertEqual(
  572. alg.getConsoleCommands({'INPUT': source,
  573. 'SOURCE_CRS': 'EPSG:3111',
  574. 'OUTPUT': outdir + '/'}, context, feedback),
  575. ['gdal2tiles.py',
  576. '-p mercator -w all -r average -s EPSG:3111 ' +
  577. source + ' ' +
  578. outdir + '/'])
  579. # with target using proj string
  580. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  581. self.assertEqual(
  582. alg.getConsoleCommands({'INPUT': source,
  583. 'SOURCE_CRS': custom_crs,
  584. 'OUTPUT': outdir + '/'}, context, feedback),
  585. ['gdal2tiles.py',
  586. '-p mercator -w all -r average -s EPSG:20936 ' +
  587. source + ' ' +
  588. outdir + '/'])
  589. # with target using custom projection
  590. custom_crs, expected_crs_string = self.get_param_value_and_expected_string_for_custom_crs('+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
  591. self.assertEqual(
  592. alg.getConsoleCommands({'INPUT': source,
  593. 'SOURCE_CRS': custom_crs,
  594. 'OUTPUT': outdir + '/'}, context, feedback),
  595. ['gdal2tiles.py',
  596. f'-p mercator -w all -r average -s "{expected_crs_string}" ' +
  597. source + ' ' +
  598. outdir + '/'])
  599. # with non-EPSG crs code
  600. self.assertEqual(
  601. alg.getConsoleCommands({'INPUT': source,
  602. 'SOURCE_CRS': 'POSTGIS:3111',
  603. 'OUTPUT': outdir + '/'}, context, feedback),
  604. ['gdal2tiles.py',
  605. '-p mercator -w all -r average -s EPSG:3111 ' +
  606. source + ' ' +
  607. outdir + '/'])
  608. def testGdalCalc(self):
  609. context = QgsProcessingContext()
  610. feedback = QgsProcessingFeedback()
  611. source = os.path.join(testDataPath, 'dem.tif')
  612. source2 = os.path.join(testDataPath, 'raster.tif')
  613. source3 = os.path.join(testDataPath, 'raster with spaces.tif')
  614. alg = gdalcalc()
  615. alg.initAlgorithm()
  616. with tempfile.TemporaryDirectory() as outdir:
  617. output = outdir + '/check.jpg'
  618. # default execution
  619. formula = 'A*2' # default formula
  620. self.assertEqual(
  621. alg.getConsoleCommands({'INPUT_A': source,
  622. 'BAND_A': 1,
  623. 'FORMULA': formula,
  624. 'OUTPUT': output}, context, feedback),
  625. ['gdal_calc.py',
  626. f'--overwrite --calc "{formula}" --format JPEG --type Float32 -A {source} --A_band 1 --outfile {output}'])
  627. if GdalUtils.version() >= 3030000:
  628. extent = QgsReferencedRectangle(QgsRectangle(1, 2, 3, 4), QgsCoordinateReferenceSystem('EPSG:4326'))
  629. self.assertEqual(
  630. alg.getConsoleCommands({'INPUT_A': source,
  631. 'BAND_A': 1,
  632. 'FORMULA': formula,
  633. 'PROJWIN': extent,
  634. 'OUTPUT': output}, context, feedback),
  635. ['gdal_calc.py',
  636. f'--overwrite --calc "{formula}" --format JPEG --type Float32 --projwin 1.0 4.0 3.0 2.0 -A {source} --A_band 1 --outfile {output}'])
  637. # Inputs A and B share same pixel size and CRS
  638. self.assertEqual(
  639. alg.getConsoleCommands({'INPUT_A': source2,
  640. 'BAND_A': 1,
  641. 'INPUT_B': source3,
  642. 'BAND_B': 1,
  643. 'FORMULA': formula,
  644. 'EXTENT_OPT': 3,
  645. 'OUTPUT': output}, context, feedback),
  646. ['gdal_calc.py',
  647. f'--overwrite --calc "{formula}" --format JPEG --type Float32 --extent=intersect -A {source2} --A_band 1 -B "{source3}" --B_band 1 --outfile {output}'])
  648. # Test mutually exclusive --extent and --projwin. Should raise an exception
  649. self.assertRaises(
  650. QgsProcessingException,
  651. lambda: alg.getConsoleCommands({'INPUT_A': source,
  652. 'BAND_A': 1,
  653. 'FORMULA': formula,
  654. 'PROJWIN': extent,
  655. 'EXTENT_OPT': 3,
  656. 'OUTPUT': output}, context, feedback))
  657. # Inputs A and B do not share same pixel size and CRS. Should raise an exception
  658. source2 = os.path.join(testDataPath, 'raster.tif')
  659. self.assertRaises(
  660. QgsProcessingException,
  661. lambda: alg.getConsoleCommands({'INPUT_A': source,
  662. 'BAND_A': 1,
  663. 'INPUT_B': source2,
  664. 'BAND_B': 1,
  665. 'FORMULA': formula,
  666. 'EXTENT_OPT': 3,
  667. 'OUTPUT': output}, context, feedback))
  668. # check that formula is not escaped and formula is returned as it is
  669. formula = 'A * 2' # <--- add spaces in the formula
  670. self.assertEqual(
  671. alg.getConsoleCommands({'INPUT_A': source,
  672. 'BAND_A': 1,
  673. 'FORMULA': formula,
  674. 'OUTPUT': output}, context, feedback),
  675. ['gdal_calc.py',
  676. f'--overwrite --calc "{formula}" --format JPEG --type Float32 -A {source} --A_band 1 --outfile {output}'])
  677. # additional creation options
  678. formula = 'A*2'
  679. self.assertEqual(
  680. alg.getConsoleCommands({'INPUT_A': source,
  681. 'BAND_A': 1,
  682. 'FORMULA': formula,
  683. 'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
  684. 'OUTPUT': output}, context, feedback),
  685. ['gdal_calc.py',
  686. f'--overwrite --calc "{formula}" --format JPEG --type Float32 -A {source} --A_band 1 --co COMPRESS=JPEG --co JPEG_QUALITY=75 --outfile {output}'])
  687. # additional parameters
  688. formula = 'A*2'
  689. self.assertEqual(
  690. alg.getConsoleCommands({'INPUT_A': source,
  691. 'BAND_A': 1,
  692. 'FORMULA': formula,
  693. 'EXTRA': '--debug --quiet',
  694. 'OUTPUT': output}, context, feedback),
  695. ['gdal_calc.py',
  696. f'--overwrite --calc "{formula}" --format JPEG --type Float32 -A {source} --A_band 1 --debug --quiet --outfile {output}'])
  697. def testGdalInfo(self):
  698. context = QgsProcessingContext()
  699. feedback = QgsProcessingFeedback()
  700. source = os.path.join(testDataPath, 'dem.tif')
  701. alg = gdalinfo()
  702. alg.initAlgorithm()
  703. self.assertEqual(
  704. alg.getConsoleCommands({'INPUT': source,
  705. 'MIN_MAX': False,
  706. 'NOGCP': False,
  707. 'NO_METADATA': False,
  708. 'STATS': False}, context, feedback),
  709. ['gdalinfo',
  710. source])
  711. source = os.path.join(testDataPath, 'raster with spaces.tif')
  712. self.assertEqual(
  713. alg.getConsoleCommands({'INPUT': source,
  714. 'MIN_MAX': False,
  715. 'NOGCP': False,
  716. 'NO_METADATA': False,
  717. 'STATS': False}, context, feedback),
  718. ['gdalinfo',
  719. '"' + source + '"'])
  720. self.assertEqual(
  721. alg.getConsoleCommands({'INPUT': source,
  722. 'MIN_MAX': True,
  723. 'NOGCP': False,
  724. 'NO_METADATA': False,
  725. 'STATS': False}, context, feedback),
  726. ['gdalinfo',
  727. '-mm "' + source + '"'])
  728. self.assertEqual(
  729. alg.getConsoleCommands({'INPUT': source,
  730. 'MIN_MAX': False,
  731. 'NOGCP': True,
  732. 'NO_METADATA': False,
  733. 'STATS': False}, context, feedback),
  734. ['gdalinfo',
  735. '-nogcp "' + source + '"'])
  736. self.assertEqual(
  737. alg.getConsoleCommands({'INPUT': source,
  738. 'MIN_MAX': False,
  739. 'NOGCP': False,
  740. 'NO_METADATA': True,
  741. 'STATS': False}, context, feedback),
  742. ['gdalinfo',
  743. '-nomd "' + source + '"'])
  744. self.assertEqual(
  745. alg.getConsoleCommands({'INPUT': source,
  746. 'MIN_MAX': False,
  747. 'NOGCP': False,
  748. 'NO_METADATA': False,
  749. 'STATS': True}, context, feedback),
  750. ['gdalinfo',
  751. '-stats "' + source + '"'])
  752. self.assertEqual(
  753. alg.getConsoleCommands({'INPUT': source,
  754. 'MIN_MAX': False,
  755. 'NOGCP': False,
  756. 'NO_METADATA': False,
  757. 'STATS': False,
  758. 'EXTRA': '-proj4 -listmdd -checksum'}, context, feedback),
  759. ['gdalinfo',
  760. '-proj4 -listmdd -checksum "' + source + '"'])
  761. def testGdalTindex(self):
  762. context = QgsProcessingContext()
  763. feedback = QgsProcessingFeedback()
  764. source = os.path.join(testDataPath, 'dem.tif')
  765. alg = gdaltindex()
  766. alg.initAlgorithm()
  767. with tempfile.TemporaryDirectory() as outdir:
  768. commands = alg.getConsoleCommands({'LAYERS': [source],
  769. 'OUTPUT': outdir + '/test.shp'}, context, feedback)
  770. self.assertEqual(len(commands), 2)
  771. self.assertEqual(commands[0], 'gdaltindex')
  772. self.assertIn('-tileindex location -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
  773. self.assertIn('--optfile ', commands[1])
  774. # with input srs
  775. commands = alg.getConsoleCommands({'LAYERS': [source],
  776. 'TARGET_CRS': 'EPSG:3111',
  777. 'OUTPUT': outdir + '/test.shp'}, context, feedback)
  778. self.assertEqual(len(commands), 2)
  779. self.assertEqual(commands[0], 'gdaltindex')
  780. self.assertIn('-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
  781. self.assertIn('--optfile ', commands[1])
  782. # with target using proj string
  783. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  784. commands = alg.getConsoleCommands({'LAYERS': [source],
  785. 'TARGET_CRS': custom_crs,
  786. 'OUTPUT': outdir + '/test.shp'}, context, feedback)
  787. self.assertEqual(len(commands), 2)
  788. self.assertEqual(commands[0], 'gdaltindex')
  789. self.assertIn('-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
  790. self.assertIn('--optfile ', commands[1])
  791. # with target using custom projection
  792. custom_crs, expected_crs_string = self.get_param_value_and_expected_string_for_custom_crs('+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
  793. commands = alg.getConsoleCommands({'LAYERS': [source],
  794. 'TARGET_CRS': custom_crs,
  795. 'OUTPUT': outdir + '/test.shp'}, context, feedback)
  796. self.assertEqual(len(commands), 2)
  797. self.assertEqual(commands[0], 'gdaltindex')
  798. self.assertIn(f'-tileindex location -t_srs "{expected_crs_string}" -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
  799. self.assertIn('--optfile ', commands[1])
  800. # with non-EPSG crs code
  801. commands = alg.getConsoleCommands({'LAYERS': [source],
  802. 'TARGET_CRS': 'POSTGIS:3111',
  803. 'OUTPUT': outdir + '/test.shp'}, context, feedback)
  804. self.assertEqual(len(commands), 2)
  805. self.assertEqual(commands[0], 'gdaltindex')
  806. self.assertIn(
  807. '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + outdir + '/test.shp',
  808. commands[1])
  809. self.assertIn('--optfile ', commands[1])
  810. def testGridAverage(self):
  811. context = QgsProcessingContext()
  812. feedback = QgsProcessingFeedback()
  813. source = os.path.join(testDataPath, 'points.gml')
  814. alg = GridAverage()
  815. alg.initAlgorithm()
  816. with tempfile.TemporaryDirectory() as outdir:
  817. # with no NODATA value
  818. self.assertEqual(
  819. alg.getConsoleCommands({'INPUT': source,
  820. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  821. ['gdal_grid',
  822. '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  823. source + ' ' +
  824. outdir + '/check.jpg'])
  825. # with NODATA value
  826. self.assertEqual(
  827. alg.getConsoleCommands({'INPUT': source,
  828. 'NODATA': 9999,
  829. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  830. ['gdal_grid',
  831. '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' +
  832. source + ' ' +
  833. outdir + '/check.jpg'])
  834. # with "0" NODATA value
  835. self.assertEqual(
  836. alg.getConsoleCommands({'INPUT': source,
  837. 'NODATA': 0,
  838. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  839. ['gdal_grid',
  840. '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  841. source + ' ' +
  842. outdir + '/check.jpg'])
  843. # with additional parameters
  844. self.assertEqual(
  845. alg.getConsoleCommands({'INPUT': source,
  846. 'EXTRA': '-z_multiply 1.5 -outsize 1754 1394',
  847. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  848. ['gdal_grid',
  849. '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG -z_multiply 1.5 -outsize 1754 1394 ' +
  850. source + ' ' +
  851. outdir + '/check.jpg'])
  852. def testGridDataMetrics(self):
  853. context = QgsProcessingContext()
  854. feedback = QgsProcessingFeedback()
  855. source = os.path.join(testDataPath, 'points.gml')
  856. alg = GridDataMetrics()
  857. alg.initAlgorithm()
  858. with tempfile.TemporaryDirectory() as outdir:
  859. # without NODATA value
  860. self.assertEqual(
  861. alg.getConsoleCommands({'INPUT': source,
  862. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  863. ['gdal_grid',
  864. '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  865. source + ' ' +
  866. outdir + '/check.jpg'])
  867. # with NODATA value
  868. self.assertEqual(
  869. alg.getConsoleCommands({'INPUT': source,
  870. 'NODATA': 9999,
  871. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  872. ['gdal_grid',
  873. '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' +
  874. source + ' ' +
  875. outdir + '/check.jpg'])
  876. # with "0" NODATA value
  877. self.assertEqual(
  878. alg.getConsoleCommands({'INPUT': source,
  879. 'NODATA': 0,
  880. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  881. ['gdal_grid',
  882. '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  883. source + ' ' +
  884. outdir + '/check.jpg'])
  885. # non-default datametrics
  886. self.assertEqual(
  887. alg.getConsoleCommands({'INPUT': source,
  888. 'METRIC': 4,
  889. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  890. ['gdal_grid',
  891. '-l points -a average_distance:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  892. source + ' ' +
  893. outdir + '/check.jpg'])
  894. # additional parameters
  895. self.assertEqual(
  896. alg.getConsoleCommands({'INPUT': source,
  897. 'EXTRA': '-z_multiply 1.5 -outsize 1754 1394',
  898. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  899. ['gdal_grid',
  900. '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 ' +
  901. '-ot Float32 -of GTiff -z_multiply 1.5 -outsize 1754 1394 ' +
  902. source + ' ' +
  903. outdir + '/check.tif'])
  904. def testGridInverseDistance(self):
  905. context = QgsProcessingContext()
  906. feedback = QgsProcessingFeedback()
  907. source = os.path.join(testDataPath, 'points.gml')
  908. alg = GridInverseDistance()
  909. alg.initAlgorithm()
  910. with tempfile.TemporaryDirectory() as outdir:
  911. # without NODATA value
  912. self.assertEqual(
  913. alg.getConsoleCommands({'INPUT': source,
  914. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  915. ['gdal_grid',
  916. '-l points -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  917. source + ' ' +
  918. outdir + '/check.jpg'])
  919. # with NODATA value
  920. self.assertEqual(
  921. alg.getConsoleCommands({'INPUT': source,
  922. 'NODATA': 9999,
  923. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  924. ['gdal_grid',
  925. '-l points -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' +
  926. source + ' ' +
  927. outdir + '/check.jpg'])
  928. # with "0" NODATA value
  929. self.assertEqual(
  930. alg.getConsoleCommands({'INPUT': source,
  931. 'NODATA': 0,
  932. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  933. ['gdal_grid',
  934. '-l points -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  935. source + ' ' +
  936. outdir + '/check.jpg'])
  937. # additional parameters
  938. self.assertEqual(
  939. alg.getConsoleCommands({'INPUT': source,
  940. 'EXTRA': '-z_multiply 1.5 -outsize 1754 1394',
  941. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  942. ['gdal_grid',
  943. '-l points -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 ' +
  944. '-ot Float32 -of GTiff -z_multiply 1.5 -outsize 1754 1394 ' +
  945. source + ' ' +
  946. outdir + '/check.tif'])
  947. def testGridInverseDistanceNearestNeighbour(self):
  948. context = QgsProcessingContext()
  949. feedback = QgsProcessingFeedback()
  950. source = os.path.join(testDataPath, 'points.gml')
  951. alg = GridInverseDistanceNearestNeighbor()
  952. alg.initAlgorithm()
  953. with tempfile.TemporaryDirectory() as outdir:
  954. # without NODATA value
  955. self.assertEqual(
  956. alg.getConsoleCommands({'INPUT': source,
  957. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  958. ['gdal_grid',
  959. '-l points -a invdistnn:power=2.0:smoothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  960. source + ' ' +
  961. outdir + '/check.jpg'])
  962. # with NODATA value
  963. self.assertEqual(
  964. alg.getConsoleCommands({'INPUT': source,
  965. 'NODATA': 9999,
  966. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  967. ['gdal_grid',
  968. '-l points -a invdistnn:power=2.0:smoothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' +
  969. source + ' ' +
  970. outdir + '/check.jpg'])
  971. # with "0" NODATA value
  972. self.assertEqual(
  973. alg.getConsoleCommands({'INPUT': source,
  974. 'NODATA': 0,
  975. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  976. ['gdal_grid',
  977. '-l points -a invdistnn:power=2.0:smoothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' +
  978. source + ' ' +
  979. outdir + '/check.jpg'])
  980. # additional parameters
  981. self.assertEqual(
  982. alg.getConsoleCommands({'INPUT': source,
  983. 'EXTRA': '-z_multiply 1.5 -outsize 1754 1394',
  984. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  985. ['gdal_grid',
  986. '-l points -a invdistnn:power=2.0:smoothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 ' +
  987. '-ot Float32 -of GTiff -z_multiply 1.5 -outsize 1754 1394 ' +
  988. source + ' ' +
  989. outdir + '/check.tif'])
  990. def testGridLinear(self):
  991. context = QgsProcessingContext()
  992. feedback = QgsProcessingFeedback()
  993. source = os.path.join(testDataPath, 'points.gml')
  994. alg = GridLinear()
  995. alg.initAlgorithm()
  996. with tempfile.TemporaryDirectory() as outdir:
  997. # without NODATA value
  998. self.assertEqual(
  999. alg.getConsoleCommands({'INPUT': source,
  1000. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1001. ['gdal_grid',
  1002. '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of JPEG ' +
  1003. source + ' ' +
  1004. outdir + '/check.jpg'])
  1005. # with NODATA value
  1006. self.assertEqual(
  1007. alg.getConsoleCommands({'INPUT': source,
  1008. 'NODATA': 9999,
  1009. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1010. ['gdal_grid',
  1011. '-l points -a linear:radius=-1.0:nodata=9999.0 -ot Float32 -of JPEG ' +
  1012. source + ' ' +
  1013. outdir + '/check.jpg'])
  1014. # with "0" NODATA value
  1015. self.assertEqual(
  1016. alg.getConsoleCommands({'INPUT': source,
  1017. 'NODATA': 0,
  1018. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1019. ['gdal_grid',
  1020. '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of JPEG ' +
  1021. source + ' ' +
  1022. outdir + '/check.jpg'])
  1023. # additional parameters
  1024. self.assertEqual(
  1025. alg.getConsoleCommands({'INPUT': source,
  1026. 'EXTRA': '-z_multiply 1.5 -outsize 1754 1394',
  1027. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1028. ['gdal_grid',
  1029. '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of GTiff ' +
  1030. '-z_multiply 1.5 -outsize 1754 1394 ' +
  1031. source + ' ' +
  1032. outdir + '/check.tif'])
  1033. def testGridNearestNeighbour(self):
  1034. context = QgsProcessingContext()
  1035. feedback = QgsProcessingFeedback()
  1036. source = os.path.join(testDataPath, 'points.gml')
  1037. alg = GridNearestNeighbor()
  1038. alg.initAlgorithm()
  1039. with tempfile.TemporaryDirectory() as outdir:
  1040. # without NODATA value
  1041. self.assertEqual(
  1042. alg.getConsoleCommands({'INPUT': source,
  1043. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1044. ['gdal_grid',
  1045. '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of JPEG ' +
  1046. source + ' ' +
  1047. outdir + '/check.jpg'])
  1048. # with NODATA value
  1049. self.assertEqual(
  1050. alg.getConsoleCommands({'INPUT': source,
  1051. 'NODATA': 9999,
  1052. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1053. ['gdal_grid',
  1054. '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=9999.0 -ot Float32 -of JPEG ' +
  1055. source + ' ' +
  1056. outdir + '/check.jpg'])
  1057. # with "0" NODATA value
  1058. self.assertEqual(
  1059. alg.getConsoleCommands({'INPUT': source,
  1060. 'NODATA': 0,
  1061. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1062. ['gdal_grid',
  1063. '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of JPEG ' +
  1064. source + ' ' +
  1065. outdir + '/check.jpg'])
  1066. # additional parameters
  1067. self.assertEqual(
  1068. alg.getConsoleCommands({'INPUT': source,
  1069. 'EXTRA': '-z_multiply 1.5 -outsize 1754 1394',
  1070. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1071. ['gdal_grid',
  1072. '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of GTiff ' +
  1073. '-z_multiply 1.5 -outsize 1754 1394 ' +
  1074. source + ' ' +
  1075. outdir + '/check.tif'])
  1076. def testHillshade(self):
  1077. context = QgsProcessingContext()
  1078. feedback = QgsProcessingFeedback()
  1079. source = os.path.join(testDataPath, 'dem.tif')
  1080. alg = hillshade()
  1081. alg.initAlgorithm()
  1082. with tempfile.TemporaryDirectory() as outdir:
  1083. self.assertEqual(
  1084. alg.getConsoleCommands({'INPUT': source,
  1085. 'BAND': 1,
  1086. 'Z_FACTOR': 5,
  1087. 'SCALE': 2,
  1088. 'AZIMUTH': 90,
  1089. 'ALTITUDE': 20,
  1090. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1091. ['gdaldem',
  1092. 'hillshade ' +
  1093. source + ' ' +
  1094. outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0'])
  1095. # paths with space
  1096. source_with_space = os.path.join(testDataPath, 'raster with spaces.tif')
  1097. self.assertEqual(
  1098. alg.getConsoleCommands({'INPUT': source_with_space,
  1099. 'BAND': 1,
  1100. 'Z_FACTOR': 5,
  1101. 'SCALE': 2,
  1102. 'AZIMUTH': 90,
  1103. 'ALTITUDE': 20,
  1104. 'OUTPUT': outdir + '/check out.tif'}, context, feedback),
  1105. ['gdaldem',
  1106. 'hillshade ' +
  1107. '"' + source_with_space + '" ' +
  1108. f'"{outdir}/check out.tif" -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0'])
  1109. # compute edges
  1110. self.assertEqual(
  1111. alg.getConsoleCommands({'INPUT': source,
  1112. 'BAND': 1,
  1113. 'Z_FACTOR': 5,
  1114. 'SCALE': 2,
  1115. 'AZIMUTH': 90,
  1116. 'ALTITUDE': 20,
  1117. 'COMPUTE_EDGES': True,
  1118. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1119. ['gdaldem',
  1120. 'hillshade ' +
  1121. source + ' ' +
  1122. outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -compute_edges'])
  1123. # with ZEVENBERGEN
  1124. self.assertEqual(
  1125. alg.getConsoleCommands({'INPUT': source,
  1126. 'BAND': 1,
  1127. 'Z_FACTOR': 5,
  1128. 'SCALE': 2,
  1129. 'AZIMUTH': 90,
  1130. 'ALTITUDE': 20,
  1131. 'ZEVENBERGEN': True,
  1132. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1133. ['gdaldem',
  1134. 'hillshade ' +
  1135. source + ' ' +
  1136. outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -alg ZevenbergenThorne'])
  1137. # with COMBINED
  1138. self.assertEqual(
  1139. alg.getConsoleCommands({'INPUT': source,
  1140. 'BAND': 1,
  1141. 'Z_FACTOR': 5,
  1142. 'SCALE': 2,
  1143. 'AZIMUTH': 90,
  1144. 'ALTITUDE': 20,
  1145. 'COMBINED': True,
  1146. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1147. ['gdaldem',
  1148. 'hillshade ' +
  1149. source + ' ' +
  1150. outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -combined'])
  1151. # with multidirectional - "az" argument is not allowed!
  1152. self.assertEqual(
  1153. alg.getConsoleCommands({'INPUT': source,
  1154. 'BAND': 1,
  1155. 'Z_FACTOR': 5,
  1156. 'SCALE': 2,
  1157. 'AZIMUTH': 90,
  1158. 'ALTITUDE': 20,
  1159. 'MULTIDIRECTIONAL': True,
  1160. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1161. ['gdaldem',
  1162. 'hillshade ' +
  1163. source + ' ' +
  1164. outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -alt 20.0 -multidirectional'])
  1165. # defaults with additional parameters
  1166. self.assertEqual(
  1167. alg.getConsoleCommands({'INPUT': source,
  1168. 'BAND': 1,
  1169. 'EXTRA': '-q',
  1170. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1171. ['gdaldem',
  1172. 'hillshade ' +
  1173. source + ' ' +
  1174. outdir + '/check.tif -of GTiff -b 1 -z 1.0 -s 1.0 -az 315.0 -alt 45.0 -q'])
  1175. # multidirectional and combined are mutually exclusive
  1176. self.assertRaises(
  1177. QgsProcessingException,
  1178. lambda: alg.getConsoleCommands({'INPUT': source,
  1179. 'BAND': 1,
  1180. 'Z_FACTOR': 5,
  1181. 'SCALE': 2,
  1182. 'AZIMUTH': 90,
  1183. 'COMBINED': True,
  1184. 'MULTIDIRECTIONAL': True,
  1185. 'OUTPUT': outdir + '/check.tif'}, context, feedback))
  1186. def testAspect(self):
  1187. context = QgsProcessingContext()
  1188. feedback = QgsProcessingFeedback()
  1189. source = os.path.join(testDataPath, 'dem.tif')
  1190. alg = aspect()
  1191. alg.initAlgorithm()
  1192. with tempfile.TemporaryDirectory() as outdir:
  1193. self.assertEqual(
  1194. alg.getConsoleCommands({'INPUT': source,
  1195. 'BAND': 1,
  1196. 'TRIG_ANGLE': False,
  1197. 'ZERO_FLAT': False,
  1198. 'COMPUTE_EDGES': False,
  1199. 'ZEVENBERGEN': False,
  1200. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1201. ['gdaldem',
  1202. 'aspect ' +
  1203. source + ' ' +
  1204. outdir + '/check.tif -of GTiff -b 1'])
  1205. # paths with space
  1206. source_with_space = os.path.join(testDataPath, 'raster with spaces.tif')
  1207. self.assertEqual(
  1208. alg.getConsoleCommands({'INPUT': source_with_space,
  1209. 'BAND': 1,
  1210. 'TRIG_ANGLE': False,
  1211. 'ZERO_FLAT': False,
  1212. 'COMPUTE_EDGES': False,
  1213. 'ZEVENBERGEN': False,
  1214. 'OUTPUT': outdir + '/check out.tif'}, context, feedback),
  1215. ['gdaldem',
  1216. 'aspect ' +
  1217. '"' + source_with_space + '" ' +
  1218. f'"{outdir}/check out.tif" -of GTiff -b 1'])
  1219. # compute edges
  1220. self.assertEqual(
  1221. alg.getConsoleCommands({'INPUT': source,
  1222. 'BAND': 1,
  1223. 'TRIG_ANGLE': False,
  1224. 'ZERO_FLAT': False,
  1225. 'COMPUTE_EDGES': True,
  1226. 'ZEVENBERGEN': False,
  1227. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1228. ['gdaldem',
  1229. 'aspect ' +
  1230. source + ' ' +
  1231. outdir + '/check.tif -of GTiff -b 1 -compute_edges'])
  1232. # with ZEVENBERGEN
  1233. self.assertEqual(
  1234. alg.getConsoleCommands({'INPUT': source,
  1235. 'BAND': 1,
  1236. 'TRIG_ANGLE': False,
  1237. 'ZERO_FLAT': False,
  1238. 'COMPUTE_EDGES': False,
  1239. 'ZEVENBERGEN': True,
  1240. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1241. ['gdaldem',
  1242. 'aspect ' +
  1243. source + ' ' +
  1244. outdir + '/check.tif -of GTiff -b 1 -alg ZevenbergenThorne'])
  1245. # with ZERO_FLAT
  1246. self.assertEqual(
  1247. alg.getConsoleCommands({'INPUT': source,
  1248. 'BAND': 1,
  1249. 'TRIG_ANGLE': False,
  1250. 'ZERO_FLAT': True,
  1251. 'COMPUTE_EDGES': False,
  1252. 'ZEVENBERGEN': False,
  1253. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1254. ['gdaldem',
  1255. 'aspect ' +
  1256. source + ' ' +
  1257. outdir + '/check.tif -of GTiff -b 1 -zero_for_flat'])
  1258. # with TRIG_ANGLE
  1259. self.assertEqual(
  1260. alg.getConsoleCommands({'INPUT': source,
  1261. 'BAND': 1,
  1262. 'TRIG_ANGLE': True,
  1263. 'ZERO_FLAT': False,
  1264. 'COMPUTE_EDGES': False,
  1265. 'ZEVENBERGEN': False,
  1266. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1267. ['gdaldem',
  1268. 'aspect ' +
  1269. source + ' ' +
  1270. outdir + '/check.tif -of GTiff -b 1 -trigonometric'])
  1271. # with creation options
  1272. self.assertEqual(
  1273. alg.getConsoleCommands({'INPUT': source,
  1274. 'BAND': 1,
  1275. 'TRIG_ANGLE': False,
  1276. 'ZERO_FLAT': False,
  1277. 'COMPUTE_EDGES': False,
  1278. 'ZEVENBERGEN': False,
  1279. 'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
  1280. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1281. ['gdaldem',
  1282. 'aspect ' +
  1283. source + ' ' +
  1284. outdir + '/check.tif -of GTiff -b 1 -co COMPRESS=JPEG -co JPEG_QUALITY=75'])
  1285. # with additional parameter
  1286. self.assertEqual(
  1287. alg.getConsoleCommands({'INPUT': source,
  1288. 'BAND': 1,
  1289. 'TRIG_ANGLE': False,
  1290. 'ZERO_FLAT': False,
  1291. 'COMPUTE_EDGES': False,
  1292. 'ZEVENBERGEN': False,
  1293. 'EXTRA': '-q',
  1294. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1295. ['gdaldem',
  1296. 'aspect ' +
  1297. source + ' ' +
  1298. outdir + '/check.tif -of GTiff -b 1 -q'])
  1299. def testSlope(self):
  1300. context = QgsProcessingContext()
  1301. feedback = QgsProcessingFeedback()
  1302. source = os.path.join(testDataPath, 'dem.tif')
  1303. alg = slope()
  1304. alg.initAlgorithm()
  1305. with tempfile.TemporaryDirectory() as outdir:
  1306. self.assertEqual(
  1307. alg.getConsoleCommands({'INPUT': source,
  1308. 'BAND': 1,
  1309. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1310. ['gdaldem',
  1311. 'slope ' +
  1312. source + ' ' +
  1313. outdir + '/check.tif -of GTiff -b 1 -s 1.0'])
  1314. # compute edges
  1315. self.assertEqual(
  1316. alg.getConsoleCommands({'INPUT': source,
  1317. 'BAND': 1,
  1318. 'COMPUTE_EDGES': True,
  1319. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1320. ['gdaldem',
  1321. 'slope ' +
  1322. source + ' ' +
  1323. outdir + '/check.tif -of GTiff -b 1 -s 1.0 -compute_edges'])
  1324. # with ZEVENBERGEN
  1325. self.assertEqual(
  1326. alg.getConsoleCommands({'INPUT': source,
  1327. 'BAND': 1,
  1328. 'ZEVENBERGEN': True,
  1329. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1330. ['gdaldem',
  1331. 'slope ' +
  1332. source + ' ' +
  1333. outdir + '/check.tif -of GTiff -b 1 -s 1.0 -alg ZevenbergenThorne'])
  1334. # custom ratio
  1335. self.assertEqual(
  1336. alg.getConsoleCommands({'INPUT': source,
  1337. 'BAND': 1,
  1338. 'SCALE': 2.0,
  1339. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1340. ['gdaldem',
  1341. 'slope ' +
  1342. source + ' ' +
  1343. outdir + '/check.tif -of GTiff -b 1 -s 2.0'])
  1344. # with creation options
  1345. self.assertEqual(
  1346. alg.getConsoleCommands({'INPUT': source,
  1347. 'BAND': 1,
  1348. 'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
  1349. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1350. ['gdaldem',
  1351. 'slope ' +
  1352. source + ' ' +
  1353. outdir + '/check.tif -of GTiff -b 1 -s 1.0 -co COMPRESS=JPEG -co JPEG_QUALITY=75'])
  1354. # with additional parameter
  1355. self.assertEqual(
  1356. alg.getConsoleCommands({'INPUT': source,
  1357. 'BAND': 1,
  1358. 'EXTRA': '-q',
  1359. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1360. ['gdaldem',
  1361. 'slope ' +
  1362. source + ' ' +
  1363. outdir + '/check.jpg -of JPEG -b 1 -s 1.0 -q'])
  1364. def testColorRelief(self):
  1365. context = QgsProcessingContext()
  1366. feedback = QgsProcessingFeedback()
  1367. source = os.path.join(testDataPath, 'dem.tif')
  1368. colorTable = os.path.join(testDataPath, 'colors.txt')
  1369. alg = ColorRelief()
  1370. alg.initAlgorithm()
  1371. with tempfile.TemporaryDirectory() as outdir:
  1372. self.assertEqual(
  1373. alg.getConsoleCommands({'INPUT': source,
  1374. 'BAND': 1,
  1375. 'COLOR_TABLE': colorTable,
  1376. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1377. ['gdaldem',
  1378. 'color-relief ' +
  1379. source + ' ' +
  1380. colorTable + ' ' +
  1381. outdir + '/check.tif -of GTiff -b 1'])
  1382. # paths with space
  1383. source_with_space = os.path.join(testDataPath, 'raster with spaces.tif')
  1384. self.assertEqual(
  1385. alg.getConsoleCommands({'INPUT': source_with_space,
  1386. 'BAND': 1,
  1387. 'COLOR_TABLE': colorTable,
  1388. 'OUTPUT': outdir + '/check out.tif'}, context, feedback),
  1389. ['gdaldem',
  1390. 'color-relief ' +
  1391. '"' + source_with_space + '" ' +
  1392. colorTable + ' ' +
  1393. f'"{outdir}/check out.tif" -of GTiff -b 1'])
  1394. # compute edges
  1395. self.assertEqual(
  1396. alg.getConsoleCommands({'INPUT': source,
  1397. 'BAND': 1,
  1398. 'COLOR_TABLE': colorTable,
  1399. 'COMPUTE_EDGES': True,
  1400. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1401. ['gdaldem',
  1402. 'color-relief ' +
  1403. source + ' ' +
  1404. colorTable + ' ' +
  1405. outdir + '/check.tif -of GTiff -b 1 -compute_edges'])
  1406. # with custom matching mode
  1407. self.assertEqual(
  1408. alg.getConsoleCommands({'INPUT': source,
  1409. 'BAND': 1,
  1410. 'COLOR_TABLE': colorTable,
  1411. 'MATCH_MODE': 1,
  1412. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1413. ['gdaldem',
  1414. 'color-relief ' +
  1415. source + ' ' +
  1416. colorTable + ' ' +
  1417. outdir + '/check.tif -of GTiff -b 1 -nearest_color_entry'])
  1418. # with creation options
  1419. self.assertEqual(
  1420. alg.getConsoleCommands({'INPUT': source,
  1421. 'BAND': 1,
  1422. 'COLOR_TABLE': colorTable,
  1423. 'MATCH_MODE': 1,
  1424. 'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
  1425. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1426. ['gdaldem',
  1427. 'color-relief ' +
  1428. source + ' ' +
  1429. colorTable + ' ' +
  1430. outdir + '/check.tif -of GTiff -b 1 -nearest_color_entry -co COMPRESS=JPEG -co JPEG_QUALITY=75'])
  1431. # with additional parameter
  1432. self.assertEqual(
  1433. alg.getConsoleCommands({'INPUT': source,
  1434. 'BAND': 1,
  1435. 'COLOR_TABLE': colorTable,
  1436. 'EXTRA': '-alpha -q',
  1437. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1438. ['gdaldem',
  1439. 'color-relief ' +
  1440. source + ' ' +
  1441. colorTable + ' ' +
  1442. outdir + '/check.tif -of GTiff -b 1 -alpha -q'])
  1443. def testProximity(self):
  1444. context = QgsProcessingContext()
  1445. feedback = QgsProcessingFeedback()
  1446. source = os.path.join(testDataPath, 'dem.tif')
  1447. alg = proximity()
  1448. alg.initAlgorithm()
  1449. with tempfile.TemporaryDirectory() as outdir:
  1450. # without NODATA value
  1451. self.assertEqual(
  1452. alg.getConsoleCommands({'INPUT': source,
  1453. 'BAND': 1,
  1454. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1455. ['gdal_proximity.py',
  1456. '-srcband 1 -distunits PIXEL -ot Float32 -of JPEG ' +
  1457. source + ' ' +
  1458. outdir + '/check.jpg'])
  1459. # with NODATA value
  1460. self.assertEqual(
  1461. alg.getConsoleCommands({'INPUT': source,
  1462. 'NODATA': 9999,
  1463. 'BAND': 2,
  1464. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1465. ['gdal_proximity.py',
  1466. '-srcband 2 -distunits PIXEL -nodata 9999.0 -ot Float32 -of JPEG ' +
  1467. source + ' ' +
  1468. outdir + '/check.jpg'])
  1469. # with "0" NODATA value
  1470. self.assertEqual(
  1471. alg.getConsoleCommands({'INPUT': source,
  1472. 'NODATA': 0,
  1473. 'BAND': 1,
  1474. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1475. ['gdal_proximity.py',
  1476. '-srcband 1 -distunits PIXEL -nodata 0.0 -ot Float32 -of JPEG ' +
  1477. source + ' ' +
  1478. outdir + '/check.jpg'])
  1479. # additional parameters
  1480. self.assertEqual(
  1481. alg.getConsoleCommands({'INPUT': source,
  1482. 'BAND': 1,
  1483. 'EXTRA': '-dstband 2 -values 3,4,12',
  1484. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1485. ['gdal_proximity.py',
  1486. '-srcband 1 -distunits PIXEL -ot Float32 -of JPEG -dstband 2 -values 3,4,12 ' +
  1487. source + ' ' +
  1488. outdir + '/check.jpg'])
  1489. def testRasterize(self):
  1490. context = QgsProcessingContext()
  1491. feedback = QgsProcessingFeedback()
  1492. source = os.path.join(testDataPath, 'polys.gml')
  1493. sourceZ = os.path.join(testDataPath, 'pointsz.gml')
  1494. extent4326 = QgsReferencedRectangle(QgsRectangle(-1, -3, 10, 6), QgsCoordinateReferenceSystem('EPSG:4326'))
  1495. extent3857 = QgsReferencedRectangle(QgsRectangle(-111319.491, -334111.171, 1113194.908, 669141.057), QgsCoordinateReferenceSystem('EPSG:3857'))
  1496. alg = rasterize()
  1497. alg.initAlgorithm()
  1498. with tempfile.TemporaryDirectory() as outdir:
  1499. # with no NODATA value
  1500. self.assertEqual(
  1501. alg.getConsoleCommands({'INPUT': source,
  1502. 'FIELD': 'id',
  1503. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1504. ['gdal_rasterize',
  1505. '-l polys2 -a id -ts 0.0 0.0 -ot Float32 -of JPEG ' +
  1506. source + ' ' +
  1507. outdir + '/check.jpg'])
  1508. # with NODATA value
  1509. self.assertEqual(
  1510. alg.getConsoleCommands({'INPUT': source,
  1511. 'NODATA': 9999,
  1512. 'FIELD': 'id',
  1513. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1514. ['gdal_rasterize',
  1515. '-l polys2 -a id -ts 0.0 0.0 -a_nodata 9999.0 -ot Float32 -of JPEG ' +
  1516. source + ' ' +
  1517. outdir + '/check.jpg'])
  1518. # with "0" INIT value
  1519. self.assertEqual(
  1520. alg.getConsoleCommands({'INPUT': source,
  1521. 'INIT': 0,
  1522. 'FIELD': 'id',
  1523. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1524. ['gdal_rasterize',
  1525. '-l polys2 -a id -ts 0.0 0.0 -init 0.0 -ot Float32 -of JPEG ' +
  1526. source + ' ' +
  1527. outdir + '/check.jpg'])
  1528. # with "0" NODATA value
  1529. self.assertEqual(
  1530. alg.getConsoleCommands({'INPUT': source,
  1531. 'NODATA': 0,
  1532. 'FIELD': 'id',
  1533. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1534. ['gdal_rasterize',
  1535. '-l polys2 -a id -ts 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' +
  1536. source + ' ' +
  1537. outdir + '/check.jpg'])
  1538. self.assertEqual(
  1539. alg.getConsoleCommands({'INPUT': source,
  1540. 'FIELD': 'id',
  1541. 'EXTRA': '-at -add',
  1542. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1543. ['gdal_rasterize',
  1544. '-l polys2 -a id -ts 0.0 0.0 -ot Float32 -of JPEG -at -add ' +
  1545. source + ' ' +
  1546. outdir + '/check.jpg'])
  1547. # use_Z selected with no field
  1548. self.assertEqual(
  1549. alg.getConsoleCommands({'INPUT': sourceZ,
  1550. 'USE_Z': True,
  1551. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1552. ['gdal_rasterize',
  1553. '-l pointsz -3d -ts 0.0 0.0 -ot Float32 -of JPEG ' +
  1554. sourceZ + ' ' +
  1555. outdir + '/check.jpg'])
  1556. # use_Z selected with field indicated (should prefer use_Z)
  1557. self.assertEqual(
  1558. alg.getConsoleCommands({'INPUT': sourceZ,
  1559. 'FIELD': 'elev',
  1560. 'USE_Z': True,
  1561. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1562. ['gdal_rasterize',
  1563. '-l pointsz -3d -ts 0.0 0.0 -ot Float32 -of JPEG ' +
  1564. sourceZ + ' ' +
  1565. outdir + '/check.jpg'])
  1566. # with EXTENT in the same CRS as the input layer source
  1567. self.assertEqual(
  1568. alg.getConsoleCommands({'INPUT': source,
  1569. 'FIELD': 'id',
  1570. 'EXTENT': extent4326,
  1571. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1572. ['gdal_rasterize',
  1573. '-l polys2 -a id -ts 0.0 0.0 -te -1.0 -3.0 10.0 6.0 -ot Float32 -of JPEG ' +
  1574. source + ' ' +
  1575. outdir + '/check.jpg'])
  1576. # with EXTENT in a different CRS than that of the input layer source
  1577. self.assertEqual(
  1578. alg.getConsoleCommands({'INPUT': source,
  1579. 'FIELD': 'id',
  1580. 'EXTENT': extent3857,
  1581. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1582. ['gdal_rasterize',
  1583. '-l polys2 -a id -ts 0.0 0.0 -te -1.000000001857055 -2.9999999963940835 10.000000000604244 5.99999999960471 -ot Float32 -of JPEG ' +
  1584. source + ' ' +
  1585. outdir + '/check.jpg'])
  1586. def testRasterizeOver(self):
  1587. context = QgsProcessingContext()
  1588. feedback = QgsProcessingFeedback()
  1589. raster = os.path.join(testDataPath, 'dem.tif')
  1590. vector = os.path.join(testDataPath, 'polys.gml')
  1591. alg = rasterize_over()
  1592. alg.initAlgorithm()
  1593. with tempfile.TemporaryDirectory() as outdir:
  1594. self.assertEqual(
  1595. alg.getConsoleCommands({'INPUT': vector,
  1596. 'FIELD': 'id',
  1597. 'INPUT_RASTER': raster}, context, feedback),
  1598. ['gdal_rasterize',
  1599. '-l polys2 -a id ' +
  1600. vector + ' ' + raster])
  1601. self.assertEqual(
  1602. alg.getConsoleCommands({'INPUT': vector,
  1603. 'FIELD': 'id',
  1604. 'ADD': True,
  1605. 'INPUT_RASTER': raster}, context, feedback),
  1606. ['gdal_rasterize',
  1607. '-l polys2 -a id -add ' +
  1608. vector + ' ' + raster])
  1609. self.assertEqual(
  1610. alg.getConsoleCommands({'INPUT': vector,
  1611. 'FIELD': 'id',
  1612. 'EXTRA': '-i',
  1613. 'INPUT_RASTER': raster}, context, feedback),
  1614. ['gdal_rasterize',
  1615. '-l polys2 -a id -i ' +
  1616. vector + ' ' + raster])
  1617. def testRasterizeOverFixed(self):
  1618. context = QgsProcessingContext()
  1619. feedback = QgsProcessingFeedback()
  1620. raster = os.path.join(testDataPath, 'dem.tif')
  1621. vector = os.path.join(testDataPath, 'polys.gml')
  1622. alg = rasterize_over_fixed_value()
  1623. alg.initAlgorithm()
  1624. with tempfile.TemporaryDirectory() as outdir:
  1625. self.assertEqual(
  1626. alg.getConsoleCommands({'INPUT': vector,
  1627. 'BURN': 100,
  1628. 'INPUT_RASTER': raster}, context, feedback),
  1629. ['gdal_rasterize',
  1630. '-l polys2 -burn 100.0 ' +
  1631. vector + ' ' + raster])
  1632. self.assertEqual(
  1633. alg.getConsoleCommands({'INPUT': vector,
  1634. 'BURN': 100,
  1635. 'ADD': True,
  1636. 'INPUT_RASTER': raster}, context, feedback),
  1637. ['gdal_rasterize',
  1638. '-l polys2 -burn 100.0 -add ' +
  1639. vector + ' ' + raster])
  1640. self.assertEqual(
  1641. alg.getConsoleCommands({'INPUT': vector,
  1642. 'BURN': 100,
  1643. 'EXTRA': '-i',
  1644. 'INPUT_RASTER': raster}, context, feedback),
  1645. ['gdal_rasterize',
  1646. '-l polys2 -burn 100.0 -i ' +
  1647. vector + ' ' + raster])
  1648. def testRasterizeOverRun(self):
  1649. # Check that rasterize over tools update QgsRasterLayer
  1650. context = QgsProcessingContext()
  1651. feedback = QgsProcessingFeedback()
  1652. source_vector = os.path.join(testDataPath, 'rasterize_zones.gml')
  1653. source_raster = os.path.join(testDataPath, 'dem.tif')
  1654. with tempfile.TemporaryDirectory() as outdir:
  1655. # fixed value
  1656. alg = rasterize_over_fixed_value()
  1657. alg.initAlgorithm()
  1658. test_dem = os.path.join(outdir, 'rasterize-fixed.tif')
  1659. shutil.copy(source_raster, test_dem)
  1660. self.assertTrue(os.path.exists(test_dem))
  1661. layer = QgsRasterLayer(test_dem, 'test')
  1662. self.assertTrue(layer.isValid())
  1663. val, ok = layer.dataProvider().sample(QgsPointXY(18.68704, 45.79568), 1)
  1664. self.assertEqual(val, 172.2267303466797)
  1665. project = QgsProject()
  1666. project.setFileName(os.path.join(outdir, 'rasterize-fixed.qgs'))
  1667. project.addMapLayer(layer)
  1668. self.assertEqual(project.count(), 1)
  1669. context.setProject(project)
  1670. alg.run({'INPUT': source_vector,
  1671. 'INPUT_RASTER': test_dem,
  1672. 'BURN': 200
  1673. }, context, feedback)
  1674. val, ok = layer.dataProvider().sample(QgsPointXY(18.68704, 45.79568), 1)
  1675. self.assertTrue(ok)
  1676. self.assertEqual(val, 200.0)
  1677. # attribute value
  1678. alg = rasterize_over()
  1679. alg.initAlgorithm()
  1680. test_dem = os.path.join(outdir, 'rasterize-over.tif')
  1681. shutil.copy(source_raster, test_dem)
  1682. self.assertTrue(os.path.exists(test_dem))
  1683. layer = QgsRasterLayer(test_dem, 'test')
  1684. self.assertTrue(layer.isValid())
  1685. val, ok = layer.dataProvider().sample(QgsPointXY(18.68704, 45.79568), 1)
  1686. self.assertEqual(val, 172.2267303466797)
  1687. project = QgsProject()
  1688. project.setFileName(os.path.join(outdir, 'rasterize-over.qgs'))
  1689. project.addMapLayer(layer)
  1690. self.assertEqual(project.count(), 1)
  1691. context.setProject(project)
  1692. alg.run({'INPUT': source_vector,
  1693. 'INPUT_RASTER': test_dem,
  1694. 'FIELD': 'value'
  1695. }, context, feedback)
  1696. val, ok = layer.dataProvider().sample(QgsPointXY(18.68704, 45.79568), 1)
  1697. self.assertTrue(ok)
  1698. self.assertEqual(val, 100.0)
  1699. def testRetile(self):
  1700. context = QgsProcessingContext()
  1701. feedback = QgsProcessingFeedback()
  1702. source = os.path.join(testDataPath, 'dem.tif')
  1703. alg = retile()
  1704. alg.initAlgorithm()
  1705. with tempfile.TemporaryDirectory() as outdir:
  1706. self.assertEqual(
  1707. alg.getConsoleCommands({'INPUT': [source],
  1708. 'OUTPUT': outdir}, context, feedback),
  1709. ['gdal_retile.py',
  1710. f'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -targetDir {outdir} ' +
  1711. source])
  1712. # with input srs
  1713. self.assertEqual(
  1714. alg.getConsoleCommands({'INPUT': [source],
  1715. 'SOURCE_CRS': 'EPSG:3111',
  1716. 'OUTPUT': outdir}, context, feedback),
  1717. ['gdal_retile.py',
  1718. f'-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir {outdir} {source}'
  1719. ])
  1720. # with target using proj string
  1721. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  1722. self.assertEqual(
  1723. alg.getConsoleCommands({'INPUT': [source],
  1724. 'SOURCE_CRS': custom_crs,
  1725. 'OUTPUT': outdir}, context, feedback),
  1726. ['gdal_retile.py',
  1727. f'-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:20936 -r near -ot Float32 -targetDir {outdir} {source}'
  1728. ])
  1729. # with target using custom projection
  1730. custom_crs, expected_crs_string = self.get_param_value_and_expected_string_for_custom_crs('+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
  1731. self.assertEqual(
  1732. alg.getConsoleCommands({'INPUT': [source],
  1733. 'SOURCE_CRS': custom_crs,
  1734. 'OUTPUT': outdir}, context, feedback),
  1735. ['gdal_retile.py',
  1736. f'-ps 256 256 -overlap 0 -levels 1 -s_srs "{expected_crs_string}" -r near -ot Float32 -targetDir {outdir} {source}'
  1737. ])
  1738. # with non-EPSG crs code
  1739. self.assertEqual(
  1740. alg.getConsoleCommands({'INPUT': [source],
  1741. 'SOURCE_CRS': 'POSTGIS:3111',
  1742. 'OUTPUT': outdir}, context, feedback),
  1743. ['gdal_retile.py',
  1744. f'-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir {outdir} {source}'
  1745. ])
  1746. self.assertEqual(
  1747. alg.getConsoleCommands({'INPUT': [source],
  1748. 'OUTPUT_CSV': 'out.csv',
  1749. 'DELIMITER': '',
  1750. 'OUTPUT': outdir}, context, feedback),
  1751. ['gdal_retile.py',
  1752. f'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -csv out.csv -targetDir {outdir} ' +
  1753. source])
  1754. self.assertEqual(
  1755. alg.getConsoleCommands({'INPUT': [source],
  1756. 'OUTPUT_CSV': 'out.csv',
  1757. 'DELIMITER': ';',
  1758. 'OUTPUT': outdir}, context, feedback),
  1759. ['gdal_retile.py',
  1760. f'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -csv out.csv -csvDelim ";" -targetDir {outdir} ' +
  1761. source])
  1762. # additional parameters
  1763. self.assertEqual(
  1764. alg.getConsoleCommands({'INPUT': [source],
  1765. 'EXTRA': '-v -tileIndex tindex.shp',
  1766. 'OUTPUT': outdir}, context, feedback),
  1767. ['gdal_retile.py',
  1768. f'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -v -tileIndex tindex.shp -targetDir {outdir} ' +
  1769. source])
  1770. self.assertEqual(
  1771. alg.getConsoleCommands({'INPUT': [source],
  1772. 'ONLY_PYRAMIDS': True,
  1773. 'OUTPUT': outdir}, context, feedback),
  1774. ['gdal_retile.py',
  1775. f'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -pyramidOnly -targetDir {outdir} ' +
  1776. source])
  1777. self.assertEqual(
  1778. alg.getConsoleCommands({'INPUT': [source],
  1779. 'DIR_FOR_ROW': True,
  1780. 'OUTPUT': outdir}, context, feedback),
  1781. ['gdal_retile.py',
  1782. f'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -useDirForEachRow -targetDir {outdir} ' +
  1783. source])
  1784. def testWarp(self):
  1785. context = QgsProcessingContext()
  1786. feedback = QgsProcessingFeedback()
  1787. source = os.path.join(testDataPath, 'dem.tif')
  1788. alg = warp()
  1789. alg.initAlgorithm()
  1790. with tempfile.TemporaryDirectory() as outdir:
  1791. # with no NODATA value
  1792. self.assertEqual(
  1793. alg.getConsoleCommands({'INPUT': source,
  1794. 'SOURCE_CRS': 'EPSG:3111',
  1795. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1796. ['gdalwarp',
  1797. '-overwrite -s_srs EPSG:3111 -r near -of JPEG ' +
  1798. source + ' ' +
  1799. outdir + '/check.jpg'])
  1800. # with None NODATA value
  1801. self.assertEqual(
  1802. alg.getConsoleCommands({'INPUT': source,
  1803. 'NODATA': None,
  1804. 'SOURCE_CRS': 'EPSG:3111',
  1805. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1806. ['gdalwarp',
  1807. '-overwrite -s_srs EPSG:3111 -r near -of JPEG ' +
  1808. source + ' ' +
  1809. outdir + '/check.jpg'])
  1810. # with NODATA value
  1811. self.assertEqual(
  1812. alg.getConsoleCommands({'INPUT': source,
  1813. 'NODATA': 9999,
  1814. 'SOURCE_CRS': 'EPSG:3111',
  1815. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1816. ['gdalwarp',
  1817. '-overwrite -s_srs EPSG:3111 -dstnodata 9999.0 -r near -of JPEG ' +
  1818. source + ' ' +
  1819. outdir + '/check.jpg'])
  1820. # with "0" NODATA value
  1821. self.assertEqual(
  1822. alg.getConsoleCommands({'INPUT': source,
  1823. 'NODATA': 0,
  1824. 'SOURCE_CRS': 'EPSG:3111',
  1825. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1826. ['gdalwarp',
  1827. '-overwrite -s_srs EPSG:3111 -dstnodata 0.0 -r near -of JPEG ' +
  1828. source + ' ' +
  1829. outdir + '/check.jpg'])
  1830. # with "0" NODATA value and custom data type
  1831. self.assertEqual(
  1832. alg.getConsoleCommands({'INPUT': source,
  1833. 'NODATA': 0,
  1834. 'DATA_TYPE': 6,
  1835. 'SOURCE_CRS': 'EPSG:3111',
  1836. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1837. ['gdalwarp',
  1838. '-overwrite -s_srs EPSG:3111 -dstnodata 0.0 -r near -ot Float32 -of JPEG ' +
  1839. source + ' ' +
  1840. outdir + '/check.jpg'])
  1841. # with target using EPSG
  1842. self.assertEqual(
  1843. alg.getConsoleCommands({'INPUT': source,
  1844. 'SOURCE_CRS': 'EPSG:3111',
  1845. 'TARGET_CRS': 'EPSG:4326',
  1846. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1847. ['gdalwarp',
  1848. '-overwrite -s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' +
  1849. source + ' ' +
  1850. outdir + '/check.jpg'])
  1851. # with target using proj string
  1852. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  1853. self.assertEqual(
  1854. alg.getConsoleCommands({'INPUT': source,
  1855. 'SOURCE_CRS': custom_crs,
  1856. 'TARGET_CRS': custom_crs,
  1857. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1858. ['gdalwarp',
  1859. '-overwrite -s_srs EPSG:20936 -t_srs EPSG:20936 -r near -of JPEG ' +
  1860. source + ' ' +
  1861. outdir + '/check.jpg'])
  1862. # with target using custom projection
  1863. custom_crs, expected_crs_string = self.get_param_value_and_expected_string_for_custom_crs('+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
  1864. self.assertEqual(
  1865. alg.getConsoleCommands({'INPUT': source,
  1866. 'SOURCE_CRS': custom_crs,
  1867. 'TARGET_CRS': custom_crs,
  1868. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1869. ['gdalwarp',
  1870. f'-overwrite -s_srs "{expected_crs_string}" -t_srs "{expected_crs_string}" -r near -of JPEG ' +
  1871. source + ' ' +
  1872. outdir + '/check.jpg'])
  1873. # with target using custom projection and user-defined extent
  1874. custom_crs2, expected_crs_string2 = self.get_param_value_and_expected_string_for_custom_crs('+proj=longlat +a=6378388 +b=6356912 +no_defs')
  1875. self.assertEqual(
  1876. alg.getConsoleCommands({'INPUT': source,
  1877. 'SOURCE_CRS': custom_crs2,
  1878. 'TARGET_CRS': custom_crs2,
  1879. 'TARGET_EXTENT': '18.67,18.70,45.78,45.81',
  1880. 'TARGET_EXTENT_CRS': custom_crs2,
  1881. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  1882. ['gdalwarp',
  1883. f'-overwrite -s_srs "{expected_crs_string2}" -t_srs "{expected_crs_string2}" -r near -te 18.67 45.78 18.7 45.81 -te_srs "{expected_crs_string2}" -of GTiff ' +
  1884. source + ' ' +
  1885. outdir + '/check.tif'])
  1886. # with non-EPSG crs code
  1887. self.assertEqual(
  1888. alg.getConsoleCommands({'INPUT': source,
  1889. 'SOURCE_CRS': 'POSTGIS:3111',
  1890. 'TARGET_CRS': 'POSTGIS:3111',
  1891. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1892. ['gdalwarp',
  1893. '-overwrite -s_srs EPSG:3111 -t_srs EPSG:3111 -r near -of JPEG ' +
  1894. source + ' ' +
  1895. outdir + '/check.jpg'])
  1896. # with target resolution with None value
  1897. self.assertEqual(
  1898. alg.getConsoleCommands({'INPUT': source,
  1899. 'SOURCE_CRS': 'EPSG:3111',
  1900. 'TARGET_RESOLUTION': None,
  1901. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1902. ['gdalwarp',
  1903. '-overwrite -s_srs EPSG:3111 -r near -of JPEG ' +
  1904. source + ' ' +
  1905. outdir + '/check.jpg'])
  1906. # test target resolution with a valid value
  1907. self.assertEqual(
  1908. alg.getConsoleCommands({'INPUT': source,
  1909. 'SOURCE_CRS': 'EPSG:3111',
  1910. 'TARGET_RESOLUTION': 10.0,
  1911. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1912. ['gdalwarp',
  1913. '-overwrite -s_srs EPSG:3111 -tr 10.0 10.0 -r near -of JPEG ' +
  1914. source + ' ' +
  1915. outdir + '/check.jpg'])
  1916. # test target resolution with a value of zero, to be ignored
  1917. self.assertEqual(
  1918. alg.getConsoleCommands({'INPUT': source,
  1919. 'SOURCE_CRS': 'EPSG:3111',
  1920. 'TARGET_RESOLUTION': 0.0,
  1921. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1922. ['gdalwarp',
  1923. '-overwrite -s_srs EPSG:3111 -r near -of JPEG ' +
  1924. source + ' ' +
  1925. outdir + '/check.jpg'])
  1926. # with additional command-line parameter
  1927. self.assertEqual(
  1928. alg.getConsoleCommands({'INPUT': source,
  1929. 'EXTRA': '-dstalpha',
  1930. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1931. ['gdalwarp',
  1932. '-overwrite -r near -of JPEG -dstalpha ' +
  1933. source + ' ' +
  1934. outdir + '/check.jpg'])
  1935. self.assertEqual(
  1936. alg.getConsoleCommands({'INPUT': source,
  1937. 'EXTRA': '-dstalpha -srcnodata -9999',
  1938. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1939. ['gdalwarp',
  1940. '-overwrite -r near -of JPEG -dstalpha -srcnodata -9999 ' +
  1941. source + ' ' +
  1942. outdir + '/check.jpg'])
  1943. self.assertEqual(
  1944. alg.getConsoleCommands({'INPUT': source,
  1945. 'EXTRA': '-dstalpha -srcnodata "-9999 -8888"',
  1946. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1947. ['gdalwarp',
  1948. '-overwrite -r near -of JPEG -dstalpha -srcnodata "-9999 -8888" ' +
  1949. source + ' ' +
  1950. outdir + '/check.jpg'])
  1951. self.assertEqual(
  1952. alg.getConsoleCommands({'INPUT': source,
  1953. 'EXTRA': '',
  1954. 'OUTPUT': outdir + '/check.jpg'}, context, feedback),
  1955. ['gdalwarp',
  1956. '-overwrite -r near -of JPEG ' +
  1957. source + ' ' +
  1958. outdir + '/check.jpg'])
  1959. def testMerge(self):
  1960. context = QgsProcessingContext()
  1961. feedback = QgsProcessingFeedback()
  1962. source = [os.path.join(testDataPath, 'dem1.tif'), os.path.join(testDataPath, 'dem1.tif')]
  1963. alg = merge()
  1964. alg.initAlgorithm()
  1965. with tempfile.TemporaryDirectory() as outdir:
  1966. # this algorithm creates temporary text file with input layers
  1967. # so we strip its path, leaving only filename
  1968. cmd = alg.getConsoleCommands({'INPUT': source,
  1969. 'OUTPUT': outdir + '/check.tif'}, context, feedback)
  1970. t = cmd[1]
  1971. cmd[1] = t[:t.find('--optfile') + 10] + t[t.find('mergeInputFiles.txt'):]
  1972. self.assertEqual(cmd,
  1973. ['gdal_merge.py',
  1974. '-ot Float32 -of GTiff ' +
  1975. '-o ' + outdir + '/check.tif ' +
  1976. '--optfile mergeInputFiles.txt'])
  1977. # separate
  1978. cmd = alg.getConsoleCommands({'INPUT': source,
  1979. 'SEPARATE': True,
  1980. 'OUTPUT': outdir + '/check.tif'}, context, feedback)
  1981. t = cmd[1]
  1982. cmd[1] = t[:t.find('--optfile') + 10] + t[t.find('mergeInputFiles.txt'):]
  1983. self.assertEqual(cmd,
  1984. ['gdal_merge.py',
  1985. '-separate -ot Float32 -of GTiff ' +
  1986. '-o ' + outdir + '/check.tif ' +
  1987. '--optfile mergeInputFiles.txt'])
  1988. # assign nodata
  1989. cmd = alg.getConsoleCommands({'INPUT': source,
  1990. 'EXTRA': '-tap -ps 0.1 0.1',
  1991. 'OUTPUT': outdir + '/check.tif'}, context, feedback)
  1992. t = cmd[1]
  1993. cmd[1] = t[:t.find('--optfile') + 10] + t[t.find('mergeInputFiles.txt'):]
  1994. self.assertEqual(cmd,
  1995. ['gdal_merge.py',
  1996. '-ot Float32 -of GTiff -tap -ps 0.1 0.1 ' +
  1997. '-o ' + outdir + '/check.tif ' +
  1998. '--optfile mergeInputFiles.txt'])
  1999. # additional parameters
  2000. cmd = alg.getConsoleCommands({'INPUT': source,
  2001. 'NODATA_OUTPUT': -9999,
  2002. 'OUTPUT': outdir + '/check.tif'}, context, feedback)
  2003. t = cmd[1]
  2004. cmd[1] = t[:t.find('--optfile') + 10] + t[t.find('mergeInputFiles.txt'):]
  2005. self.assertEqual(cmd,
  2006. ['gdal_merge.py',
  2007. '-a_nodata -9999.0 -ot Float32 -of GTiff ' +
  2008. '-o ' + outdir + '/check.tif ' +
  2009. '--optfile mergeInputFiles.txt'])
  2010. def testNearblack(self):
  2011. context = QgsProcessingContext()
  2012. feedback = QgsProcessingFeedback()
  2013. source = os.path.join(testDataPath, 'dem.tif')
  2014. alg = nearblack()
  2015. alg.initAlgorithm()
  2016. with tempfile.TemporaryDirectory() as outdir:
  2017. # defaults
  2018. self.assertEqual(
  2019. alg.getConsoleCommands({'INPUT': source,
  2020. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2021. ['nearblack',
  2022. source + ' -of GTiff -o ' + outdir + '/check.tif ' +
  2023. '-near 15'])
  2024. # search white pixels
  2025. self.assertEqual(
  2026. alg.getConsoleCommands({'INPUT': source,
  2027. 'WHITE': True,
  2028. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2029. ['nearblack',
  2030. source + ' -of GTiff -o ' + outdir + '/check.tif ' +
  2031. '-near 15 -white'])
  2032. # additional parameters
  2033. self.assertEqual(
  2034. alg.getConsoleCommands({'INPUT': source,
  2035. 'EXTRA': '-nb 5 -setalpha',
  2036. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2037. ['nearblack',
  2038. source + ' -of GTiff -o ' + outdir + '/check.tif ' +
  2039. '-near 15 -nb 5 -setalpha'])
  2040. # additional parameters and creation options
  2041. self.assertEqual(
  2042. alg.getConsoleCommands({'INPUT': source,
  2043. 'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
  2044. 'EXTRA': '-nb 5 -setalpha',
  2045. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2046. ['nearblack',
  2047. source + ' -of GTiff -o ' + outdir + '/check.tif ' +
  2048. '-near 15 -co COMPRESS=JPEG -co JPEG_QUALITY=75 -nb 5 -setalpha'])
  2049. def testRearrangeBands(self):
  2050. context = QgsProcessingContext()
  2051. feedback = QgsProcessingFeedback()
  2052. source = os.path.join(testDataPath, 'dem.tif')
  2053. with tempfile.TemporaryDirectory() as outdir:
  2054. outsource = outdir + '/check.tif'
  2055. alg = rearrange_bands()
  2056. alg.initAlgorithm()
  2057. # single band
  2058. self.assertEqual(
  2059. alg.getConsoleCommands({'INPUT': source,
  2060. 'BANDS': 1,
  2061. 'OUTPUT': outsource}, context, feedback),
  2062. ['gdal_translate', '-b 1 ' +
  2063. '-of GTiff ' +
  2064. source + ' ' + outsource])
  2065. # three bands, re-ordered
  2066. self.assertEqual(
  2067. alg.getConsoleCommands({'INPUT': source,
  2068. 'BANDS': [3, 2, 1],
  2069. 'OUTPUT': outsource}, context, feedback),
  2070. ['gdal_translate', '-b 3 -b 2 -b 1 ' +
  2071. '-of GTiff ' +
  2072. source + ' ' + outsource])
  2073. # three bands, re-ordered with custom data type
  2074. self.assertEqual(
  2075. alg.getConsoleCommands({'INPUT': source,
  2076. 'BANDS': [3, 2, 1],
  2077. 'DATA_TYPE': 6,
  2078. 'OUTPUT': outsource}, context, feedback),
  2079. ['gdal_translate', '-b 3 -b 2 -b 1 ' +
  2080. '-ot Float32 -of GTiff ' +
  2081. source + ' ' + outsource])
  2082. def testFillnodata(self):
  2083. context = QgsProcessingContext()
  2084. feedback = QgsProcessingFeedback()
  2085. source = os.path.join(testDataPath, 'dem.tif')
  2086. mask = os.path.join(testDataPath, 'raster.tif')
  2087. with tempfile.TemporaryDirectory() as outdir:
  2088. outsource = outdir + '/check.tif'
  2089. alg = fillnodata()
  2090. alg.initAlgorithm()
  2091. # with mask value
  2092. self.assertEqual(
  2093. alg.getConsoleCommands({'INPUT': source,
  2094. 'BAND': 1,
  2095. 'DISTANCE': 10,
  2096. 'ITERATIONS': 0,
  2097. 'MASK_LAYER': mask,
  2098. 'NO_MASK': False,
  2099. 'OUTPUT': outsource}, context, feedback),
  2100. ['gdal_fillnodata.py',
  2101. f'{source} {outsource} -md 10 -b 1 -mask {mask} -of GTiff'])
  2102. # without mask value
  2103. self.assertEqual(
  2104. alg.getConsoleCommands({'INPUT': source,
  2105. 'BAND': 1,
  2106. 'DISTANCE': 10,
  2107. 'ITERATIONS': 0,
  2108. 'NO_MASK': False,
  2109. 'OUTPUT': outsource}, context, feedback),
  2110. ['gdal_fillnodata.py',
  2111. f'{source} {outsource} -md 10 -b 1 -of GTiff'])
  2112. # The -nomask option is no longer supported since GDAL 3.4 and
  2113. # it doesn't work as expected even using GDAL < 3.4 https://github.com/OSGeo/gdal/pull/4201
  2114. # Silently ignore the NO_MASK parameter
  2115. self.assertEqual(
  2116. alg.getConsoleCommands({'INPUT': source,
  2117. 'BAND': 1,
  2118. 'DISTANCE': 10,
  2119. 'ITERATIONS': 0,
  2120. 'NO_MASK': True,
  2121. 'OUTPUT': outsource}, context, feedback),
  2122. ['gdal_fillnodata.py',
  2123. f'{source} {outsource} -md 10 -b 1 -of GTiff'])
  2124. # creation options
  2125. self.assertEqual(
  2126. alg.getConsoleCommands({'INPUT': source,
  2127. 'BAND': 1,
  2128. 'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
  2129. 'OUTPUT': outsource}, context, feedback),
  2130. ['gdal_fillnodata.py',
  2131. f'{source} {outsource} -md 10 -b 1 -of GTiff -co COMPRESS=JPEG -co JPEG_QUALITY=75'])
  2132. # additional parameters
  2133. self.assertEqual(
  2134. alg.getConsoleCommands({'INPUT': source,
  2135. 'BAND': 1,
  2136. 'EXTRA': '-q',
  2137. 'OUTPUT': outsource}, context, feedback),
  2138. ['gdal_fillnodata.py',
  2139. f'{source} {outsource} -md 10 -b 1 -of GTiff -q'])
  2140. def testGdalAddo(self):
  2141. context = QgsProcessingContext()
  2142. feedback = QgsProcessingFeedback()
  2143. source = os.path.join(testDataPath, 'dem.tif')
  2144. with tempfile.TemporaryDirectory() as outdir:
  2145. alg = gdaladdo()
  2146. alg.initAlgorithm()
  2147. # defaults
  2148. self.assertEqual(
  2149. alg.getConsoleCommands({'INPUT': source,
  2150. 'LEVELS': '2 4 8 16',
  2151. 'CLEAN': False,
  2152. 'RESAMPLING': 0,
  2153. 'FORMAT': 0}, context, feedback),
  2154. ['gdaladdo',
  2155. source + ' ' + '-r nearest 2 4 8 16'])
  2156. # with "clean" option
  2157. self.assertEqual(
  2158. alg.getConsoleCommands({'INPUT': source,
  2159. 'LEVELS': '2 4 8 16',
  2160. 'CLEAN': True,
  2161. 'RESAMPLING': 0,
  2162. 'FORMAT': 0}, context, feedback),
  2163. ['gdaladdo',
  2164. source + ' ' + '-r nearest -clean 2 4 8 16'])
  2165. # ovr format
  2166. self.assertEqual(
  2167. alg.getConsoleCommands({'INPUT': source,
  2168. 'LEVELS': '2 4 8 16',
  2169. 'CLEAN': False,
  2170. 'RESAMPLING': 0,
  2171. 'FORMAT': 1}, context, feedback),
  2172. ['gdaladdo',
  2173. source + ' ' + '-r nearest -ro 2 4 8 16'])
  2174. # Erdas format
  2175. self.assertEqual(
  2176. alg.getConsoleCommands({'INPUT': source,
  2177. 'LEVELS': '2 4 8 16',
  2178. 'CLEAN': False,
  2179. 'RESAMPLING': 0,
  2180. 'FORMAT': 2}, context, feedback),
  2181. ['gdaladdo',
  2182. source + ' ' + '-r nearest --config USE_RRD YES 2 4 8 16'])
  2183. # custom resampling method format
  2184. self.assertEqual(
  2185. alg.getConsoleCommands({'INPUT': source,
  2186. 'LEVELS': '2 4 8 16',
  2187. 'CLEAN': False,
  2188. 'RESAMPLING': 4,
  2189. 'FORMAT': 0}, context, feedback),
  2190. ['gdaladdo',
  2191. source + ' ' + '-r cubicspline 2 4 8 16'])
  2192. # more levels
  2193. self.assertEqual(
  2194. alg.getConsoleCommands({'INPUT': source,
  2195. 'LEVELS': '2 4 8 16 32 64',
  2196. 'CLEAN': False,
  2197. 'RESAMPLING': 0,
  2198. 'FORMAT': 0}, context, feedback),
  2199. ['gdaladdo',
  2200. source + ' ' + '-r nearest 2 4 8 16 32 64'])
  2201. # additional parameters
  2202. self.assertEqual(
  2203. alg.getConsoleCommands({'INPUT': source,
  2204. 'LEVELS': '2 4 8 16',
  2205. 'CLEAN': False,
  2206. 'EXTRA': '--config COMPRESS_OVERVIEW JPEG'}, context, feedback),
  2207. ['gdaladdo',
  2208. source + ' ' + '--config COMPRESS_OVERVIEW JPEG 2 4 8 16'])
  2209. if GdalUtils.version() >= 230000:
  2210. # without levels
  2211. self.assertEqual(
  2212. alg.getConsoleCommands({'INPUT': source,
  2213. 'CLEAN': False}, context, feedback),
  2214. ['gdaladdo',
  2215. source])
  2216. # without advanced params
  2217. self.assertEqual(
  2218. alg.getConsoleCommands({'INPUT': source,
  2219. 'LEVELS': '2 4 8 16',
  2220. 'CLEAN': False}, context, feedback),
  2221. ['gdaladdo',
  2222. source + ' ' + '2 4 8 16'])
  2223. def testSieve(self):
  2224. context = QgsProcessingContext()
  2225. feedback = QgsProcessingFeedback()
  2226. source = os.path.join(testDataPath, 'dem.tif')
  2227. mask = os.path.join(testDataPath, 'raster.tif')
  2228. with tempfile.TemporaryDirectory() as outdir:
  2229. outsource = outdir + '/check.tif'
  2230. alg = sieve()
  2231. alg.initAlgorithm()
  2232. # defaults
  2233. self.assertEqual(
  2234. alg.getConsoleCommands({'INPUT': source,
  2235. 'OUTPUT': outsource}, context, feedback),
  2236. ['gdal_sieve.py',
  2237. '-st 10 -4 -of GTiff ' +
  2238. source + ' ' +
  2239. outsource])
  2240. # Eight connectedness and custom threshold
  2241. self.assertEqual(
  2242. alg.getConsoleCommands({'INPUT': source,
  2243. 'THRESHOLD': 16,
  2244. 'EIGHT_CONNECTEDNESS': True,
  2245. 'OUTPUT': outsource}, context, feedback),
  2246. ['gdal_sieve.py',
  2247. '-st 16 -8 -of GTiff ' +
  2248. source + ' ' +
  2249. outsource])
  2250. # without default mask layer
  2251. self.assertEqual(
  2252. alg.getConsoleCommands({'INPUT': source,
  2253. 'NO_MASK': True,
  2254. 'OUTPUT': outsource}, context, feedback),
  2255. ['gdal_sieve.py',
  2256. '-st 10 -4 -nomask -of GTiff ' +
  2257. source + ' ' +
  2258. outsource])
  2259. # defaults with external validity mask
  2260. self.assertEqual(
  2261. alg.getConsoleCommands({'INPUT': source,
  2262. 'MASK_LAYER': mask,
  2263. 'OUTPUT': outsource}, context, feedback),
  2264. ['gdal_sieve.py',
  2265. '-st 10 -4 -mask ' +
  2266. mask +
  2267. ' -of GTiff ' +
  2268. source + ' ' +
  2269. outsource])
  2270. # additional parameters
  2271. self.assertEqual(
  2272. alg.getConsoleCommands({'INPUT': source,
  2273. 'EXTRA': '-q',
  2274. 'OUTPUT': outsource}, context, feedback),
  2275. ['gdal_sieve.py',
  2276. '-st 10 -4 -of GTiff -q ' +
  2277. source + ' ' +
  2278. outsource])
  2279. def testGdal2Xyz(self):
  2280. context = QgsProcessingContext()
  2281. feedback = QgsProcessingFeedback()
  2282. source = os.path.join(testDataPath, 'dem.tif')
  2283. with tempfile.TemporaryDirectory() as outdir:
  2284. outsource = outdir + '/check.csv'
  2285. alg = gdal2xyz()
  2286. alg.initAlgorithm()
  2287. # defaults
  2288. self.assertEqual(
  2289. alg.getConsoleCommands({'INPUT': source,
  2290. 'BAND': 1,
  2291. 'CSV': False,
  2292. 'OUTPUT': outsource}, context, feedback),
  2293. ['gdal2xyz.py',
  2294. '-band 1 ' +
  2295. source + ' ' +
  2296. outsource])
  2297. # csv output
  2298. self.assertEqual(
  2299. alg.getConsoleCommands({'INPUT': source,
  2300. 'BAND': 1,
  2301. 'CSV': True,
  2302. 'OUTPUT': outsource}, context, feedback),
  2303. ['gdal2xyz.py',
  2304. '-band 1 -csv ' +
  2305. source + ' ' +
  2306. outsource])
  2307. if GdalUtils.version() >= 3030000:
  2308. # skip nodata output
  2309. self.assertEqual(
  2310. alg.getConsoleCommands({'INPUT': source,
  2311. 'BAND': 1,
  2312. 'CSV': False,
  2313. 'SKIP_NODATA': True,
  2314. 'OUTPUT': outsource}, context, feedback),
  2315. ['gdal2xyz.py',
  2316. '-band 1 -skipnodata ' +
  2317. source + ' ' +
  2318. outsource])
  2319. if GdalUtils.version() > 3060300:
  2320. # srcnodata output
  2321. self.assertEqual(
  2322. alg.getConsoleCommands({'INPUT': source,
  2323. 'BAND': 1,
  2324. 'CSV': False,
  2325. 'NODATA_INPUT': -999,
  2326. 'SKIP_NODATA': False,
  2327. 'OUTPUT': outsource}, context, feedback),
  2328. ['gdal2xyz.py',
  2329. '-band 1 -srcnodata -999.0 ' +
  2330. source + ' ' +
  2331. outsource])
  2332. # dstnodata output
  2333. self.assertEqual(
  2334. alg.getConsoleCommands({'INPUT': source,
  2335. 'BAND': 1,
  2336. 'CSV': False,
  2337. 'NODATA_OUTPUT': -999,
  2338. 'SKIP_NODATA': False,
  2339. 'OUTPUT': outsource}, context, feedback),
  2340. ['gdal2xyz.py',
  2341. '-band 1 -dstnodata -999.0 ' +
  2342. source + ' ' +
  2343. outsource])
  2344. def testGdalPolygonize(self):
  2345. context = QgsProcessingContext()
  2346. feedback = QgsProcessingFeedback()
  2347. source = os.path.join(testDataPath, 'dem.tif')
  2348. with tempfile.TemporaryDirectory() as outdir:
  2349. outsource = outdir + '/check.shp'
  2350. alg = polygonize()
  2351. alg.initAlgorithm()
  2352. # defaults
  2353. self.assertEqual(
  2354. alg.getConsoleCommands({'INPUT': source,
  2355. 'BAND': 1,
  2356. 'FIELD': 'DN',
  2357. 'EIGHT_CONNECTEDNESS': False,
  2358. 'OUTPUT': outsource}, context, feedback),
  2359. ['gdal_polygonize.py',
  2360. source + ' ' +
  2361. '-b 1 -f "ESRI Shapefile"' + ' ' + outsource + ' ' + 'check DN'
  2362. ])
  2363. self.assertEqual(
  2364. alg.getConsoleCommands({'INPUT': source,
  2365. 'BAND': 1,
  2366. 'FIELD': 'VAL',
  2367. 'EIGHT_CONNECTEDNESS': False,
  2368. 'OUTPUT': outsource}, context, feedback),
  2369. ['gdal_polygonize.py',
  2370. source + ' ' +
  2371. '-b 1 -f "ESRI Shapefile"' + ' ' + outsource + ' ' + 'check VAL'
  2372. ])
  2373. # 8 connectedness
  2374. self.assertEqual(
  2375. alg.getConsoleCommands({'INPUT': source,
  2376. 'BAND': 1,
  2377. 'FIELD': 'DN',
  2378. 'EIGHT_CONNECTEDNESS': True,
  2379. 'OUTPUT': outsource}, context, feedback),
  2380. ['gdal_polygonize.py',
  2381. '-8' + ' ' + source + ' ' +
  2382. '-b 1 -f "ESRI Shapefile"' + ' ' + outsource + ' ' + 'check DN'
  2383. ])
  2384. # custom output format
  2385. outsource = outdir + '/check.gpkg'
  2386. self.assertEqual(
  2387. alg.getConsoleCommands({'INPUT': source,
  2388. 'BAND': 1,
  2389. 'FIELD': 'DN',
  2390. 'EIGHT_CONNECTEDNESS': False,
  2391. 'OUTPUT': outsource}, context, feedback),
  2392. ['gdal_polygonize.py',
  2393. source + ' ' +
  2394. '-b 1 -f "GPKG"' + ' ' + outsource + ' ' + 'check DN'
  2395. ])
  2396. # additional parameters
  2397. self.assertEqual(
  2398. alg.getConsoleCommands({'INPUT': source,
  2399. 'BAND': 1,
  2400. 'FIELD': 'DN',
  2401. 'EXTRA': '-nomask -q',
  2402. 'OUTPUT': outsource}, context, feedback),
  2403. ['gdal_polygonize.py',
  2404. '-nomask -q' + ' ' + source + ' ' +
  2405. '-b 1 -f "GPKG"' + ' ' + outsource + ' ' + 'check DN'
  2406. ])
  2407. def testGdalPansharpen(self):
  2408. context = QgsProcessingContext()
  2409. feedback = QgsProcessingFeedback()
  2410. panchrom = os.path.join(testDataPath, 'dem.tif')
  2411. spectral = os.path.join(testDataPath, 'raster.tif')
  2412. with tempfile.TemporaryDirectory() as outdir:
  2413. outsource = outdir + '/out.tif'
  2414. alg = pansharp()
  2415. alg.initAlgorithm()
  2416. # defaults
  2417. self.assertEqual(
  2418. alg.getConsoleCommands({'SPECTRAL': spectral,
  2419. 'PANCHROMATIC': panchrom,
  2420. 'OUTPUT': outsource}, context, feedback),
  2421. ['gdal_pansharpen.py',
  2422. panchrom + ' ' +
  2423. spectral + ' ' +
  2424. outsource + ' ' +
  2425. '-r cubic -of GTiff'
  2426. ])
  2427. # custom resampling
  2428. self.assertEqual(
  2429. alg.getConsoleCommands({'SPECTRAL': spectral,
  2430. 'PANCHROMATIC': panchrom,
  2431. 'RESAMPLING': 4,
  2432. 'OUTPUT': outsource}, context, feedback),
  2433. ['gdal_pansharpen.py',
  2434. panchrom + ' ' +
  2435. spectral + ' ' +
  2436. outsource + ' ' +
  2437. '-r lanczos -of GTiff'
  2438. ])
  2439. # additional parameters
  2440. self.assertEqual(
  2441. alg.getConsoleCommands({'SPECTRAL': spectral,
  2442. 'PANCHROMATIC': panchrom,
  2443. 'EXTRA': '-bitdepth 12 -threads ALL_CPUS',
  2444. 'OUTPUT': outsource}, context, feedback),
  2445. ['gdal_pansharpen.py',
  2446. panchrom + ' ' +
  2447. spectral + ' ' +
  2448. outsource + ' ' +
  2449. '-r cubic -of GTiff -bitdepth 12 -threads ALL_CPUS'
  2450. ])
  2451. def testGdalViewshed(self):
  2452. context = QgsProcessingContext()
  2453. feedback = QgsProcessingFeedback()
  2454. dem = os.path.join(testDataPath, 'dem.tif')
  2455. with tempfile.TemporaryDirectory() as outdir:
  2456. outsource = outdir + '/out.tif'
  2457. alg = viewshed()
  2458. alg.initAlgorithm()
  2459. # defaults
  2460. self.assertEqual(
  2461. alg.getConsoleCommands({'INPUT': dem,
  2462. 'BAND': 1,
  2463. 'OBSERVER': '18.67274,45.80599',
  2464. 'OUTPUT': outsource}, context, feedback),
  2465. ['gdal_viewshed',
  2466. '-b 1 -ox 18.67274 -oy 45.80599 -oz 1.0 -tz 1.0 -md 100.0 -f GTiff ' +
  2467. dem + ' ' + outsource
  2468. ])
  2469. self.assertEqual(
  2470. alg.getConsoleCommands({'INPUT': dem,
  2471. 'BAND': 2,
  2472. 'OBSERVER': '18.67274,45.80599',
  2473. 'OBSERVER_HEIGHT': 1.8,
  2474. 'TARGET_HEIGHT': 20,
  2475. 'MAX_DISTANCE': 1000,
  2476. 'OUTPUT': outsource}, context, feedback),
  2477. ['gdal_viewshed',
  2478. '-b 2 -ox 18.67274 -oy 45.80599 -oz 1.8 -tz 20.0 -md 1000.0 -f GTiff ' +
  2479. dem + ' ' + outsource
  2480. ])
  2481. self.assertEqual(
  2482. alg.getConsoleCommands({'INPUT': dem,
  2483. 'BAND': 1,
  2484. 'OBSERVER': '18.67274,45.80599',
  2485. 'EXTRA': '-a_nodata=-9999 -cc 0.2',
  2486. 'OUTPUT': outsource}, context, feedback),
  2487. ['gdal_viewshed',
  2488. '-b 1 -ox 18.67274 -oy 45.80599 -oz 1.0 -tz 1.0 -md 100.0 -f GTiff ' +
  2489. '-a_nodata=-9999 -cc 0.2 ' + dem + ' ' + outsource
  2490. ])
  2491. self.assertEqual(
  2492. alg.getConsoleCommands({'INPUT': dem,
  2493. 'BAND': 1,
  2494. 'OBSERVER': '18.67274,45.80599',
  2495. 'OPTIONS': 'COMPRESS=DEFLATE|PREDICTOR=2|ZLEVEL=9',
  2496. 'OUTPUT': outsource}, context, feedback),
  2497. ['gdal_viewshed',
  2498. '-b 1 -ox 18.67274 -oy 45.80599 -oz 1.0 -tz 1.0 -md 100.0 -f GTiff ' +
  2499. '-co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 ' + dem + ' ' + outsource
  2500. ])
  2501. def testBuildVrt(self):
  2502. context = QgsProcessingContext()
  2503. feedback = QgsProcessingFeedback()
  2504. source = os.path.join(testDataPath, 'dem.tif')
  2505. alg = buildvrt()
  2506. alg.initAlgorithm()
  2507. with tempfile.TemporaryDirectory() as outdir:
  2508. # defaults
  2509. cmd = alg.getConsoleCommands({'INPUT': [source],
  2510. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2511. t = cmd[1]
  2512. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2513. self.assertEqual(cmd,
  2514. ['gdalbuildvrt',
  2515. '-overwrite -resolution average -separate -r nearest ' +
  2516. '-input_file_list buildvrtInputFiles.txt ' +
  2517. outdir + '/check.vrt'])
  2518. # custom resolution
  2519. cmd = alg.getConsoleCommands({'INPUT': [source],
  2520. 'RESOLUTION': 2,
  2521. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2522. t = cmd[1]
  2523. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2524. self.assertEqual(cmd,
  2525. ['gdalbuildvrt',
  2526. '-overwrite -resolution lowest -separate -r nearest ' +
  2527. '-input_file_list buildvrtInputFiles.txt ' +
  2528. outdir + '/check.vrt'])
  2529. # single layer
  2530. cmd = alg.getConsoleCommands({'INPUT': [source],
  2531. 'SEPARATE': False,
  2532. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2533. t = cmd[1]
  2534. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2535. self.assertEqual(cmd,
  2536. ['gdalbuildvrt',
  2537. '-overwrite -resolution average -r nearest ' +
  2538. '-input_file_list buildvrtInputFiles.txt ' +
  2539. outdir + '/check.vrt'])
  2540. # projection difference
  2541. cmd = alg.getConsoleCommands({'INPUT': [source],
  2542. 'PROJ_DIFFERENCE': True,
  2543. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2544. t = cmd[1]
  2545. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2546. self.assertEqual(cmd,
  2547. ['gdalbuildvrt',
  2548. '-overwrite -resolution average -separate -allow_projection_difference -r nearest ' +
  2549. '-input_file_list buildvrtInputFiles.txt ' +
  2550. outdir + '/check.vrt'])
  2551. # add alpha band
  2552. cmd = alg.getConsoleCommands({'INPUT': [source],
  2553. 'ADD_ALPHA': True,
  2554. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2555. t = cmd[1]
  2556. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2557. self.assertEqual(cmd,
  2558. ['gdalbuildvrt',
  2559. '-overwrite -resolution average -separate -addalpha -r nearest ' +
  2560. '-input_file_list buildvrtInputFiles.txt ' +
  2561. outdir + '/check.vrt'])
  2562. # assign CRS
  2563. cmd = alg.getConsoleCommands({'INPUT': [source],
  2564. 'ASSIGN_CRS': 'EPSG:3111',
  2565. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2566. t = cmd[1]
  2567. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2568. self.assertEqual(cmd,
  2569. ['gdalbuildvrt',
  2570. '-overwrite -resolution average -separate -a_srs EPSG:3111 -r nearest ' +
  2571. '-input_file_list buildvrtInputFiles.txt ' +
  2572. outdir + '/check.vrt'])
  2573. custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
  2574. cmd = alg.getConsoleCommands({'INPUT': [source],
  2575. 'ASSIGN_CRS': custom_crs,
  2576. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2577. t = cmd[1]
  2578. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2579. self.assertEqual(cmd,
  2580. ['gdalbuildvrt',
  2581. '-overwrite -resolution average -separate -a_srs EPSG:20936 -r nearest ' +
  2582. '-input_file_list buildvrtInputFiles.txt ' +
  2583. outdir + '/check.vrt'])
  2584. # source NODATA
  2585. cmd = alg.getConsoleCommands({'INPUT': [source],
  2586. 'SRC_NODATA': '-9999',
  2587. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2588. t = cmd[1]
  2589. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2590. self.assertEqual(cmd,
  2591. ['gdalbuildvrt',
  2592. '-overwrite -resolution average -separate -r nearest -srcnodata -9999 ' +
  2593. '-input_file_list buildvrtInputFiles.txt ' +
  2594. outdir + '/check.vrt'])
  2595. cmd = alg.getConsoleCommands({'INPUT': [source],
  2596. 'SRC_NODATA': '-9999 9999',
  2597. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2598. t = cmd[1]
  2599. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2600. self.assertEqual(cmd,
  2601. ['gdalbuildvrt',
  2602. '-overwrite -resolution average -separate -r nearest -srcnodata "-9999 9999" ' +
  2603. '-input_file_list buildvrtInputFiles.txt ' +
  2604. outdir + '/check.vrt'])
  2605. cmd = alg.getConsoleCommands({'INPUT': [source],
  2606. 'SRC_NODATA': '',
  2607. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2608. t = cmd[1]
  2609. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2610. self.assertEqual(cmd,
  2611. ['gdalbuildvrt',
  2612. '-overwrite -resolution average -separate -r nearest ' +
  2613. '-input_file_list buildvrtInputFiles.txt ' +
  2614. outdir + '/check.vrt'])
  2615. # additional parameters
  2616. cmd = alg.getConsoleCommands({'INPUT': [source],
  2617. 'EXTRA': '-overwrite -optim RASTER -vrtnodata -9999',
  2618. 'OUTPUT': outdir + '/check.vrt'}, context, feedback)
  2619. t = cmd[1]
  2620. cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
  2621. self.assertEqual(cmd,
  2622. ['gdalbuildvrt',
  2623. '-overwrite -resolution average -separate -r nearest -overwrite -optim RASTER -vrtnodata -9999 ' +
  2624. '-input_file_list buildvrtInputFiles.txt ' +
  2625. outdir + '/check.vrt'])
  2626. def testPct2Rgb(self):
  2627. context = QgsProcessingContext()
  2628. feedback = QgsProcessingFeedback()
  2629. source = os.path.join(testDataPath, 'dem.tif')
  2630. alg = pct2rgb()
  2631. alg.initAlgorithm()
  2632. with tempfile.TemporaryDirectory() as outdir:
  2633. # defaults
  2634. self.assertEqual(
  2635. alg.getConsoleCommands({'INPUT': source,
  2636. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2637. ['pct2rgb.py',
  2638. source + ' ' + outdir + '/check.tif ' +
  2639. '-of GTiff -b 1'])
  2640. # set band
  2641. self.assertEqual(
  2642. alg.getConsoleCommands({'INPUT': source,
  2643. 'BAND': 3,
  2644. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2645. ['pct2rgb.py',
  2646. source + ' ' + outdir + '/check.tif ' +
  2647. '-of GTiff -b 3'])
  2648. # set RGBA
  2649. self.assertEqual(
  2650. alg.getConsoleCommands({'INPUT': source,
  2651. 'RGBA': True,
  2652. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2653. ['pct2rgb.py',
  2654. source + ' ' + outdir + '/check.tif ' +
  2655. '-of GTiff -b 1 -rgba'])
  2656. def testRgb2Pct(self):
  2657. context = QgsProcessingContext()
  2658. feedback = QgsProcessingFeedback()
  2659. source = os.path.join(testDataPath, 'dem.tif')
  2660. alg = rgb2pct()
  2661. alg.initAlgorithm()
  2662. with tempfile.TemporaryDirectory() as outdir:
  2663. # defaults
  2664. self.assertEqual(
  2665. alg.getConsoleCommands({'INPUT': source,
  2666. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2667. ['rgb2pct.py',
  2668. '-n 2 -of GTiff ' + source + ' ' + outdir + '/check.tif'])
  2669. # set number of colors
  2670. self.assertEqual(
  2671. alg.getConsoleCommands({'INPUT': source,
  2672. 'NCOLORS': 8,
  2673. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2674. ['rgb2pct.py',
  2675. '-n 8 -of GTiff ' + source + ' ' + outdir + '/check.tif'])
  2676. def testRoughness(self):
  2677. context = QgsProcessingContext()
  2678. feedback = QgsProcessingFeedback()
  2679. source = os.path.join(testDataPath, 'dem.tif')
  2680. alg = roughness()
  2681. alg.initAlgorithm()
  2682. with tempfile.TemporaryDirectory() as outdir:
  2683. # defaults
  2684. self.assertEqual(
  2685. alg.getConsoleCommands({'INPUT': source,
  2686. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2687. ['gdaldem',
  2688. 'roughness ' + source + ' ' + outdir + '/check.tif ' + '-of GTiff -b 1'])
  2689. # set band
  2690. self.assertEqual(
  2691. alg.getConsoleCommands({'INPUT': source,
  2692. 'BAND': 3,
  2693. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2694. ['gdaldem',
  2695. 'roughness ' + source + ' ' + outdir + '/check.tif ' + '-of GTiff -b 3'])
  2696. # compute edges
  2697. self.assertEqual(
  2698. alg.getConsoleCommands({'INPUT': source,
  2699. 'COMPUTE_EDGES': True,
  2700. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2701. ['gdaldem',
  2702. 'roughness ' + source + ' ' + outdir + '/check.tif ' + '-of GTiff -b 1 -compute_edges'])
  2703. # creation options
  2704. self.assertEqual(
  2705. alg.getConsoleCommands({'INPUT': source,
  2706. 'OPTIONS': 'COMPRESS=DEFLATE|PREDICTOR=2|ZLEVEL=9',
  2707. 'OUTPUT': outdir + '/check.tif'}, context, feedback),
  2708. ['gdaldem',
  2709. 'roughness ' + source + ' ' + outdir + '/check.tif ' +
  2710. '-of GTiff -b 1 -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9'])
  2711. if __name__ == '__main__':
  2712. nose2.main()