Skip to content

Commit 2327728

Browse files
committed
[processing][gdal] Fix execution of hillshade with multidirectional argument
And add tests
1 parent 289032b commit 2327728

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

python/plugins/processing/algs/gdal/hillshade.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
147147
arguments.append(str(self.parameterAsDouble(parameters, self.Z_FACTOR, context)))
148148
arguments.append('-s')
149149
arguments.append(str(self.parameterAsDouble(parameters, self.SCALE, context)))
150-
arguments.append('-az')
151-
arguments.append(str(self.parameterAsDouble(parameters, self.AZIMUTH, context)))
150+
151+
multidirectional = self.parameterAsBool(parameters, self.MULTIDIRECTIONAL, context)
152+
# azimuth and multidirectional are mutually exclusive
153+
if not multidirectional:
154+
arguments.append('-az')
155+
arguments.append(str(self.parameterAsDouble(parameters, self.AZIMUTH, context)))
156+
152157
arguments.append('-alt')
153158
arguments.append(str(self.parameterAsDouble(parameters, self.ALTITUDE, context)))
154159

@@ -162,7 +167,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
162167
if self.parameterAsBool(parameters, self.COMBINED, context):
163168
arguments.append('-combined')
164169

165-
if self.parameterAsBool(parameters, self.MULTIDIRECTIONAL, context):
170+
if multidirectional:
166171
arguments.append('-multidirectional')
167172

168173
options = self.parameterAsString(parameters, self.OPTIONS, context)

python/plugins/processing/tests/GdalAlgorithmsTest.py

+96
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from processing.algs.gdal.GridInverseDistanceNearestNeighbor import GridInverseDistanceNearestNeighbor
4242
from processing.algs.gdal.GridLinear import GridLinear
4343
from processing.algs.gdal.GridNearestNeighbor import GridNearestNeighbor
44+
from processing.algs.gdal.hillshade import hillshade
4445
from processing.algs.gdal.ogr2ogr import ogr2ogr
4546
from processing.algs.gdal.proximity import proximity
4647
from processing.algs.gdal.rasterize import rasterize
@@ -926,6 +927,101 @@ def testOgr2Ogr(self):
926927
'-f "LIBKML" "d:/temp/my out/check.kml" ' +
927928
source + ' polys2'])
928929

930+
def testHillshade(self):
931+
context = QgsProcessingContext()
932+
feedback = QgsProcessingFeedback()
933+
source = os.path.join(testDataPath, 'dem.tif')
934+
alg = hillshade()
935+
alg.initAlgorithm()
936+
937+
self.assertEqual(
938+
alg.getConsoleCommands({'INPUT': source,
939+
'BAND': 1,
940+
'Z_FACTOR': 5,
941+
'SCALE': 2,
942+
'AZIMUTH': 90,
943+
'ALTITUDE': 20,
944+
'OUTPUT': 'd:/temp/check.tif'}, context, feedback),
945+
['gdaldem',
946+
'hillshade ' +
947+
source + ' ' +
948+
'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0'])
949+
950+
#paths with space
951+
source_with_space = os.path.join(testDataPath, 'raster with spaces.tif')
952+
self.assertEqual(
953+
alg.getConsoleCommands({'INPUT': source_with_space,
954+
'BAND': 1,
955+
'Z_FACTOR': 5,
956+
'SCALE': 2,
957+
'AZIMUTH': 90,
958+
'ALTITUDE': 20,
959+
'OUTPUT': 'd:/temp/check out.tif'}, context, feedback),
960+
['gdaldem',
961+
'hillshade ' +
962+
'"' + source_with_space + '" ' +
963+
'"d:/temp/check out.tif" -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0'])
964+
965+
# compute edges
966+
self.assertEqual(
967+
alg.getConsoleCommands({'INPUT': source,
968+
'BAND': 1,
969+
'Z_FACTOR': 5,
970+
'SCALE': 2,
971+
'AZIMUTH': 90,
972+
'ALTITUDE': 20,
973+
'COMPUTE_EDGES': True,
974+
'OUTPUT': 'd:/temp/check.tif'}, context, feedback),
975+
['gdaldem',
976+
'hillshade ' +
977+
source + ' ' +
978+
'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -compute_edges'])
979+
980+
# with ZEVENBERGEN
981+
self.assertEqual(
982+
alg.getConsoleCommands({'INPUT': source,
983+
'BAND': 1,
984+
'Z_FACTOR': 5,
985+
'SCALE': 2,
986+
'AZIMUTH': 90,
987+
'ALTITUDE': 20,
988+
'ZEVENBERGEN': True,
989+
'OUTPUT': 'd:/temp/check.tif'}, context, feedback),
990+
['gdaldem',
991+
'hillshade ' +
992+
source + ' ' +
993+
'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -alg ZevenbergenThorne'])
994+
995+
# with COMBINED
996+
self.assertEqual(
997+
alg.getConsoleCommands({'INPUT': source,
998+
'BAND': 1,
999+
'Z_FACTOR': 5,
1000+
'SCALE': 2,
1001+
'AZIMUTH': 90,
1002+
'ALTITUDE': 20,
1003+
'COMBINED': True,
1004+
'OUTPUT': 'd:/temp/check.tif'}, context, feedback),
1005+
['gdaldem',
1006+
'hillshade ' +
1007+
source + ' ' +
1008+
'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -combined'])
1009+
1010+
# with multidirectional - "az" argument is not allowed!
1011+
self.assertEqual(
1012+
alg.getConsoleCommands({'INPUT': source,
1013+
'BAND': 1,
1014+
'Z_FACTOR': 5,
1015+
'SCALE': 2,
1016+
'AZIMUTH': 90,
1017+
'ALTITUDE': 20,
1018+
'MULTIDIRECTIONAL': True,
1019+
'OUTPUT': 'd:/temp/check.tif'}, context, feedback),
1020+
['gdaldem',
1021+
'hillshade ' +
1022+
source + ' ' +
1023+
'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -alt 20.0 -multidirectional'])
1024+
9291025
def testProximity(self):
9301026
context = QgsProcessingContext()
9311027
feedback = QgsProcessingFeedback()
Binary file not shown.

0 commit comments

Comments
 (0)