2013-09-12 21:28:27 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2017-09-08 15:47:44 +03:00
|
|
|
ClipRasterByExtent.py
|
2013-09-12 21:28:27 +03:00
|
|
|
---------------------
|
|
|
|
Date : September 2013
|
2013-09-14 14:02:47 +03:00
|
|
|
Copyright : (C) 2013 by Alexander Bruy
|
2013-09-12 21:28:27 +03:00
|
|
|
Email : alexander 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__ = 'September 2013'
|
|
|
|
__copyright__ = '(C) 2013, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-12 21:28:27 +03:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-12 21:28:27 +03:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-09-06 10:47:17 +03:00
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingParameterRasterLayer,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterExtent,
|
|
|
|
QgsProcessingParameterString,
|
|
|
|
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-09-12 21:28:27 +03:00
|
|
|
|
2017-09-06 10:47:17 +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-01 20:52:22 +03:00
|
|
|
|
2017-09-08 15:47:44 +03:00
|
|
|
class ClipRasterByExtent(GdalAlgorithm):
|
2013-09-12 21:28:27 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
2017-09-06 10:47:17 +03:00
|
|
|
EXTENT = 'PROJWIN'
|
|
|
|
NODATA = 'NODATA'
|
2016-12-30 15:24:34 +02:00
|
|
|
OPTIONS = 'OPTIONS'
|
2017-09-06 10:47:17 +03:00
|
|
|
DATA_TYPE = 'DATA_TYPE'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
|
|
|
TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
|
2015-10-30 23:30:16 +01: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-06 10:47:17 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
2017-09-06 11:18:13 +03:00
|
|
|
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
|
|
|
|
self.tr('Clipping extent')))
|
2017-09-06 10:47:17 +03:00
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
|
|
|
|
self.tr('Assign a specified nodata value to output bands'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
defaultValue=0.0,
|
|
|
|
optional=True))
|
|
|
|
|
|
|
|
options_param = QgsProcessingParameterString(self.OPTIONS,
|
|
|
|
self.tr('Additional creation parameters'),
|
|
|
|
defaultValue='',
|
|
|
|
optional=True)
|
|
|
|
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
options_param.setMetadata({
|
|
|
|
'widget_wrapper': {
|
|
|
|
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
|
|
|
|
self.addParameter(options_param)
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE,
|
|
|
|
self.tr('Output data type'),
|
|
|
|
self.TYPES,
|
|
|
|
allowMultiple=False,
|
|
|
|
defaultValue=5)
|
|
|
|
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(dataType_param)
|
2017-09-06 10:47:17 +03:00
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
|
|
|
|
self.tr('Clipped (extent)')))
|
2013-09-12 21:28:27 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'cliprasterbyextent'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Clip raster by extent')
|
|
|
|
|
2017-09-06 10:47:17 +03:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster extraction')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'raster-clip.png'))
|
|
|
|
|
2017-09-06 10:47:17 +03:00
|
|
|
def commandName(self):
|
|
|
|
return "gdal_translate"
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2017-09-06 10:47:17 +03:00
|
|
|
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
2017-09-26 16:37:57 +03:00
|
|
|
bbox = self.parameterAsExtent(parameters, self.EXTENT, context, inLayer.crs())
|
2017-09-06 10:47:17 +03:00
|
|
|
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
|
|
|
|
options = self.parameterAsString(parameters, self.OPTIONS, context)
|
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2015-10-22 09:57:38 +01:00
|
|
|
|
2013-09-12 21:28:27 +03:00
|
|
|
arguments = []
|
2017-09-06 10:47:17 +03:00
|
|
|
arguments.append('-projwin')
|
|
|
|
arguments.append(str(bbox.xMinimum()))
|
|
|
|
arguments.append(str(bbox.yMaximum()))
|
|
|
|
arguments.append(str(bbox.xMaximum()))
|
|
|
|
arguments.append(str(bbox.yMinimum()))
|
|
|
|
|
|
|
|
if nodata:
|
|
|
|
arguments.append('-a_nodata {}'.format(nodata))
|
|
|
|
|
2015-10-22 09:48:45 +01:00
|
|
|
arguments.append('-ot')
|
2017-09-06 10:47:17 +03:00
|
|
|
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
|
2013-09-12 21:28:27 +03:00
|
|
|
|
2017-09-06 10:47:17 +03:00
|
|
|
arguments.append('-of')
|
|
|
|
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
|
2013-09-12 21:28:27 +03:00
|
|
|
|
2017-09-06 10:47:17 +03:00
|
|
|
if options:
|
2016-12-30 15:24:34 +02:00
|
|
|
arguments.append('-co')
|
2017-09-06 10:47:17 +03:00
|
|
|
arguments.append(options)
|
2013-09-12 21:28:27 +03:00
|
|
|
|
2017-09-06 10:47:17 +03:00
|
|
|
arguments.append(inLayer.source())
|
2013-09-12 21:28:27 +03:00
|
|
|
arguments.append(out)
|
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return ['gdal_translate', GdalUtils.escapeAndJoin(arguments)]
|