2013-10-13 16:12:05 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
GridAverage.py
|
|
|
|
---------------------
|
|
|
|
Date : October 2013
|
|
|
|
Copyright : (C) 2013 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'October 2013'
|
|
|
|
__copyright__ = '(C) 2013, Alexander Bruy'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
import os
|
2013-10-13 16:12:05 +03:00
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-09-01 14:30:52 +03:00
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
|
|
|
QgsProcessing,
|
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingParameterVectorLayer,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterRasterDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2013-10-13 16:12:05 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-13 16:12:05 +03:00
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class GridAverage(GdalAlgorithm):
|
2013-10-13 16:12:05 +03:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
Z_FIELD = 'Z_FIELD'
|
|
|
|
RADIUS_1 = 'RADIUS_1'
|
|
|
|
RADIUS_2 = 'RADIUS_2'
|
|
|
|
MIN_POINTS = 'MIN_POINTS'
|
|
|
|
ANGLE = 'ANGLE'
|
|
|
|
NODATA = 'NODATA'
|
2017-09-01 14:30:52 +03:00
|
|
|
DATA_TYPE = 'DATA_TYPE'
|
2013-10-13 16:12:05 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2014-11-13 23:13:13 +01:00
|
|
|
|
2017-09-01 14:30:52 +03:00
|
|
|
TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
|
2014-06-13 09:03:15 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-27 17:20:57 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-09-01 14:30:52 +03:00
|
|
|
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
|
|
|
|
self.tr('Point layer'),
|
|
|
|
[QgsProcessing.TypeVectorPoint]))
|
|
|
|
|
|
|
|
z_field_param = QgsProcessingParameterField(self.Z_FIELD,
|
|
|
|
self.tr('Z value from field'),
|
|
|
|
None,
|
|
|
|
self.INPUT,
|
|
|
|
QgsProcessingParameterField.Numeric,
|
|
|
|
optional=True)
|
|
|
|
z_field_param.setFlags(z_field_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(z_field_param)
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.RADIUS_1,
|
|
|
|
self.tr('The first radius of search ellipse'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=0.0,
|
|
|
|
maxValue=99999999.999999,
|
|
|
|
defaultValue=0.0))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.RADIUS_2,
|
|
|
|
self.tr('The second radius of search ellipse'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=0.0,
|
|
|
|
maxValue=99999999.999999,
|
|
|
|
defaultValue=0.0))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.ANGLE,
|
|
|
|
self.tr('Angle of search ellipse rotation in degrees (counter clockwise)'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=0.0,
|
|
|
|
maxValue=360.0,
|
|
|
|
defaultValue=0.0))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.MIN_POINTS,
|
|
|
|
self.tr('Minimum number of data points to use'),
|
|
|
|
type=QgsProcessingParameterNumber.Integer,
|
|
|
|
minValue=0,
|
|
|
|
maxValue=99999999,
|
|
|
|
defaultValue=0))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
|
|
|
|
self.tr('NODATA marker to fill empty points'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=-99999999.999999,
|
|
|
|
maxValue=99999999.999999,
|
|
|
|
defaultValue=0.0))
|
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.DATA_TYPE,
|
|
|
|
self.tr('Output data type'),
|
|
|
|
self.TYPE,
|
|
|
|
allowMultiple=False,
|
|
|
|
defaultValue=5))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
|
|
|
|
self.tr('Interpolated (moving average)')))
|
2013-10-13 16:12:05 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'gridaverage'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Grid (Moving average)')
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster analysis')
|
|
|
|
|
2017-09-01 14:30:52 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'grid.png'))
|
|
|
|
|
2017-06-27 17:20:57 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback):
|
2017-09-01 14:30:52 +03:00
|
|
|
inLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
|
|
|
|
connectionString = GdalUtils.ogrConnectionString(inLayer.source(), context)
|
|
|
|
|
2013-10-13 16:12:05 +03:00
|
|
|
arguments = ['-l']
|
2017-09-01 14:30:52 +03:00
|
|
|
arguments.append(GdalUtils.ogrLayerName(connectionString))
|
2013-10-13 16:12:05 +03:00
|
|
|
|
2017-09-01 14:30:52 +03:00
|
|
|
fieldName = self.parameterAsString(parameters, self.Z_FIELD, context)
|
|
|
|
if fieldName:
|
2013-10-13 16:12:05 +03:00
|
|
|
arguments.append('-zfield')
|
|
|
|
arguments.append(fieldName)
|
|
|
|
|
|
|
|
params = 'average'
|
2017-09-01 14:30:52 +03:00
|
|
|
params += ':radius1={}'.format(self.parameterAsDouble(parameters, self.RADIUS_1, context))
|
|
|
|
params += ':radius2={}'.format(self.parameterAsDouble(parameters, self.RADIUS_2, context))
|
|
|
|
params += ':angle={}'.format(self.parameterAsDouble(parameters, self.ANGLE, context))
|
|
|
|
params += ':min_points={}'.format(self.parameterAsInt(parameters, self.MIN_POINTS, context))
|
|
|
|
params += ':nodata={}'.format(self.parameterAsDouble(parameters, self.NODATA, context))
|
2013-10-13 16:12:05 +03:00
|
|
|
|
|
|
|
arguments.append('-a')
|
|
|
|
arguments.append(params)
|
2014-10-21 12:34:16 +01:00
|
|
|
arguments.append('-ot')
|
2017-09-01 14:30:52 +03:00
|
|
|
arguments.append(self.TYPE[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
|
|
|
|
|
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
|
|
|
arguments.append('-of')
|
|
|
|
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
|
|
|
|
|
|
|
|
arguments.append(connectionString)
|
|
|
|
arguments.append(out)
|
2013-10-13 16:12:05 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
return ['gdal_grid', GdalUtils.escapeAndJoin(arguments)]
|