|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + retile.py |
| 6 | + --------------------- |
| 7 | + Date : January 2016 |
| 8 | + Copyright : (C) 2016 by Médéric Ribreux |
| 9 | + Email : mederic dot ribreux at medspx dot fr |
| 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__ = 'Médéric Ribreux' |
| 21 | +__date__ = 'January 2016' |
| 22 | +__copyright__ = '(C) 2016, Médéric Ribreux' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm |
| 29 | +from processing.core.parameters import ParameterString |
| 30 | +from processing.core.parameters import ParameterSelection |
| 31 | +from processing.core.parameters import ParameterNumber |
| 32 | +from processing.core.parameters import ParameterMultipleInput |
| 33 | +from processing.core.parameters import ParameterCrs |
| 34 | +from processing.core.parameters import ParameterBoolean |
| 35 | +from processing.core.outputs import OutputDirectory |
| 36 | +from processing.tools.system import isWindows |
| 37 | +from processing.algs.gdal.GdalUtils import GdalUtils |
| 38 | +import re |
| 39 | + |
| 40 | + |
| 41 | +class retile(GdalAlgorithm): |
| 42 | + |
| 43 | + INPUT = 'INPUT' |
| 44 | + RTYPE = 'RTYPE' |
| 45 | + ONLYPYRAMIDS = 'ONLYPYRAMIDS' |
| 46 | + PYRAMIDLEVELS = 'PYRAMIDLEVELS' |
| 47 | + PIXELSIZE = 'PIXELSIZE' |
| 48 | + ALGORITHM = 'ALGORITHM' |
| 49 | + USEDIRFOREACHROW = 'USEDIRFOREACHROW' |
| 50 | + S_SRS = 'S_SRS' |
| 51 | + TARGETDIR = 'TARGETDIR' |
| 52 | + CSVFILE = 'CSVFILE' |
| 53 | + CSVDELIM = 'CSVDELIM' |
| 54 | + TILEINDEX = 'TILEINDEX' |
| 55 | + TILEINDEXFIELD = 'TILEINDEXFIELD' |
| 56 | + FORMAT = 'FORMAT' |
| 57 | + |
| 58 | + TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64'] |
| 59 | + ALGO = ['near', 'bilinear', 'cubic', 'cubicspline', 'lanczos'] |
| 60 | + |
| 61 | + def commandLineName(self): |
| 62 | + return "gdalogr:retile" |
| 63 | + |
| 64 | + def commandName(self): |
| 65 | + return "gdal_retile" |
| 66 | + |
| 67 | + def defineCharacteristics(self): |
| 68 | + self.name, self.i18n_name = self.trAlgorithm('Retile') |
| 69 | + self.group, self.i18n_group = self.trAlgorithm('[GDAL] Miscellaneous') |
| 70 | + |
| 71 | + # Required parameters |
| 72 | + self.addParameter(ParameterMultipleInput(self.INPUT, |
| 73 | + self.tr('Input layers'), |
| 74 | + ParameterMultipleInput.TYPE_RASTER)) |
| 75 | + # Advanced parameters |
| 76 | + params = [] |
| 77 | + params.append(ParameterString(self.PIXELSIZE, |
| 78 | + self.tr('Pixel size to be used for the output file (XSIZE YSIZE like 512 512)'), |
| 79 | + None, False, True)) |
| 80 | + params.append(ParameterSelection(self.ALGORITHM, |
| 81 | + self.tr('Resampling algorithm'), self.ALGO, 0, False, True)) |
| 82 | + params.append(ParameterCrs(self.S_SRS, |
| 83 | + self.tr('Override source CRS'), None, True)) |
| 84 | + params.append(ParameterNumber(self.PYRAMIDLEVELS, |
| 85 | + self.tr('Number of pyramids levels to build'), |
| 86 | + None, None, None, True)) |
| 87 | + params.append(ParameterBoolean(self.ONLYPYRAMIDS, |
| 88 | + self.tr('Build only the pyramids'), |
| 89 | + False, True)) |
| 90 | + params.append(ParameterSelection(self.RTYPE, |
| 91 | + self.tr('Output raster type'), |
| 92 | + self.TYPE, 5, False, True)) |
| 93 | + params.append(ParameterSelection(self.FORMAT, |
| 94 | + self.tr('Output raster format'), |
| 95 | + GdalUtils.getSupportedRasters().keys(), 0, False, True)) |
| 96 | + params.append(ParameterBoolean(self.USEDIRFOREACHROW, |
| 97 | + self.tr('Use a directory for each row'), |
| 98 | + False, True)) |
| 99 | + params.append(ParameterString(self.CSVFILE, |
| 100 | + self.tr('Name of the csv file containing the tile(s) georeferencing information'), |
| 101 | + None, False, True)) |
| 102 | + params.append(ParameterString(self.CSVDELIM, |
| 103 | + self.tr('Column delimiter used in the CSV file'), |
| 104 | + None, False, True)) |
| 105 | + params.append(ParameterString(self.TILEINDEX, |
| 106 | + self.tr('name of shape file containing the result tile(s) index'), |
| 107 | + None, False, True)) |
| 108 | + params.append(ParameterString(self.TILEINDEXFIELD, |
| 109 | + self.tr('name of the attribute containing the tile name in the result shape file'), |
| 110 | + None, False, True)) |
| 111 | + |
| 112 | + for param in params: |
| 113 | + param.isAdvanced = True |
| 114 | + self.addParameter(param) |
| 115 | + |
| 116 | + self.addOutput(OutputDirectory(self.TARGETDIR, |
| 117 | + self.tr('The directory where the tile result is created'))) |
| 118 | + |
| 119 | + def getConsoleCommands(self): |
| 120 | + |
| 121 | + arguments = [] |
| 122 | + |
| 123 | + if self.getParameterValue(self.RTYPE): |
| 124 | + arguments.append('-ot') |
| 125 | + arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)]) |
| 126 | + |
| 127 | + arguments.append('-of') |
| 128 | + arguments.append(GdalUtils.getSupportedRasters().keys()[self.getParameterValue(self.FORMAT)]) |
| 129 | + |
| 130 | + if self.getParameterValue(self.PIXELSIZE): |
| 131 | + pixelSize = self.getParameterValue(self.PIXELSIZE) |
| 132 | + if re.match(r'\d+ \d+', pixelSize): |
| 133 | + xsize, ysize = pixelSize.split(' ') |
| 134 | + arguments.append('-ps') |
| 135 | + arguments.append(xsize) |
| 136 | + arguments.append(ysize) |
| 137 | + |
| 138 | + if self.getParameterValue(self.ONLYPYRAMIDS): |
| 139 | + arguments.append('-pyramidOnly') |
| 140 | + |
| 141 | + if self.getParameterValue(self.USEDIRFOREACHROW): |
| 142 | + arguments.append('-useDirForEachRow') |
| 143 | + |
| 144 | + ssrs = unicode(self.getParameterValue(self.S_SRS)) |
| 145 | + if len(ssrs) > 0: |
| 146 | + arguments.append('-s_srs') |
| 147 | + arguments.append(ssrs) |
| 148 | + |
| 149 | + if self.getParameterValue(self.PYRAMIDLEVELS): |
| 150 | + arguments.append('-levels') |
| 151 | + arguments.append(unicode(self.getParameterValue(self.PYRAMIDLEVELS))) |
| 152 | + |
| 153 | + arguments.append('-r') |
| 154 | + arguments.append(self.ALGO[self.getParameterValue(self.ALGORITHM)]) |
| 155 | + |
| 156 | + # Handle CSV |
| 157 | + if self.getParameterValue(self.CSVFILE): |
| 158 | + arguments.append('-csv') |
| 159 | + arguments.append(self.getParameterValue(self.CSVFILE)) |
| 160 | + |
| 161 | + if self.getParameterValue(self.CSVFILE) and self.getParameterValue(self.CSVDELIM): |
| 162 | + arguments.append('-csvDelim') |
| 163 | + arguments.append(self.getParameterValue(self.CSVDELIM)) |
| 164 | + |
| 165 | + # Handle Shp |
| 166 | + if self.getParameterValue(self.TILEINDEX): |
| 167 | + arguments.append('-tileIndex') |
| 168 | + arguments.append(self.getParameterValue(self.TILEINDEX)) |
| 169 | + |
| 170 | + if self.getParameterValue(self.TILEINDEX) and self.getParameterValue(self.TILEINDEXFIELD): |
| 171 | + arguments.append('-tileIndexField') |
| 172 | + arguments.append(self.getParameterValue(self.TILEINDEXFIELD)) |
| 173 | + |
| 174 | + arguments.append('-targetDir') |
| 175 | + arguments.append(self.getOutputValue(self.TARGETDIR)) |
| 176 | + |
| 177 | + arguments.extend(self.getParameterValue(self.INPUT).split(';')) |
| 178 | + |
| 179 | + commands = [] |
| 180 | + if isWindows(): |
| 181 | + commands = ['cmd.exe', '/C ', 'gdal_retile.bat', |
| 182 | + GdalUtils.escapeAndJoin(arguments)] |
| 183 | + else: |
| 184 | + commands = ['gdal_retile.py', |
| 185 | + GdalUtils.escapeAndJoin(arguments)] |
| 186 | + |
| 187 | + return commands |
0 commit comments