IdwInterpolation.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """
  2. ***************************************************************************
  3. IdwInterpolation.py
  4. ---------------------
  5. Date : October 2016
  6. Copyright : (C) 2016 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__ = 'October 2016'
  19. __copyright__ = '(C) 2016, Alexander Bruy'
  20. import os
  21. import math
  22. from qgis.PyQt.QtGui import QIcon
  23. from qgis.core import (QgsRectangle,
  24. QgsProcessingUtils,
  25. QgsProcessingParameterNumber,
  26. QgsProcessingParameterExtent,
  27. QgsProcessingParameterDefinition,
  28. QgsProcessingParameterRasterDestination,
  29. QgsProcessingException)
  30. from qgis.analysis import (QgsInterpolator,
  31. QgsIDWInterpolator,
  32. QgsGridFileWriter)
  33. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  34. from processing.algs.qgis.ui.InterpolationWidgets import ParameterInterpolationData, ParameterPixelSize
  35. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  36. class IdwInterpolation(QgisAlgorithm):
  37. INTERPOLATION_DATA = 'INTERPOLATION_DATA'
  38. DISTANCE_COEFFICIENT = 'DISTANCE_COEFFICIENT'
  39. PIXEL_SIZE = 'PIXEL_SIZE'
  40. COLUMNS = 'COLUMNS'
  41. ROWS = 'ROWS'
  42. EXTENT = 'EXTENT'
  43. OUTPUT = 'OUTPUT'
  44. def icon(self):
  45. return QIcon(os.path.join(pluginPath, 'images', 'interpolation.png'))
  46. def group(self):
  47. return self.tr('Interpolation')
  48. def groupId(self):
  49. return 'interpolation'
  50. def __init__(self):
  51. super().__init__()
  52. def initAlgorithm(self, config=None):
  53. self.addParameter(ParameterInterpolationData(self.INTERPOLATION_DATA,
  54. self.tr('Input layer(s)')))
  55. self.addParameter(QgsProcessingParameterNumber(self.DISTANCE_COEFFICIENT,
  56. self.tr('Distance coefficient P'), type=QgsProcessingParameterNumber.Double,
  57. minValue=0.0, maxValue=99.99, defaultValue=2.0))
  58. self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
  59. self.tr('Extent'),
  60. optional=False))
  61. pixel_size_param = ParameterPixelSize(self.PIXEL_SIZE,
  62. self.tr('Output raster size'),
  63. layersData=self.INTERPOLATION_DATA,
  64. extent=self.EXTENT,
  65. minValue=0.0,
  66. default=0.1)
  67. self.addParameter(pixel_size_param)
  68. cols_param = QgsProcessingParameterNumber(self.COLUMNS,
  69. self.tr('Number of columns'),
  70. optional=True,
  71. minValue=0, maxValue=10000000)
  72. cols_param.setFlags(cols_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
  73. self.addParameter(cols_param)
  74. rows_param = QgsProcessingParameterNumber(self.ROWS,
  75. self.tr('Number of rows'),
  76. optional=True,
  77. minValue=0, maxValue=10000000)
  78. rows_param.setFlags(rows_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
  79. self.addParameter(rows_param)
  80. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
  81. self.tr('Interpolated')))
  82. def name(self):
  83. return 'idwinterpolation'
  84. def displayName(self):
  85. return self.tr('IDW interpolation')
  86. def processAlgorithm(self, parameters, context, feedback):
  87. interpolationData = ParameterInterpolationData.parseValue(parameters[self.INTERPOLATION_DATA])
  88. coefficient = self.parameterAsDouble(parameters, self.DISTANCE_COEFFICIENT, context)
  89. bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
  90. pixel_size = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)
  91. output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  92. columns = self.parameterAsInt(parameters, self.COLUMNS, context)
  93. rows = self.parameterAsInt(parameters, self.ROWS, context)
  94. if columns == 0:
  95. columns = max(math.ceil(bbox.width() / pixel_size), 1)
  96. if rows == 0:
  97. rows = max(math.ceil(bbox.height() / pixel_size), 1)
  98. if interpolationData is None:
  99. raise QgsProcessingException(
  100. self.tr('You need to specify at least one input layer.'))
  101. layerData = []
  102. layers = []
  103. for i, row in enumerate(interpolationData.split('::|::')):
  104. v = row.split('::~::')
  105. data = QgsInterpolator.LayerData()
  106. # need to keep a reference until interpolation is complete
  107. layer = QgsProcessingUtils.variantToSource(v[0], context)
  108. data.source = layer
  109. data.transformContext = context.transformContext()
  110. layers.append(layer)
  111. data.valueSource = int(v[1])
  112. data.interpolationAttribute = int(v[2])
  113. if data.valueSource == QgsInterpolator.ValueAttribute and data.interpolationAttribute == -1:
  114. raise QgsProcessingException(self.tr(
  115. 'Layer {} is set to use a value attribute, but no attribute was set').format(i + 1))
  116. if v[3] == '0':
  117. data.sourceType = QgsInterpolator.SourcePoints
  118. elif v[3] == '1':
  119. data.sourceType = QgsInterpolator.SourceStructureLines
  120. else:
  121. data.sourceType = QgsInterpolator.SourceBreakLines
  122. layerData.append(data)
  123. interpolator = QgsIDWInterpolator(layerData)
  124. interpolator.setDistanceCoefficient(coefficient)
  125. writer = QgsGridFileWriter(interpolator,
  126. output,
  127. bbox,
  128. columns,
  129. rows)
  130. writer.writeFile(feedback)
  131. return {self.OUTPUT: output}