2014-10-06 16:58:24 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
merge.py
|
|
|
|
---------------------
|
|
|
|
Date : October 2014
|
|
|
|
Copyright : (C) 2014 by Radoslaw Guzinski
|
|
|
|
Email : rmgu at dhi-gras 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__ = 'Radoslaw Guzinski'
|
|
|
|
__date__ = 'October 2014'
|
|
|
|
__copyright__ = '(C) 2014, Radoslaw Guzinski'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__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
|
2014-10-06 16:58:24 +02:00
|
|
|
|
2017-08-15 21:29:47 +10:00
|
|
|
from qgis.core import (QgsProcessing,
|
|
|
|
QgsProperty,
|
|
|
|
QgsProcessingParameterMultipleLayers,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterRasterDestination,
|
|
|
|
QgsProcessingOutputLayerDefinition,
|
|
|
|
QgsProcessingUtils)
|
2014-10-06 16:58:24 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
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
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
2014-10-06 16:58:24 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-10-06 16:58:24 +02:00
|
|
|
class buildvrt(GdalAlgorithm):
|
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
RESOLUTION = 'RESOLUTION'
|
|
|
|
SEPARATE = 'SEPARATE'
|
|
|
|
PROJ_DIFFERENCE = 'PROJ_DIFFERENCE'
|
2014-11-30 10:26:17 +01:00
|
|
|
|
2014-10-06 16:58:24 +02:00
|
|
|
RESOLUTION_OPTIONS = ['average', 'highest', 'lowest']
|
|
|
|
|
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-08-17 04:11:53 +10:00
|
|
|
|
|
|
|
class ParameterVrtDestination(QgsProcessingParameterRasterDestination):
|
|
|
|
|
|
|
|
def __init__(self, name, description):
|
|
|
|
super().__init__(name, description)
|
|
|
|
|
2017-08-18 00:37:58 +10:00
|
|
|
def clone(self):
|
|
|
|
copy = ParameterVrtDestination(self.name(), self.description())
|
|
|
|
return copy
|
|
|
|
|
2017-08-17 04:11:53 +10:00
|
|
|
def type(self):
|
|
|
|
return 'vrt_destination'
|
|
|
|
|
|
|
|
def defaultFileExtension(self):
|
|
|
|
return 'vrt'
|
|
|
|
|
2017-08-15 21:29:47 +10:00
|
|
|
self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT,
|
2017-09-06 16:59:48 +03:00
|
|
|
self.tr('Input layers'),
|
|
|
|
QgsProcessing.TypeRaster))
|
2017-08-15 21:29:47 +10:00
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.RESOLUTION,
|
2017-09-06 16:59:48 +03:00
|
|
|
self.tr('Resolution'),
|
|
|
|
options=self.RESOLUTION_OPTIONS,
|
|
|
|
defaultValue=0))
|
2017-08-15 21:29:47 +10:00
|
|
|
self.addParameter(QgsProcessingParameterBoolean(self.SEPARATE,
|
2017-09-06 16:59:48 +03:00
|
|
|
self.tr('Layer stack'),
|
|
|
|
defaultValue=True))
|
2017-08-15 21:29:47 +10:00
|
|
|
self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE,
|
2017-09-06 16:59:48 +03:00
|
|
|
self.tr('Allow projection difference'),
|
|
|
|
defaultValue=False))
|
2017-08-17 04:11:53 +10:00
|
|
|
self.addParameter(ParameterVrtDestination(self.OUTPUT, self.tr('Virtual')))
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'buildvirtualraster'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Build Virtual Raster')
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'vrt.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster miscellaneous')
|
|
|
|
|
2017-06-27 17:20:57 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback):
|
2014-10-06 16:58:24 +02:00
|
|
|
arguments = []
|
|
|
|
arguments.append('-resolution')
|
2017-08-15 21:29:47 +10:00
|
|
|
arguments.append(self.RESOLUTION_OPTIONS[self.parameterAsEnum(parameters, self.RESOLUTION, context)])
|
|
|
|
if self.parameterAsBool(parameters, buildvrt.SEPARATE, context):
|
2014-10-06 16:58:24 +02:00
|
|
|
arguments.append('-separate')
|
2017-08-15 21:29:47 +10:00
|
|
|
if self.parameterAsBool(parameters, buildvrt.PROJ_DIFFERENCE, context):
|
2014-10-06 16:58:24 +02:00
|
|
|
arguments.append('-allow_projection_difference')
|
|
|
|
# Always write input files to a text file in case there are many of them and the
|
|
|
|
# length of the command will be longer then allowed in command prompt
|
2017-06-21 18:23:45 +10:00
|
|
|
listFile = os.path.join(QgsProcessingUtils.tempFolder(), 'buildvrtInputFiles.txt')
|
2014-10-06 16:58:24 +02:00
|
|
|
with open(listFile, 'w') as f:
|
2017-08-15 21:29:47 +10:00
|
|
|
layers = []
|
|
|
|
for l in self.parameterAsLayerList(parameters, self.INPUT, context):
|
|
|
|
layers.append(l.source())
|
|
|
|
f.write('\n'.join(layers))
|
2014-10-06 16:58:24 +02:00
|
|
|
arguments.append('-input_file_list')
|
|
|
|
arguments.append(listFile)
|
2017-08-15 21:29:47 +10:00
|
|
|
|
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2014-10-06 16:58:24 +02:00
|
|
|
# Ideally the file extensions should be limited to just .vrt but I'm not sure how
|
|
|
|
# to do it simply so instead a check is performed.
|
|
|
|
_, ext = os.path.splitext(out)
|
|
|
|
if not ext.lower() == '.vrt':
|
2017-08-15 21:29:47 +10:00
|
|
|
out = out[:-len(ext)] + '.vrt'
|
|
|
|
if isinstance(parameters[self.OUTPUT], QgsProcessingOutputLayerDefinition):
|
|
|
|
output_def = QgsProcessingOutputLayerDefinition(parameters[self.OUTPUT])
|
|
|
|
output_def.sink = QgsProperty.fromValue(out)
|
|
|
|
self.setOutputValue(self.OUTPUT, output_def)
|
|
|
|
else:
|
|
|
|
self.setOutputValue(self.OUTPUT, out)
|
2014-10-06 16:58:24 +02:00
|
|
|
arguments.append(out)
|
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return ['gdalbuildvrt', GdalUtils.escapeAndJoin(arguments)]
|