|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + GridInverseDistanceNearestNeighbor.py |
| 6 | + --------------------- |
| 7 | + Date : September 2017 |
| 8 | + Copyright : (C) 2017 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'September 2017' |
| 22 | +__copyright__ = '(C) 2017, Alexander Bruy' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | + |
| 29 | +import os |
| 30 | + |
| 31 | +from qgis.PyQt.QtGui import QIcon |
| 32 | + |
| 33 | +from qgis.core import (QgsRasterFileWriter, |
| 34 | + QgsProcessing, |
| 35 | + QgsProcessingParameterDefinition, |
| 36 | + QgsProcessingParameterVectorLayer, |
| 37 | + QgsProcessingParameterEnum, |
| 38 | + QgsProcessingParameterField, |
| 39 | + QgsProcessingParameterNumber, |
| 40 | + QgsProcessingParameterRasterDestination) |
| 41 | +from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm |
| 42 | +from processing.algs.gdal.GdalUtils import GdalUtils |
| 43 | + |
| 44 | +pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] |
| 45 | + |
| 46 | + |
| 47 | +class GridInverseDistanceNearestNeighbor(GdalAlgorithm): |
| 48 | + |
| 49 | + INPUT = 'INPUT' |
| 50 | + Z_FIELD = 'Z_FIELD' |
| 51 | + POWER = 'POWER' |
| 52 | + SMOOTHING = 'SMOOTHING' |
| 53 | + RADIUS = 'RADIUS' |
| 54 | + MAX_POINTS = 'MAX_POINTS' |
| 55 | + MIN_POINTS = 'MIN_POINTS' |
| 56 | + NODATA = 'NODATA' |
| 57 | + DATA_TYPE = 'DATA_TYPE' |
| 58 | + OUTPUT = 'OUTPUT' |
| 59 | + |
| 60 | + TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64'] |
| 61 | + |
| 62 | + def __init__(self): |
| 63 | + super().__init__() |
| 64 | + |
| 65 | + def initAlgorithm(self, config=None): |
| 66 | + self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, |
| 67 | + self.tr('Point layer'), |
| 68 | + [QgsProcessing.TypeVectorPoint])) |
| 69 | + |
| 70 | + z_field_param = QgsProcessingParameterField(self.Z_FIELD, |
| 71 | + self.tr('Z value from field'), |
| 72 | + None, |
| 73 | + self.INPUT, |
| 74 | + QgsProcessingParameterField.Numeric, |
| 75 | + optional=True) |
| 76 | + z_field_param.setFlags(z_field_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) |
| 77 | + self.addParameter(z_field_param) |
| 78 | + |
| 79 | + self.addParameter(QgsProcessingParameterNumber(self.POWER, |
| 80 | + self.tr('Weighting power'), |
| 81 | + type=QgsProcessingParameterNumber.Double, |
| 82 | + minValue=0.0, |
| 83 | + maxValue=100.0, |
| 84 | + defaultValue=2.0)) |
| 85 | + self.addParameter(QgsProcessingParameterNumber(self.SMOOTHING, |
| 86 | + self.tr('Smoothing'), |
| 87 | + type=QgsProcessingParameterNumber.Double, |
| 88 | + minValue=0.0, |
| 89 | + maxValue=99999999.999999, |
| 90 | + defaultValue=0.0)) |
| 91 | + self.addParameter(QgsProcessingParameterNumber(self.RADIUS, |
| 92 | + self.tr('The radius of the search circle'), |
| 93 | + type=QgsProcessingParameterNumber.Double, |
| 94 | + minValue=0.0, |
| 95 | + maxValue=99999999.999999, |
| 96 | + defaultValue=1.0)) |
| 97 | + self.addParameter(QgsProcessingParameterNumber(self.MAX_POINTS, |
| 98 | + self.tr('Maximum number of data points to use'), |
| 99 | + type=QgsProcessingParameterNumber.Integer, |
| 100 | + minValue=0, |
| 101 | + maxValue=99999999, |
| 102 | + defaultValue=0)) |
| 103 | + self.addParameter(QgsProcessingParameterNumber(self.MIN_POINTS, |
| 104 | + self.tr('Minimum number of data points to use'), |
| 105 | + type=QgsProcessingParameterNumber.Integer, |
| 106 | + minValue=0, |
| 107 | + maxValue=99999999, |
| 108 | + defaultValue=0)) |
| 109 | + self.addParameter(QgsProcessingParameterNumber(self.NODATA, |
| 110 | + self.tr('NODATA marker to fill empty points'), |
| 111 | + type=QgsProcessingParameterNumber.Double, |
| 112 | + minValue=-99999999.999999, |
| 113 | + maxValue=99999999.999999, |
| 114 | + defaultValue=0.0)) |
| 115 | + self.addParameter(QgsProcessingParameterEnum(self.DATA_TYPE, |
| 116 | + self.tr('Output data type'), |
| 117 | + self.TYPE, |
| 118 | + allowMultiple=False, |
| 119 | + defaultValue=5)) |
| 120 | + |
| 121 | + self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, |
| 122 | + self.tr('Interpolated (IDW)'))) |
| 123 | + |
| 124 | + def name(self): |
| 125 | + return 'gridinversedistancenearestneighbor' |
| 126 | + |
| 127 | + def displayName(self): |
| 128 | + return self.tr('Grid (IDW with nearest neighbor searching)') |
| 129 | + |
| 130 | + def group(self): |
| 131 | + return self.tr('Raster analysis') |
| 132 | + |
| 133 | + def icon(self): |
| 134 | + return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'grid.png')) |
| 135 | + |
| 136 | + def getConsoleCommands(self, parameters, context, feedback): |
| 137 | + inLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context) |
| 138 | + connectionString = GdalUtils.ogrConnectionString(inLayer.source(), context) |
| 139 | + |
| 140 | + arguments = ['-l'] |
| 141 | + arguments.append(GdalUtils.ogrLayerName(connectionString)) |
| 142 | + |
| 143 | + fieldName = self.parameterAsString(parameters, self.Z_FIELD, context) |
| 144 | + if fieldName: |
| 145 | + arguments.append('-zfield') |
| 146 | + arguments.append(fieldName) |
| 147 | + |
| 148 | + params = 'invdistnn' |
| 149 | + params += ':power={}'.format(self.parameterAsDouble(parameters, self.POWER, context)) |
| 150 | + params += ':smothing={}'.format(self.parameterAsDouble(parameters, self.SMOOTHING, context)) |
| 151 | + params += ':radius={}'.format(self.parameterAsDouble(parameters, self.RADIUS, context)) |
| 152 | + params += ':max_points={}'.format(self.parameterAsInt(parameters, self.MAX_POINTS, context)) |
| 153 | + params += ':min_points={}'.format(self.parameterAsInt(parameters, self.MIN_POINTS, context)) |
| 154 | + params += ':nodata={}'.format(self.parameterAsDouble(parameters, self.NODATA, context)) |
| 155 | + |
| 156 | + arguments.append('-a') |
| 157 | + arguments.append(params) |
| 158 | + arguments.append('-ot') |
| 159 | + arguments.append(self.TYPE[self.parameterAsEnum(parameters, self.DATA_TYPE, context)]) |
| 160 | + |
| 161 | + out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) |
| 162 | + arguments.append('-of') |
| 163 | + arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])) |
| 164 | + |
| 165 | + arguments.append(connectionString) |
| 166 | + arguments.append(out) |
| 167 | + |
| 168 | + return ['gdal_grid', GdalUtils.escapeAndJoin(arguments)] |
0 commit comments