|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + merge.py |
| 6 | + --------------------- |
| 7 | + Date : October 2014 |
| 8 | + Copyright : (C) 2014 by Radoslaw Guzinski |
| 9 | + Email : rmgu at dhi-gras dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Radoslaw Guzinski' |
| 21 | +__date__ = 'October 2014' |
| 22 | +__copyright__ = '(C) 2014, Radoslaw Guzinski' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | + |
| 29 | +from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm |
| 30 | +from processing.core.outputs import OutputRaster |
| 31 | +from processing.core.parameters import ParameterBoolean |
| 32 | +from processing.core.parameters import ParameterMultipleInput |
| 33 | +from processing.core.parameters import ParameterSelection |
| 34 | +from processing.tools.system import * |
| 35 | +from processing.algs.gdal.GdalUtils import GdalUtils |
| 36 | +import os |
| 37 | + |
| 38 | +class buildvrt(GdalAlgorithm): |
| 39 | + |
| 40 | + INPUT = 'INPUT' |
| 41 | + OUTPUT = 'OUTPUT' |
| 42 | + RESOLUTION = 'RESOLUTION' |
| 43 | + SEPARATE = 'SEPARATE' |
| 44 | + PROJ_DIFFERENCE = 'PROJ_DIFFERENCE' |
| 45 | + |
| 46 | + RESOLUTION_OPTIONS = ['average', 'highest', 'lowest'] |
| 47 | + |
| 48 | + def defineCharacteristics(self): |
| 49 | + self.name = 'Build Virtual Raster' |
| 50 | + self.group = '[GDAL] Miscellaneous' |
| 51 | + self.addParameter(ParameterMultipleInput(buildvrt.INPUT, 'Input layers', ParameterMultipleInput.TYPE_RASTER)) |
| 52 | + self.addParameter(ParameterSelection(buildvrt.RESOLUTION, 'Resolution', buildvrt.RESOLUTION_OPTIONS, 0)) |
| 53 | + self.addParameter(ParameterBoolean(buildvrt.SEPARATE, 'Layer stack', True)) |
| 54 | + self.addParameter(ParameterBoolean(buildvrt.PROJ_DIFFERENCE, 'Allow projection difference', False)) |
| 55 | + self.addOutput(OutputRaster(buildvrt.OUTPUT, 'Output layer')) |
| 56 | + |
| 57 | + def processAlgorithm(self, progress): |
| 58 | + arguments = [] |
| 59 | + arguments.append('-resolution') |
| 60 | + arguments.append(self.RESOLUTION_OPTIONS[self.getParameterValue(self.RESOLUTION)]) |
| 61 | + if self.getParameterValue(buildvrt.SEPARATE): |
| 62 | + arguments.append('-separate') |
| 63 | + if self.getParameterValue(buildvrt.PROJ_DIFFERENCE): |
| 64 | + arguments.append('-allow_projection_difference') |
| 65 | + # Always write input files to a text file in case there are many of them and the |
| 66 | + # length of the command will be longer then allowed in command prompt |
| 67 | + listFile = os.path.join(tempFolder(), 'buildvrtInputFiles.txt') |
| 68 | + with open(listFile, 'w') as f: |
| 69 | + f.write(self.getParameterValue(buildvrt.INPUT).replace(';', '\n')) |
| 70 | + arguments.append('-input_file_list') |
| 71 | + arguments.append(listFile) |
| 72 | + out = self.getOutputValue(buildvrt.OUTPUT) |
| 73 | + # Ideally the file extensions should be limited to just .vrt but I'm not sure how |
| 74 | + # to do it simply so instead a check is performed. |
| 75 | + _, ext = os.path.splitext(out) |
| 76 | + if not ext.lower() == '.vrt': |
| 77 | + out = out.replace(ext, '.vrt') |
| 78 | + self.setOutputValue(self.OUTPUT, out) |
| 79 | + arguments.append(out) |
| 80 | + |
| 81 | + |
| 82 | + GdalUtils.runGdal(['gdalbuildvrt', GdalUtils.escapeAndJoin(arguments)], progress) |
0 commit comments