2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2013-10-01 20:52:22 +03:00
|
|
|
self.py
|
2012-10-04 19:33:47 +02:00
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2012-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02: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
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2016-12-30 15:24:34 +02:00
|
|
|
from processing.core.parameters import (ParameterRaster,
|
|
|
|
ParameterExtent,
|
|
|
|
ParameterSelection,
|
|
|
|
ParameterCrs,
|
|
|
|
ParameterNumber,
|
2017-03-04 19:41:23 +01:00
|
|
|
ParameterString)
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.outputs import OutputRaster
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2012-09-15 18:25:25 +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
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class warp(GdalAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
SOURCE_SRS = 'SOURCE_SRS'
|
2015-06-15 11:29:20 +02:00
|
|
|
DEST_SRS = 'DEST_SRS'
|
2013-10-01 20:52:22 +03:00
|
|
|
METHOD = 'METHOD'
|
|
|
|
TR = 'TR'
|
2015-02-04 14:26:57 +00:00
|
|
|
NO_DATA = 'NO_DATA'
|
2016-03-10 09:14:29 +00:00
|
|
|
RAST_EXT = 'RAST_EXT'
|
2016-06-22 12:44:30 +03:00
|
|
|
EXT_CRS = 'EXT_CRS'
|
2016-12-30 15:24:34 +02:00
|
|
|
RTYPE = 'RTYPE'
|
|
|
|
OPTIONS = 'OPTIONS'
|
|
|
|
|
|
|
|
METHOD_OPTIONS = ['near', 'bilinear', 'cubic', 'cubicspline', 'lanczos']
|
|
|
|
TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']
|
2014-11-13 23:13:13 +01:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
def getIcon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'warp.png'))
|
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('transform,reproject,crs,srs').split(',')
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Warp (reproject)')
|
2017-02-28 12:37:48 +02:00
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Raster projections')
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer'), False))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterCrs(self.SOURCE_SRS,
|
2016-12-30 15:24:34 +02:00
|
|
|
self.tr('Source SRS'),
|
|
|
|
'',
|
|
|
|
optional=True))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterCrs(self.DEST_SRS,
|
2016-12-30 15:24:34 +02:00
|
|
|
self.tr('Destination SRS'),
|
|
|
|
'EPSG:4326'))
|
2015-02-04 14:26:57 +00:00
|
|
|
self.addParameter(ParameterString(self.NO_DATA,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr("Nodata value, leave blank to take the nodata value from input"),
|
2016-04-25 22:09:42 +01:00
|
|
|
'', optional=True))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterNumber(self.TR,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Output file resolution in target georeferenced units (leave 0 for no change)'),
|
|
|
|
0.0, None, 0.0))
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterSelection(self.METHOD,
|
2016-12-30 15:24:34 +02:00
|
|
|
self.tr('Resampling method'),
|
|
|
|
self.METHOD_OPTIONS))
|
|
|
|
self.addParameter(ParameterExtent(self.RAST_EXT,
|
|
|
|
self.tr('Raster extent'),
|
|
|
|
optional=True))
|
2015-10-22 09:37:47 +01:00
|
|
|
|
2017-02-09 14:15:31 +02:00
|
|
|
self.addParameter(ParameterCrs(self.EXT_CRS,
|
|
|
|
self.tr('CRS of the raster extent, leave blank for using Destination SRS'),
|
|
|
|
optional=True))
|
2016-06-22 12:44:30 +03:00
|
|
|
|
2016-12-30 15:24:34 +02:00
|
|
|
self.addParameter(ParameterString(self.OPTIONS,
|
|
|
|
self.tr('Additional creation options'),
|
|
|
|
optional=True,
|
|
|
|
metadata={'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}))
|
|
|
|
self.addParameter(ParameterSelection(self.RTYPE,
|
|
|
|
self.tr('Output raster type'),
|
|
|
|
self.TYPE, 5))
|
2015-10-22 09:37:47 +01:00
|
|
|
|
2015-05-18 09:29:41 +02:00
|
|
|
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Reprojected')))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
def getConsoleCommands(self):
|
2015-02-04 14:26:57 +00:00
|
|
|
srccrs = self.getParameterValue(self.SOURCE_SRS)
|
|
|
|
dstcrs = self.getParameterValue(self.DEST_SRS)
|
2016-10-19 12:09:27 +02:00
|
|
|
rastext = self.getParameterValue(self.RAST_EXT)
|
2016-06-22 12:44:30 +03:00
|
|
|
rastext_crs = self.getParameterValue(self.EXT_CRS)
|
2016-12-30 15:24:34 +02:00
|
|
|
opts = self.getParameterValue(self.OPTIONS)
|
|
|
|
noData = self.getParameterValue(self.NO_DATA)
|
|
|
|
|
|
|
|
if noData is not None:
|
|
|
|
noData = str(noData)
|
2016-03-23 13:53:04 +01:00
|
|
|
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments = []
|
2014-10-21 12:34:16 +01:00
|
|
|
arguments.append('-ot')
|
2014-10-21 12:38:44 +01:00
|
|
|
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
|
2016-10-20 10:51:07 +02:00
|
|
|
if srccrs:
|
2015-02-04 14:26:57 +00:00
|
|
|
arguments.append('-s_srs')
|
|
|
|
arguments.append(srccrs)
|
2016-10-20 10:51:07 +02:00
|
|
|
if dstcrs:
|
2015-02-04 14:26:57 +00:00
|
|
|
arguments.append('-t_srs')
|
|
|
|
arguments.append(dstcrs)
|
2016-10-20 10:51:07 +02:00
|
|
|
if noData:
|
2015-02-04 14:26:57 +00:00
|
|
|
arguments.append('-dstnodata')
|
|
|
|
arguments.append(noData)
|
2016-12-30 15:24:34 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-r')
|
|
|
|
arguments.append(
|
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
|
|
|
self.METHOD_OPTIONS[self.getParameterValue(self.METHOD)])
|
2016-12-30 15:24:34 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-of')
|
|
|
|
out = self.getOutputValue(self.OUTPUT)
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
|
2016-12-30 15:24:34 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
if self.getParameterValue(self.TR) != 0:
|
|
|
|
arguments.append('-tr')
|
2016-09-21 18:24:26 +02:00
|
|
|
arguments.append(str(self.getParameterValue(self.TR)))
|
|
|
|
arguments.append(str(self.getParameterValue(self.TR)))
|
2016-12-30 15:24:34 +02:00
|
|
|
|
2016-10-20 10:51:07 +02:00
|
|
|
if rastext:
|
|
|
|
regionCoords = rastext.split(',')
|
|
|
|
if len(regionCoords) >= 4:
|
|
|
|
arguments.append('-te')
|
|
|
|
arguments.append(regionCoords[0])
|
|
|
|
arguments.append(regionCoords[2])
|
|
|
|
arguments.append(regionCoords[1])
|
|
|
|
arguments.append(regionCoords[3])
|
|
|
|
|
2017-02-09 14:15:31 +02:00
|
|
|
if rastext_crs:
|
|
|
|
arguments.append('-te_srs')
|
|
|
|
arguments.append(rastext_crs)
|
2016-06-22 12:44:30 +03:00
|
|
|
|
2016-12-30 15:24:34 +02:00
|
|
|
if opts:
|
|
|
|
arguments.append('-co')
|
|
|
|
arguments.append(opts)
|
2016-03-25 15:37:20 +02:00
|
|
|
|
2016-10-25 17:46:52 +03:00
|
|
|
if GdalUtils.version() in [2010000, 2010100]:
|
|
|
|
arguments.append("--config GDALWARP_IGNORE_BAD_CUTLINE YES")
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append(self.getParameterValue(self.INPUT))
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments.append(out)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return ['gdalwarp', GdalUtils.escapeAndJoin(arguments)]
|