Skip to content

Commit

Permalink
[processing] restore grid data metrics algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent 8c09860 commit dc7b9e4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 81 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .fillnodata import fillnodata
from .gdaltindex import gdaltindex
from .GridAverage import GridAverage
from .GridDataMetrics import GridDataMetrics
from .hillshade import hillshade
from .information import information
from .nearblack import nearblack
Expand All @@ -65,7 +66,6 @@
# from .gdal2xyz import gdal2xyz
# from .GridInvDist import GridInvDist
# from .GridNearest import GridNearest
# from .GridDataMetrics import GridDataMetrics
# from .gdalcalc import gdalcalc
# from .rasterize_over import rasterize_over
# from .retile import retile
Expand Down Expand Up @@ -142,6 +142,7 @@ def loadAlgorithms(self):
fillnodata(),
gdaltindex(),
GridAverage(),
GridDataMetrics(),
hillshade(),
information(),
nearblack(),
Expand All @@ -165,7 +166,6 @@ def loadAlgorithms(self):
# gdal2xyz(),
# GridInvDist(),
# GridNearest(),
# GridDataMetrics(),
# gdalcalc(),
# rasterize_over(),
# retile(),
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def ogrConnectionStringAndFormat(uri, context):

@staticmethod
def ogrLayerName(uri):
uri = uri.strip('"')
if os.path.isfile(uri):
return os.path.basename(os.path.splitext(uri)[0])

Expand Down
154 changes: 94 additions & 60 deletions python/plugins/processing/algs/gdal/GridDataMetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from builtins import str

__author__ = 'Alexander Bruy'
__date__ = 'October 2013'
Expand All @@ -30,13 +29,15 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsRasterFileWriter,
QgsProcessing,
QgsProcessingParameterDefinition,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterEnum,
QgsProcessingParameterField,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.tools import dataobjects
from processing.algs.gdal.GdalUtils import GdalUtils


Expand All @@ -53,39 +54,78 @@ class GridDataMetrics(GdalAlgorithm):
MIN_POINTS = 'MIN_POINTS'
ANGLE = 'ANGLE'
NODATA = 'NODATA'
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'
RTYPE = 'RTYPE'

TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']

DATA_METRICS = ['Minimum', 'Maximum', 'Range', 'Count', 'Average distance',
'Average distance between points']
TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POINT]))
self.addParameter(ParameterTableField(self.Z_FIELD,
self.tr('Z field'), self.INPUT,
ParameterTableField.DATA_TYPE_NUMBER, True))
self.addParameter(ParameterSelection(self.METRIC,
self.tr('Metrics'), self.DATA_METRICS, 0))
self.addParameter(ParameterNumber(self.RADIUS_1,
self.tr('Radius 1'), 0.0, 99999999.999999, 0.0))
self.addParameter(ParameterNumber(self.RADIUS_2,
self.tr('Radius 2'), 0.0, 99999999.999999, 0.0))
self.addParameter(ParameterNumber(self.MIN_POINTS,
self.tr('Min points'), 0.0, 99999999.999999, 0.0))
self.addParameter(ParameterNumber(self.ANGLE,
self.tr('Angle'), 0.0, 359.0, 0.0))
self.addParameter(ParameterNumber(self.NODATA,
self.tr('Nodata'), 0.0, 99999999.999999, 0.0))
self.addParameter(ParameterSelection(self.RTYPE,
self.tr('Output raster type'), self.TYPE, 5))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Interpolated metrics')))
self.metrics = ((self.tr('Minimum'), 'minimum'),
(self.tr('Maximum'), 'maximum'),
(self.tr('Range'), 'range'),
(self.tr('Count'), 'count'),
(self.tr('Average distance'), 'average_distance'),
(self.tr('Average distance between points'), 'average_distance_pts'))

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(QgsProcessingParameterEnum(self.METRIC,
self.tr('Data metric to use'),
options=[i[0] for i in self.metrics],
allowMultiple=False,
defaultValue=0))
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 (data metrics)')))

def name(self):
return 'griddatametrics'
Expand All @@ -100,41 +140,35 @@ def group(self):
return self.tr('Raster analysis')

def getConsoleCommands(self, parameters, context, feedback):
inLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
connectionString = GdalUtils.ogrConnectionString(inLayer.source(), context)
print(connectionString)

arguments = ['-l']
arguments.append(
os.path.basename(os.path.splitext(
str(self.getParameterValue(self.INPUT)))[0]))
arguments.append(GdalUtils.ogrLayerName(connectionString))

fieldName = self.getParameterValue(self.Z_FIELD)
if fieldName is not None and fieldName != '':
fieldName = self.parameterAsString(parameters, self.Z_FIELD, context)
if fieldName:
arguments.append('-zfield')
arguments.append(fieldName)

metric = self.getParameterValue(self.METRIC)
if metric == 0:
params = 'minimum'
elif metric == 1:
params = 'maximum'
elif metric == 2:
params = 'range'
elif metric == 3:
params = 'count'
elif metric == 4:
params = 'average_distance'
elif metric == 5:
params = 'average_distance_pts'

params += ':radius1=%s' % self.getParameterValue(self.RADIUS_1)
params += ':radius2=%s' % self.getParameterValue(self.RADIUS_2)
params += ':angle=%s' % self.getParameterValue(self.ANGLE)
params += ':min_points=%s' % self.getParameterValue(self.MIN_POINTS)
params += ':nodata=%s' % self.getParameterValue(self.NODATA)
params = self.metrics[self.parameterAsEnum(parameters, self.METRIC, context)][1]
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))

arguments.append('-a')
arguments.append(params)
arguments.append('-ot')
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
arguments.append(str(self.getParameterValue(self.INPUT)))
arguments.append(str(self.getOutputValue(self.OUTPUT)))
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)

return ['gdal_grid', GdalUtils.escapeAndJoin(arguments)]
38 changes: 19 additions & 19 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ tests:
hash: 3542d928bccee955b1c426cac7b698c603c9a385429ca529758083bf
type: rasterhash

- algorithm: gdal:griddatametrics
name: Data Metrics interpolation
params:
ANGLE: 0.0
INPUT:
name: custom/points_values.gml
type: vector
METRIC: '1'
MIN_POINTS: 0.0
NODATA: 0.0
RADIUS_1: 0.01
RADIUS_2: 0.02
RTYPE: '5'
Z_FIELD: EC
results:
OUTPUT:
hash: 220953299b18286b57ce093a054323e59add7c78c0429a02e51b4ad4
type: rasterhash

# - algorithm: gdal:merge
# name: Merge 2 raster layers
# params:
Expand Down Expand Up @@ -483,25 +502,6 @@ tests:
# hash: 2033731d6fd653da391a2633158f0ea755d96e1b2b2c60a9100332b1
# type: rasterhash
#
# - algorithm: gdal:griddatametrics
# name: Data Metrics interpolation
# params:
# ANGLE: 0.0
# INPUT:
# name: custom/points_values.gml
# type: vector
# METRIC: '1'
# MIN_POINTS: 0.0
# NODATA: 0.0
# RADIUS_1: 0.01
# RADIUS_2: 0.02
# RTYPE: '5'
# Z_FIELD: EC
# results:
# OUTPUT:
# hash: 220953299b18286b57ce093a054323e59add7c78c0429a02e51b4ad4
# type: rasterhash
#
# - algorithm: gdal:gridnearestneighbor
# name: Nearest Neighbor interpolation
# params:
Expand Down

0 comments on commit dc7b9e4

Please sign in to comment.