2016-10-10 20:07:42 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2016-12-19 20:48:52 +02:00
|
|
|
IdwInterpolation.py
|
2016-10-10 20:07:42 +03:00
|
|
|
---------------------
|
|
|
|
Date : October 2016
|
|
|
|
Copyright : (C) 2016 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 2016'
|
|
|
|
__copyright__ = '(C) 2016, Alexander Bruy'
|
|
|
|
|
2016-10-11 11:50:09 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
|
2017-05-02 14:36:23 +10:00
|
|
|
from qgis.core import (QgsRectangle,
|
2017-05-16 16:36:00 +10:00
|
|
|
QgsProcessingUtils,
|
2017-08-19 04:03:50 +10:00
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterExtent,
|
2018-12-30 06:52:34 +02:00
|
|
|
QgsProcessingParameterDefinition,
|
2017-08-19 04:03:50 +10:00
|
|
|
QgsProcessingParameterRasterDestination,
|
|
|
|
QgsProcessingException)
|
2016-10-10 20:07:42 +03:00
|
|
|
from qgis.analysis import (QgsInterpolator,
|
|
|
|
QgsIDWInterpolator,
|
2017-08-19 04:03:50 +10:00
|
|
|
QgsGridFileWriter)
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2018-12-29 17:13:25 +02:00
|
|
|
from processing.algs.qgis.ui.InterpolationWidgets import ParameterInterpolationData, ParameterPixelSize
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2016-10-11 11:50:09 +03:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class IdwInterpolation(QgisAlgorithm):
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2016-12-19 20:48:52 +02:00
|
|
|
INTERPOLATION_DATA = 'INTERPOLATION_DATA'
|
2016-10-10 20:07:42 +03:00
|
|
|
DISTANCE_COEFFICIENT = 'DISTANCE_COEFFICIENT'
|
2018-12-29 17:13:25 +02:00
|
|
|
PIXEL_SIZE = 'PIXEL_SIZE'
|
2018-12-30 06:52:34 +02:00
|
|
|
COLUMNS = 'COLUMNS'
|
|
|
|
ROWS = 'ROWS'
|
2016-10-10 20:07:42 +03:00
|
|
|
EXTENT = 'EXTENT'
|
2017-08-19 04:03:50 +10:00
|
|
|
OUTPUT = 'OUTPUT'
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-10-11 11:50:09 +03:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'interpolation.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Interpolation')
|
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'interpolation'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2017-07-10 16:31:14 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2016-12-19 20:48:52 +02:00
|
|
|
|
|
|
|
self.addParameter(ParameterInterpolationData(self.INTERPOLATION_DATA,
|
|
|
|
self.tr('Input layer(s)')))
|
2017-08-19 04:03:50 +10:00
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE_COEFFICIENT,
|
|
|
|
self.tr('Distance coefficient P'), type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=0.0, maxValue=99.99, defaultValue=2.0))
|
|
|
|
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
|
|
|
|
self.tr('Extent'),
|
|
|
|
optional=False))
|
2018-12-29 17:13:25 +02:00
|
|
|
pixel_size_param = ParameterPixelSize(self.PIXEL_SIZE,
|
|
|
|
self.tr('Output raster size'),
|
|
|
|
layersData=self.INTERPOLATION_DATA,
|
|
|
|
extent=self.EXTENT,
|
|
|
|
minValue=0.0,
|
|
|
|
default=0.1)
|
|
|
|
self.addParameter(pixel_size_param)
|
|
|
|
|
2018-12-30 06:52:34 +02:00
|
|
|
cols_param = QgsProcessingParameterNumber(self.COLUMNS,
|
|
|
|
self.tr('Number of columns'),
|
|
|
|
optional=True,
|
|
|
|
minValue=0, maxValue=10000000)
|
|
|
|
cols_param.setFlags(cols_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
|
|
|
|
self.addParameter(cols_param)
|
|
|
|
|
|
|
|
rows_param = QgsProcessingParameterNumber(self.ROWS,
|
|
|
|
self.tr('Number of rows'),
|
|
|
|
optional=True,
|
|
|
|
minValue=0, maxValue=10000000)
|
|
|
|
rows_param.setFlags(rows_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
|
|
|
|
self.addParameter(rows_param)
|
|
|
|
|
2017-08-19 04:03:50 +10:00
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
|
|
|
|
self.tr('Interpolated')))
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'idwinterpolation'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('IDW interpolation')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-08-19 04:03:50 +10:00
|
|
|
interpolationData = ParameterInterpolationData.parseValue(parameters[self.INTERPOLATION_DATA])
|
|
|
|
coefficient = self.parameterAsDouble(parameters, self.DISTANCE_COEFFICIENT, context)
|
|
|
|
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
|
2018-12-29 17:13:25 +02:00
|
|
|
pixel_size = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)
|
2017-08-19 04:03:50 +10:00
|
|
|
output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2018-12-30 06:52:34 +02:00
|
|
|
columns = self.parameterAsInt(parameters, self.COLUMNS, context)
|
|
|
|
rows = self.parameterAsInt(parameters, self.ROWS, context)
|
|
|
|
if columns == 0:
|
|
|
|
columns = max(round(bbox.width() / pixel_size) + 1, 1)
|
|
|
|
if rows == 0:
|
|
|
|
rows = max(round(bbox.height() / pixel_size) + 1, 1)
|
|
|
|
|
2016-12-19 20:48:52 +02:00
|
|
|
if interpolationData is None:
|
2017-08-19 04:03:50 +10:00
|
|
|
raise QgsProcessingException(
|
2016-12-19 20:48:52 +02:00
|
|
|
self.tr('You need to specify at least one input layer.'))
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2016-12-19 20:48:52 +02:00
|
|
|
layerData = []
|
2017-04-05 18:35:55 +10:00
|
|
|
layers = []
|
2019-01-22 10:15:07 +02:00
|
|
|
for row in interpolationData.split('::|::'):
|
2018-04-19 16:39:41 +07:00
|
|
|
v = row.split('::~::')
|
2016-12-19 20:48:52 +02:00
|
|
|
data = QgsInterpolator.LayerData()
|
2017-04-05 18:35:55 +10:00
|
|
|
|
|
|
|
# need to keep a reference until interpolation is complete
|
2017-11-02 20:07:15 +10:00
|
|
|
layer = QgsProcessingUtils.variantToSource(v[0], context)
|
|
|
|
data.source = layer
|
2017-04-05 18:35:55 +10:00
|
|
|
layers.append(layer)
|
|
|
|
|
2017-11-03 09:18:45 +10:00
|
|
|
data.valueSource = int(v[1])
|
2016-12-19 20:48:52 +02:00
|
|
|
data.interpolationAttribute = int(v[2])
|
|
|
|
if v[3] == '0':
|
2017-11-02 20:07:15 +10:00
|
|
|
data.sourceType = QgsInterpolator.SourcePoints
|
2016-12-19 20:48:52 +02:00
|
|
|
elif v[3] == '1':
|
2017-11-02 20:07:15 +10:00
|
|
|
data.sourceType = QgsInterpolator.SourceStructureLines
|
2016-12-19 20:48:52 +02:00
|
|
|
else:
|
2017-11-02 20:07:15 +10:00
|
|
|
data.sourceType = QgsInterpolator.SourceBreakLines
|
2016-12-19 20:48:52 +02:00
|
|
|
layerData.append(data)
|
|
|
|
|
|
|
|
interpolator = QgsIDWInterpolator(layerData)
|
2016-10-10 20:07:42 +03:00
|
|
|
interpolator.setDistanceCoefficient(coefficient)
|
|
|
|
|
|
|
|
writer = QgsGridFileWriter(interpolator,
|
|
|
|
output,
|
|
|
|
bbox,
|
|
|
|
columns,
|
2017-11-03 14:00:07 +10:00
|
|
|
rows)
|
2016-10-10 20:07:42 +03:00
|
|
|
|
2017-08-19 04:08:04 +10:00
|
|
|
writer.writeFile(feedback)
|
2017-08-19 04:03:50 +10:00
|
|
|
return {self.OUTPUT: output}
|