gdal2xyz.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. ***************************************************************************
  3. gdal2xyz.py
  4. ---------------------
  5. Date : September 2013
  6. Copyright : (C) 2013 by Alexander Bruy
  7. Email : alexander dot bruy at gmail dot com
  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__ = 'Alexander Bruy'
  18. __date__ = 'September 2013'
  19. __copyright__ = '(C) 2013, Alexander Bruy'
  20. from qgis.core import (QgsProcessingAlgorithm,
  21. QgsProcessingException,
  22. QgsProcessingParameterRasterLayer,
  23. QgsProcessingParameterBand,
  24. QgsProcessingParameterBoolean,
  25. QgsProcessingParameterFileDestination,
  26. QgsProcessingParameterNumber
  27. )
  28. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  29. from processing.algs.gdal.GdalUtils import GdalUtils
  30. from processing.tools.system import isWindows
  31. class gdal2xyz(GdalAlgorithm):
  32. INPUT = 'INPUT'
  33. BAND = 'BAND'
  34. SRCNODATA = 'NODATA_INPUT'
  35. DSTNODATA = 'NODATA_OUTPUT'
  36. SKIPNODATA = 'SKIP_NODATA'
  37. CSV = 'CSV'
  38. OUTPUT = 'OUTPUT'
  39. def __init__(self):
  40. super().__init__()
  41. def initAlgorithm(self, config=None):
  42. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
  43. self.tr('Input layer')))
  44. self.addParameter(QgsProcessingParameterBand(self.BAND,
  45. self.tr('Band number'),
  46. 1,
  47. parentLayerParameterName=self.INPUT))
  48. self.addParameter(QgsProcessingParameterNumber(self.SRCNODATA,
  49. self.tr('Input pixel value to treat as "nodata"'),
  50. optional=True)) # GDAL > 3.6.3
  51. self.addParameter(QgsProcessingParameterNumber(self.DSTNODATA,
  52. self.tr('Assign specified "nodata" value to output'),
  53. optional=True)) # GDAL > 3.6.3
  54. self.addParameter(QgsProcessingParameterBoolean(self.SKIPNODATA,
  55. self.tr('Do not output nodata values'),
  56. defaultValue=False)) # GDAL > 3.3
  57. self.addParameter(QgsProcessingParameterBoolean(self.CSV,
  58. self.tr('Output comma-separated values'),
  59. defaultValue=False))
  60. self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT,
  61. self.tr('XYZ ASCII file'),
  62. self.tr('CSV files (*.csv)')))
  63. def name(self):
  64. return 'gdal2xyz'
  65. def displayName(self):
  66. return self.tr('gdal2xyz')
  67. def group(self):
  68. return self.tr('Raster conversion')
  69. def groupId(self):
  70. return 'rasterconversion'
  71. def commandName(self):
  72. return 'gdal2xyz'
  73. def flags(self):
  74. return super().flags() | QgsProcessingAlgorithm.FlagDisplayNameIsLiteral
  75. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  76. arguments = [
  77. '-band',
  78. str(self.parameterAsInt(parameters, self.BAND, context))
  79. ]
  80. if self.SRCNODATA in parameters and parameters[self.SRCNODATA] is not None:
  81. if GdalUtils.version() > 3060300: # src/dstnodata broken <= 3.6.3 https://github.com/OSGeo/gdal/issues/7410
  82. srcnodata = self.parameterAsDouble(parameters, self.SRCNODATA, context)
  83. arguments.append('-srcnodata')
  84. arguments.append(srcnodata)
  85. else:
  86. raise QgsProcessingException(self.tr('The source nodata option (-srcnodata) is only available on GDAL 3.6.4 or later'))
  87. if self.DSTNODATA in parameters and parameters[self.DSTNODATA] is not None:
  88. if GdalUtils.version() > 3060300: # src/dstnodata broken <= 3.6.3 https://github.com/OSGeo/gdal/issues/7410
  89. dstnodata = self.parameterAsDouble(parameters, self.DSTNODATA, context)
  90. arguments.append('-dstnodata')
  91. arguments.append(dstnodata)
  92. else:
  93. raise QgsProcessingException(self.tr('The destination nodata option (-dstnodata) is only available on GDAL 3.6.4 or later'))
  94. if self.SKIPNODATA in parameters:
  95. if GdalUtils.version() >= 3030000: # skipnodata added at GDAL 3.3
  96. if self.parameterAsBoolean(parameters, self.SKIPNODATA, context):
  97. arguments.append('-skipnodata')
  98. else:
  99. raise QgsProcessingException(self.tr('The skip nodata option (-skipnodata) is only available on GDAL 3.3 or later'))
  100. if self.parameterAsBoolean(parameters, self.CSV, context):
  101. arguments.append('-csv')
  102. raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  103. if raster is None:
  104. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  105. arguments.append(raster.source())
  106. arguments.append(self.parameterAsFileOutput(parameters, self.OUTPUT, context))
  107. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]